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@537 | 28 | #include <vector> |
paul@124 | 29 | |
paul@535 | 30 | #include <resource/resource.h> |
paul@124 | 31 | #include <systypes/base.h> |
paul@124 | 32 | |
paul@534 | 33 | #include "notifier_interface.h" |
paul@534 | 34 | |
paul@124 | 35 | |
paul@124 | 36 | |
paul@479 | 37 | /* Object-specific notification details. */ |
paul@177 | 38 | |
paul@479 | 39 | class ObjectNotificationState |
paul@177 | 40 | { |
paul@177 | 41 | public: |
paul@177 | 42 | /* Synchronisation primitives for state access and notification. */ |
paul@177 | 43 | |
paul@177 | 44 | std::mutex lock; |
paul@177 | 45 | std::condition_variable condition; |
paul@177 | 46 | |
paul@479 | 47 | /* Pending notifications for monitored objects. */ |
paul@177 | 48 | |
paul@481 | 49 | notify_flags_t pending_notifications = 0; |
paul@481 | 50 | notify_values_t pending_values = NOTIFY_VALUES_NULL; |
paul@177 | 51 | |
paul@479 | 52 | /* Endpoints associated with monitored objects. */ |
paul@177 | 53 | |
paul@177 | 54 | l4_cap_idx_t endpoint = L4_INVALID_CAP; |
paul@177 | 55 | |
paul@177 | 56 | bool is_null() { return l4_is_invalid_cap(endpoint); } |
paul@177 | 57 | }; |
paul@177 | 58 | |
paul@177 | 59 | |
paul@177 | 60 | |
paul@125 | 61 | /* Collection types. */ |
paul@125 | 62 | |
paul@479 | 63 | typedef std::map<notifiable_t *, ObjectNotificationState> ObjectNotificationStates; |
paul@479 | 64 | typedef std::map<notifiable_t *, std::mutex> ObjectStateLocks; |
paul@125 | 65 | |
paul@537 | 66 | typedef std::vector<ipc_server_config_type *> ServerConfigs; |
paul@537 | 67 | |
paul@125 | 68 | |
paul@125 | 69 | |
paul@479 | 70 | /* An object for monitoring object event notifications. */ |
paul@124 | 71 | |
paul@479 | 72 | class ObjectNotifier |
paul@124 | 73 | { |
paul@124 | 74 | protected: |
paul@177 | 75 | /* General state access locking. */ |
paul@177 | 76 | |
paul@177 | 77 | std::mutex _state_lock; |
paul@177 | 78 | |
paul@479 | 79 | /* Object-specific state locking. */ |
paul@177 | 80 | |
paul@479 | 81 | ObjectStateLocks _object_locks; |
paul@177 | 82 | |
paul@177 | 83 | /* Notification state. */ |
paul@177 | 84 | |
paul@479 | 85 | ObjectNotificationStates _state; |
paul@177 | 86 | |
paul@534 | 87 | /* Notifier resource details. */ |
paul@177 | 88 | |
paul@537 | 89 | ServerConfigs _configs; |
paul@124 | 90 | bool _started = false; |
paul@539 | 91 | l4_cap_idx_t _irq = L4_INVALID_CAP; |
paul@124 | 92 | |
paul@479 | 93 | /* Convenience method to access object state. */ |
paul@177 | 94 | |
paul@479 | 95 | virtual ObjectNotificationState &object_state(notifiable_t *object, bool create); |
paul@124 | 96 | |
paul@124 | 97 | /* Helper methods. */ |
paul@124 | 98 | |
paul@479 | 99 | virtual bool _transfer(ObjectNotificationState &state, notifiable_t *object); |
paul@177 | 100 | |
paul@124 | 101 | public: |
paul@479 | 102 | virtual ~ObjectNotifier(); |
paul@180 | 103 | |
paul@124 | 104 | /* Local operations. */ |
paul@124 | 105 | |
paul@129 | 106 | virtual long start(); |
paul@129 | 107 | |
paul@539 | 108 | virtual void stop(); |
paul@539 | 109 | |
paul@479 | 110 | virtual long subscribe(notifiable_t *object, notify_flags_t flags); |
paul@124 | 111 | |
paul@479 | 112 | virtual long unsubscribe(notifiable_t *object); |
paul@124 | 113 | |
paul@483 | 114 | virtual long remove_endpoint(notifiable_t *object, l4_cap_idx_t endpoint); |
paul@483 | 115 | |
paul@534 | 116 | /* Helper methods. */ |
paul@124 | 117 | |
paul@534 | 118 | virtual void notify(notifiable_t *object, notify_flags_t flags, |
paul@534 | 119 | notify_values_t values) = 0; |
paul@124 | 120 | }; |
paul@124 | 121 | |
paul@124 | 122 | |
paul@124 | 123 | |
paul@479 | 124 | /* An object monitoring notifications for a collection of different objects. */ |
paul@177 | 125 | |
paul@479 | 126 | class GeneralObjectNotifier : public ObjectNotifier |
paul@177 | 127 | { |
paul@177 | 128 | protected: |
paul@177 | 129 | /* Locking to protect pending notification members and to coordinate access |
paul@177 | 130 | to notifications. */ |
paul@177 | 131 | |
paul@177 | 132 | std::mutex _general_lock; |
paul@177 | 133 | |
paul@177 | 134 | /* General lock synchronisation. */ |
paul@177 | 135 | |
paul@177 | 136 | std::condition_variable _general_condition; |
paul@177 | 137 | |
paul@479 | 138 | /* Objects affected by notifications. */ |
paul@177 | 139 | |
paul@479 | 140 | std::list<notifiable_t *> _affected; |
paul@177 | 141 | |
paul@177 | 142 | /* Helper methods. */ |
paul@177 | 143 | |
paul@479 | 144 | virtual bool _retrieve(notifiable_t **object); |
paul@177 | 145 | |
paul@479 | 146 | virtual bool _retrieve_for_object(notifiable_t *object); |
paul@177 | 147 | |
paul@177 | 148 | public: |
paul@479 | 149 | virtual long wait(notifiable_t **object); |
paul@534 | 150 | |
paul@534 | 151 | /* Helper methods. */ |
paul@534 | 152 | |
paul@534 | 153 | virtual void notify(notifiable_t *object, notify_flags_t flags, |
paul@534 | 154 | notify_values_t values); |
paul@177 | 155 | }; |
paul@177 | 156 | |
paul@177 | 157 | |
paul@177 | 158 | |
paul@479 | 159 | /* An object monitoring notifications for specific objects. */ |
paul@177 | 160 | |
paul@479 | 161 | class SpecificObjectNotifier : public ObjectNotifier |
paul@177 | 162 | { |
paul@534 | 163 | public: |
paul@534 | 164 | virtual long wait_object(notifiable_t *object); |
paul@534 | 165 | |
paul@177 | 166 | /* Helper methods. */ |
paul@177 | 167 | |
paul@534 | 168 | virtual void notify(notifiable_t *object, notify_flags_t flags, |
paul@534 | 169 | notify_values_t values); |
paul@534 | 170 | }; |
paul@534 | 171 | |
paul@534 | 172 | |
paul@534 | 173 | |
paul@534 | 174 | /* Object-specific notifier resource. */ |
paul@534 | 175 | |
paul@534 | 176 | class NotifierResource : public Notifier, public Resource |
paul@534 | 177 | { |
paul@534 | 178 | protected: |
paul@534 | 179 | ObjectNotifier *_notifier; |
paul@534 | 180 | notifiable_t *_object; |
paul@177 | 181 | |
paul@177 | 182 | public: |
paul@534 | 183 | explicit NotifierResource(ObjectNotifier *notifier, notifiable_t *object) |
paul@534 | 184 | : _notifier(notifier), _object(object) |
paul@534 | 185 | { |
paul@534 | 186 | } |
paul@534 | 187 | |
paul@534 | 188 | explicit NotifierResource() |
paul@534 | 189 | : _notifier(NULL), _object(NULL) |
paul@534 | 190 | { |
paul@534 | 191 | } |
paul@534 | 192 | |
paul@534 | 193 | /* Server details. */ |
paul@534 | 194 | |
paul@534 | 195 | virtual ipc_server_default_config_type config(); |
paul@534 | 196 | |
paul@534 | 197 | virtual void *interface() |
paul@534 | 198 | { return static_cast<Notifier *>(this); } |
paul@534 | 199 | |
paul@534 | 200 | /* Notifier methods. */ |
paul@534 | 201 | |
paul@534 | 202 | virtual long notify(notify_flags_t flags, notify_values_t values); |
paul@177 | 203 | }; |
paul@177 | 204 | |
paul@177 | 205 | |
paul@177 | 206 | |
paul@124 | 207 | /* Helper functions. */ |
paul@124 | 208 | |
paul@479 | 209 | SpecificObjectNotifier *notifier_get_task_notifier(); |
paul@177 | 210 | |
paul@479 | 211 | GeneralObjectNotifier *notifier_get_local_notifier(); |
paul@124 | 212 | |
paul@124 | 213 | // vim: tabstop=4 expandtab shiftwidth=4 |