paul@124 | 1 | /* |
paul@479 | 2 | * Generic object event notification support. |
paul@124 | 3 | * |
paul@479 | 4 | * Copyright (C) 2021, 2022, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@124 | 5 | * |
paul@124 | 6 | * This program is free software; you can redistribute it and/or |
paul@124 | 7 | * modify it under the terms of the GNU General Public License as |
paul@124 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@124 | 9 | * the License, or (at your option) any later version. |
paul@124 | 10 | * |
paul@124 | 11 | * This program is distributed in the hope that it will be useful, |
paul@124 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@124 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@124 | 14 | * GNU General Public License for more details. |
paul@124 | 15 | * |
paul@124 | 16 | * You should have received a copy of the GNU General Public License |
paul@124 | 17 | * along with this program; if not, write to the Free Software |
paul@124 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@124 | 19 | * Boston, MA 02110-1301, USA |
paul@124 | 20 | */ |
paul@124 | 21 | |
paul@124 | 22 | #pragma once |
paul@124 | 23 | |
paul@124 | 24 | #include <condition_variable> |
paul@124 | 25 | #include <list> |
paul@125 | 26 | #include <map> |
paul@124 | 27 | #include <mutex> |
paul@124 | 28 | |
paul@124 | 29 | #include <fsclient/file.h> |
paul@124 | 30 | #include <systypes/base.h> |
paul@124 | 31 | |
paul@124 | 32 | |
paul@124 | 33 | |
paul@479 | 34 | /* Object-specific notification details. */ |
paul@177 | 35 | |
paul@479 | 36 | class ObjectNotificationState |
paul@177 | 37 | { |
paul@177 | 38 | public: |
paul@177 | 39 | /* Synchronisation primitives for state access and notification. */ |
paul@177 | 40 | |
paul@177 | 41 | std::mutex lock; |
paul@177 | 42 | std::condition_variable condition; |
paul@177 | 43 | |
paul@479 | 44 | /* Pending notifications for monitored objects. */ |
paul@177 | 45 | |
paul@481 | 46 | notify_flags_t pending_notifications = 0; |
paul@481 | 47 | notify_values_t pending_values = NOTIFY_VALUES_NULL; |
paul@177 | 48 | |
paul@479 | 49 | /* Endpoints associated with monitored objects. */ |
paul@177 | 50 | |
paul@177 | 51 | l4_cap_idx_t endpoint = L4_INVALID_CAP; |
paul@177 | 52 | |
paul@177 | 53 | bool is_null() { return l4_is_invalid_cap(endpoint); } |
paul@177 | 54 | }; |
paul@177 | 55 | |
paul@177 | 56 | |
paul@177 | 57 | |
paul@125 | 58 | /* Collection types. */ |
paul@125 | 59 | |
paul@479 | 60 | typedef std::map<notifiable_t *, ObjectNotificationState> ObjectNotificationStates; |
paul@479 | 61 | typedef std::map<notifiable_t *, std::mutex> ObjectStateLocks; |
paul@125 | 62 | |
paul@125 | 63 | |
paul@125 | 64 | |
paul@479 | 65 | /* An object for monitoring object event notifications. */ |
paul@124 | 66 | |
paul@479 | 67 | class ObjectNotifier |
paul@124 | 68 | { |
paul@124 | 69 | protected: |
paul@177 | 70 | /* General state access locking. */ |
paul@177 | 71 | |
paul@177 | 72 | std::mutex _state_lock; |
paul@177 | 73 | |
paul@479 | 74 | /* Object-specific state locking. */ |
paul@177 | 75 | |
paul@479 | 76 | ObjectStateLocks _object_locks; |
paul@177 | 77 | |
paul@177 | 78 | /* Notification state. */ |
paul@177 | 79 | |
paul@479 | 80 | ObjectNotificationStates _state; |
paul@177 | 81 | |
paul@177 | 82 | /* Notifier thread details. */ |
paul@177 | 83 | |
paul@124 | 84 | l4_cap_idx_t _thread = L4_INVALID_CAP; |
paul@124 | 85 | bool _started = false; |
paul@124 | 86 | |
paul@479 | 87 | /* Convenience method to access object state. */ |
paul@177 | 88 | |
paul@479 | 89 | virtual ObjectNotificationState &object_state(notifiable_t *object, bool create); |
paul@124 | 90 | |
paul@124 | 91 | /* Helper methods. */ |
paul@124 | 92 | |
paul@481 | 93 | virtual void _notify(notifiable_t *object, notify_flags_t flags, |
paul@481 | 94 | notify_values_t values) = 0; |
paul@177 | 95 | |
paul@479 | 96 | virtual bool _transfer(ObjectNotificationState &state, notifiable_t *object); |
paul@177 | 97 | |
paul@124 | 98 | public: |
paul@479 | 99 | virtual ~ObjectNotifier(); |
paul@180 | 100 | |
paul@124 | 101 | /* Local operations. */ |
paul@124 | 102 | |
paul@129 | 103 | virtual long start(); |
paul@129 | 104 | |
paul@479 | 105 | virtual long subscribe(notifiable_t *object, notify_flags_t flags); |
paul@124 | 106 | |
paul@479 | 107 | virtual long unsubscribe(notifiable_t *object); |
paul@124 | 108 | |
paul@483 | 109 | virtual long get_endpoint(notifiable_t *object, l4_cap_idx_t *endpoint, bool create); |
paul@483 | 110 | |
paul@483 | 111 | virtual long remove_endpoint(notifiable_t *object, l4_cap_idx_t endpoint); |
paul@483 | 112 | |
paul@129 | 113 | /* Event handling support. */ |
paul@124 | 114 | |
paul@129 | 115 | virtual void mainloop(); |
paul@124 | 116 | }; |
paul@124 | 117 | |
paul@124 | 118 | |
paul@124 | 119 | |
paul@479 | 120 | /* An object monitoring notifications for a collection of different objects. */ |
paul@177 | 121 | |
paul@479 | 122 | class GeneralObjectNotifier : public ObjectNotifier |
paul@177 | 123 | { |
paul@177 | 124 | protected: |
paul@177 | 125 | /* Locking to protect pending notification members and to coordinate access |
paul@177 | 126 | to notifications. */ |
paul@177 | 127 | |
paul@177 | 128 | std::mutex _general_lock; |
paul@177 | 129 | |
paul@177 | 130 | /* General lock synchronisation. */ |
paul@177 | 131 | |
paul@177 | 132 | std::condition_variable _general_condition; |
paul@177 | 133 | |
paul@479 | 134 | /* Objects affected by notifications. */ |
paul@177 | 135 | |
paul@479 | 136 | std::list<notifiable_t *> _affected; |
paul@177 | 137 | |
paul@177 | 138 | /* Helper methods. */ |
paul@177 | 139 | |
paul@481 | 140 | virtual void _notify(notifiable_t *object, notify_flags_t flags, |
paul@481 | 141 | notify_values_t values); |
paul@177 | 142 | |
paul@479 | 143 | virtual bool _retrieve(notifiable_t **object); |
paul@177 | 144 | |
paul@479 | 145 | virtual bool _retrieve_for_object(notifiable_t *object); |
paul@177 | 146 | |
paul@177 | 147 | public: |
paul@479 | 148 | virtual long wait(notifiable_t **object); |
paul@177 | 149 | }; |
paul@177 | 150 | |
paul@177 | 151 | |
paul@177 | 152 | |
paul@479 | 153 | /* An object monitoring notifications for specific objects. */ |
paul@177 | 154 | |
paul@479 | 155 | class SpecificObjectNotifier : public ObjectNotifier |
paul@177 | 156 | { |
paul@177 | 157 | protected: |
paul@177 | 158 | /* Helper methods. */ |
paul@177 | 159 | |
paul@481 | 160 | virtual void _notify(notifiable_t *object, notify_flags_t flags, |
paul@481 | 161 | notify_values_t values); |
paul@177 | 162 | |
paul@177 | 163 | public: |
paul@479 | 164 | virtual long wait_object(notifiable_t *object); |
paul@177 | 165 | }; |
paul@177 | 166 | |
paul@177 | 167 | |
paul@177 | 168 | |
paul@124 | 169 | /* Helper functions. */ |
paul@124 | 170 | |
paul@479 | 171 | SpecificObjectNotifier *notifier_get_task_notifier(); |
paul@177 | 172 | |
paul@479 | 173 | GeneralObjectNotifier *notifier_get_local_notifier(); |
paul@124 | 174 | |
paul@124 | 175 | // vim: tabstop=4 expandtab shiftwidth=4 |