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 <systypes/base.h> |
paul@124 | 30 | |
paul@124 | 31 | |
paul@124 | 32 | |
paul@479 | 33 | /* Object-specific notification details. */ |
paul@177 | 34 | |
paul@479 | 35 | class ObjectNotificationState |
paul@177 | 36 | { |
paul@177 | 37 | public: |
paul@177 | 38 | /* Synchronisation primitives for state access and notification. */ |
paul@177 | 39 | |
paul@177 | 40 | std::mutex lock; |
paul@177 | 41 | std::condition_variable condition; |
paul@177 | 42 | |
paul@479 | 43 | /* Pending notifications for monitored objects. */ |
paul@177 | 44 | |
paul@481 | 45 | notify_flags_t pending_notifications = 0; |
paul@481 | 46 | notify_values_t pending_values = NOTIFY_VALUES_NULL; |
paul@177 | 47 | |
paul@479 | 48 | /* Endpoints associated with monitored objects. */ |
paul@177 | 49 | |
paul@177 | 50 | l4_cap_idx_t endpoint = L4_INVALID_CAP; |
paul@177 | 51 | |
paul@177 | 52 | bool is_null() { return l4_is_invalid_cap(endpoint); } |
paul@177 | 53 | }; |
paul@177 | 54 | |
paul@177 | 55 | |
paul@177 | 56 | |
paul@125 | 57 | /* Collection types. */ |
paul@125 | 58 | |
paul@479 | 59 | typedef std::map<notifiable_t *, ObjectNotificationState> ObjectNotificationStates; |
paul@479 | 60 | typedef std::map<notifiable_t *, std::mutex> ObjectStateLocks; |
paul@125 | 61 | |
paul@125 | 62 | |
paul@125 | 63 | |
paul@479 | 64 | /* An object for monitoring object event notifications. */ |
paul@124 | 65 | |
paul@479 | 66 | class ObjectNotifier |
paul@124 | 67 | { |
paul@124 | 68 | protected: |
paul@177 | 69 | /* General state access locking. */ |
paul@177 | 70 | |
paul@177 | 71 | std::mutex _state_lock; |
paul@177 | 72 | |
paul@479 | 73 | /* Object-specific state locking. */ |
paul@177 | 74 | |
paul@479 | 75 | ObjectStateLocks _object_locks; |
paul@177 | 76 | |
paul@177 | 77 | /* Notification state. */ |
paul@177 | 78 | |
paul@479 | 79 | ObjectNotificationStates _state; |
paul@177 | 80 | |
paul@177 | 81 | /* Notifier thread details. */ |
paul@177 | 82 | |
paul@124 | 83 | l4_cap_idx_t _thread = L4_INVALID_CAP; |
paul@124 | 84 | bool _started = false; |
paul@124 | 85 | |
paul@479 | 86 | /* Convenience method to access object state. */ |
paul@177 | 87 | |
paul@479 | 88 | virtual ObjectNotificationState &object_state(notifiable_t *object, bool create); |
paul@124 | 89 | |
paul@124 | 90 | /* Helper methods. */ |
paul@124 | 91 | |
paul@481 | 92 | virtual void _notify(notifiable_t *object, notify_flags_t flags, |
paul@481 | 93 | notify_values_t values) = 0; |
paul@177 | 94 | |
paul@479 | 95 | virtual bool _transfer(ObjectNotificationState &state, notifiable_t *object); |
paul@177 | 96 | |
paul@124 | 97 | public: |
paul@479 | 98 | virtual ~ObjectNotifier(); |
paul@180 | 99 | |
paul@124 | 100 | /* Local operations. */ |
paul@124 | 101 | |
paul@129 | 102 | virtual long start(); |
paul@129 | 103 | |
paul@479 | 104 | virtual long subscribe(notifiable_t *object, notify_flags_t flags); |
paul@124 | 105 | |
paul@479 | 106 | virtual long unsubscribe(notifiable_t *object); |
paul@124 | 107 | |
paul@483 | 108 | virtual long get_endpoint(notifiable_t *object, l4_cap_idx_t *endpoint, bool create); |
paul@483 | 109 | |
paul@483 | 110 | virtual long remove_endpoint(notifiable_t *object, l4_cap_idx_t endpoint); |
paul@483 | 111 | |
paul@129 | 112 | /* Event handling support. */ |
paul@124 | 113 | |
paul@129 | 114 | virtual void mainloop(); |
paul@124 | 115 | }; |
paul@124 | 116 | |
paul@124 | 117 | |
paul@124 | 118 | |
paul@479 | 119 | /* An object monitoring notifications for a collection of different objects. */ |
paul@177 | 120 | |
paul@479 | 121 | class GeneralObjectNotifier : public ObjectNotifier |
paul@177 | 122 | { |
paul@177 | 123 | protected: |
paul@177 | 124 | /* Locking to protect pending notification members and to coordinate access |
paul@177 | 125 | to notifications. */ |
paul@177 | 126 | |
paul@177 | 127 | std::mutex _general_lock; |
paul@177 | 128 | |
paul@177 | 129 | /* General lock synchronisation. */ |
paul@177 | 130 | |
paul@177 | 131 | std::condition_variable _general_condition; |
paul@177 | 132 | |
paul@479 | 133 | /* Objects affected by notifications. */ |
paul@177 | 134 | |
paul@479 | 135 | std::list<notifiable_t *> _affected; |
paul@177 | 136 | |
paul@177 | 137 | /* Helper methods. */ |
paul@177 | 138 | |
paul@481 | 139 | virtual void _notify(notifiable_t *object, notify_flags_t flags, |
paul@481 | 140 | notify_values_t values); |
paul@177 | 141 | |
paul@479 | 142 | virtual bool _retrieve(notifiable_t **object); |
paul@177 | 143 | |
paul@479 | 144 | virtual bool _retrieve_for_object(notifiable_t *object); |
paul@177 | 145 | |
paul@177 | 146 | public: |
paul@479 | 147 | virtual long wait(notifiable_t **object); |
paul@177 | 148 | }; |
paul@177 | 149 | |
paul@177 | 150 | |
paul@177 | 151 | |
paul@479 | 152 | /* An object monitoring notifications for specific objects. */ |
paul@177 | 153 | |
paul@479 | 154 | class SpecificObjectNotifier : public ObjectNotifier |
paul@177 | 155 | { |
paul@177 | 156 | protected: |
paul@177 | 157 | /* Helper methods. */ |
paul@177 | 158 | |
paul@481 | 159 | virtual void _notify(notifiable_t *object, notify_flags_t flags, |
paul@481 | 160 | notify_values_t values); |
paul@177 | 161 | |
paul@177 | 162 | public: |
paul@479 | 163 | virtual long wait_object(notifiable_t *object); |
paul@177 | 164 | }; |
paul@177 | 165 | |
paul@177 | 166 | |
paul@177 | 167 | |
paul@124 | 168 | /* Helper functions. */ |
paul@124 | 169 | |
paul@479 | 170 | SpecificObjectNotifier *notifier_get_task_notifier(); |
paul@177 | 171 | |
paul@479 | 172 | GeneralObjectNotifier *notifier_get_local_notifier(); |
paul@124 | 173 | |
paul@124 | 174 | // vim: tabstop=4 expandtab shiftwidth=4 |