paul@133 | 1 | /* |
paul@133 | 2 | * Notification support. |
paul@133 | 3 | * |
paul@481 | 4 | * Copyright (C) 2021, 2022, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@133 | 5 | * |
paul@133 | 6 | * This program is free software; you can redistribute it and/or |
paul@133 | 7 | * modify it under the terms of the GNU General Public License as |
paul@133 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@133 | 9 | * the License, or (at your option) any later version. |
paul@133 | 10 | * |
paul@133 | 11 | * This program is distributed in the hope that it will be useful, |
paul@133 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@133 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@133 | 14 | * GNU General Public License for more details. |
paul@133 | 15 | * |
paul@133 | 16 | * You should have received a copy of the GNU General Public License |
paul@133 | 17 | * along with this program; if not, write to the Free Software |
paul@133 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@133 | 19 | * Boston, MA 02110-1301, USA |
paul@133 | 20 | */ |
paul@133 | 21 | |
paul@133 | 22 | #include <ipc/cap_alloc.h> |
paul@133 | 23 | |
paul@133 | 24 | #include "notification.h" |
paul@133 | 25 | #include "notifier_client.h" |
paul@288 | 26 | |
paul@288 | 27 | #include "resource_server.h" |
paul@133 | 28 | |
paul@133 | 29 | |
paul@133 | 30 | |
paul@133 | 31 | /* Initialise endpoints and flags for notifications. */ |
paul@133 | 32 | |
paul@133 | 33 | NotificationSupport::NotificationSupport(unsigned int endpoints) |
paul@138 | 34 | : _min_endpoints(endpoints) |
paul@133 | 35 | { |
paul@138 | 36 | _endpoints.resize(_min_endpoints); |
paul@133 | 37 | } |
paul@133 | 38 | |
paul@193 | 39 | NotificationSupport::~NotificationSupport() |
paul@193 | 40 | { |
paul@193 | 41 | } |
paul@193 | 42 | |
paul@140 | 43 | /* Subscribe to notifications using a notification object, returning the |
paul@140 | 44 | endpoint number. */ |
paul@140 | 45 | |
paul@288 | 46 | long NotificationSupport::subscribe(l4_cap_idx_t notifier, notify_flags_t flags, |
paul@290 | 47 | void *subscriber, unsigned int *endpoint_number) |
paul@140 | 48 | { |
paul@140 | 49 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 50 | |
paul@140 | 51 | unsigned int endpoint = _endpoints.size(); |
paul@140 | 52 | _endpoints.resize(endpoint + 1); |
paul@140 | 53 | |
paul@484 | 54 | /* Propagate deferred flags and values for new endpoints. */ |
paul@140 | 55 | |
paul@484 | 56 | if (_deferred_flags) |
paul@484 | 57 | { |
paul@484 | 58 | _endpoints[endpoint].deferred_flags = _deferred_flags; |
paul@484 | 59 | _endpoints[endpoint].deferred_values = _deferred_values; |
paul@484 | 60 | } |
paul@140 | 61 | |
paul@288 | 62 | *endpoint_number = endpoint; |
paul@290 | 63 | return _subscribe(endpoint, notifier, flags, subscriber); |
paul@140 | 64 | } |
paul@140 | 65 | |
paul@140 | 66 | /* Subscribe to a specific endpoint's notifications using a notification |
paul@140 | 67 | object. */ |
paul@133 | 68 | |
paul@288 | 69 | long NotificationSupport::subscribe(unsigned int endpoint, l4_cap_idx_t notifier, |
paul@290 | 70 | notify_flags_t flags, void *subscriber) |
paul@133 | 71 | { |
paul@140 | 72 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 73 | |
paul@138 | 74 | if (endpoint >= _endpoints.size()) |
paul@138 | 75 | _endpoints.resize(endpoint + 1); |
paul@138 | 76 | |
paul@400 | 77 | /* Propagate deferred flags for new endpoints. */ |
paul@140 | 78 | |
paul@484 | 79 | if (_deferred_flags) |
paul@484 | 80 | { |
paul@484 | 81 | _endpoints[endpoint].deferred_flags = _deferred_flags; |
paul@484 | 82 | _endpoints[endpoint].deferred_values = _deferred_values; |
paul@484 | 83 | } |
paul@140 | 84 | |
paul@290 | 85 | return _subscribe(endpoint, notifier, flags, subscriber); |
paul@288 | 86 | } |
paul@288 | 87 | |
paul@288 | 88 | /* Unsubscribe from an endpoint's notifications. */ |
paul@288 | 89 | |
paul@290 | 90 | void NotificationSupport::unsubscribe(unsigned int endpoint, void *subscriber) |
paul@288 | 91 | { |
paul@288 | 92 | std::lock_guard<std::mutex> guard(_lock); |
paul@288 | 93 | |
paul@290 | 94 | _unsubscribe(endpoint, subscriber); |
paul@140 | 95 | } |
paul@140 | 96 | |
paul@140 | 97 | /* Common subscription functionality. */ |
paul@140 | 98 | |
paul@288 | 99 | long NotificationSupport::_subscribe(unsigned int endpoint, l4_cap_idx_t notifier, |
paul@290 | 100 | notify_flags_t flags, void *subscriber) |
paul@140 | 101 | { |
paul@288 | 102 | /* Record details of the notifier itself. */ |
paul@288 | 103 | |
paul@138 | 104 | NotificationEndpoint &ep = _endpoints[endpoint]; |
paul@138 | 105 | |
paul@138 | 106 | ep.notifiers.insert(notifier); |
paul@138 | 107 | ep.flags = flags; |
paul@133 | 108 | |
paul@133 | 109 | /* Send deferred conditions held from before subscription occurred. */ |
paul@133 | 110 | |
paul@484 | 111 | if (ep.deferred_flags) |
paul@133 | 112 | { |
paul@484 | 113 | _notify(endpoint, ep.deferred_flags, ep.deferred_values); |
paul@484 | 114 | ep.deferred_flags = 0; |
paul@484 | 115 | ep.deferred_values = NOTIFY_VALUES_NULL; |
paul@133 | 116 | } |
paul@288 | 117 | |
paul@290 | 118 | /* Record the subscriber's notifier. */ |
paul@288 | 119 | |
paul@290 | 120 | _subscribers[subscriber] = notifier; |
paul@290 | 121 | return L4_EOK; |
paul@288 | 122 | } |
paul@288 | 123 | |
paul@290 | 124 | void NotificationSupport::_unsubscribe(unsigned int endpoint, void *subscriber) |
paul@288 | 125 | { |
paul@288 | 126 | if (endpoint >= _endpoints.size()) |
paul@288 | 127 | return; |
paul@288 | 128 | |
paul@290 | 129 | /* Find and remove the subscriber's notifier. */ |
paul@290 | 130 | |
paul@290 | 131 | SubscriberNotifierMap::iterator itp = _subscribers.find(subscriber); |
paul@290 | 132 | l4_cap_idx_t notifier; |
paul@290 | 133 | |
paul@290 | 134 | if (itp == _subscribers.end()) |
paul@290 | 135 | return; |
paul@290 | 136 | |
paul@290 | 137 | notifier = itp->second; |
paul@290 | 138 | _subscribers.erase(itp); |
paul@290 | 139 | |
paul@290 | 140 | /* Release the notifier. */ |
paul@290 | 141 | |
paul@288 | 142 | NotificationEndpoint &ep = _endpoints[endpoint]; |
paul@288 | 143 | NotifierSet::iterator it = ep.notifiers.find(notifier); |
paul@288 | 144 | |
paul@288 | 145 | if (it != ep.notifiers.end()) |
paul@288 | 146 | { |
paul@288 | 147 | ep.notifiers.erase(it); |
paul@288 | 148 | ipc_cap_free_um(notifier); |
paul@288 | 149 | |
paul@288 | 150 | if (ep.notifiers.empty()) |
paul@288 | 151 | { |
paul@288 | 152 | ep.flags = 0; |
paul@484 | 153 | ep.deferred_flags = 0; |
paul@484 | 154 | ep.deferred_values = NOTIFY_VALUES_NULL; |
paul@288 | 155 | } |
paul@288 | 156 | } |
paul@133 | 157 | } |
paul@133 | 158 | |
paul@137 | 159 | /* Notify a particular endpoint. */ |
paul@133 | 160 | |
paul@484 | 161 | void NotificationSupport::notify(unsigned int endpoint, notify_flags_t flags, |
paul@484 | 162 | notify_values_t values) |
paul@133 | 163 | { |
paul@140 | 164 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 165 | |
paul@484 | 166 | _notify(endpoint, flags, values); |
paul@140 | 167 | } |
paul@140 | 168 | |
paul@484 | 169 | void NotificationSupport::_notify(unsigned int endpoint, notify_flags_t flags, |
paul@484 | 170 | notify_values_t values) |
paul@140 | 171 | { |
paul@138 | 172 | if (endpoint >= _endpoints.size()) |
paul@138 | 173 | return; |
paul@138 | 174 | |
paul@138 | 175 | NotificationEndpoint &ep = _endpoints[endpoint]; |
paul@138 | 176 | |
paul@133 | 177 | /* Notify the endpoint or hold any notification for potential future |
paul@133 | 178 | subscription. */ |
paul@133 | 179 | |
paul@138 | 180 | if (!ep.notifiers.empty()) |
paul@133 | 181 | { |
paul@138 | 182 | if (flags & ep.flags) |
paul@133 | 183 | { |
paul@138 | 184 | NotifierSet::iterator it; |
paul@133 | 185 | |
paul@138 | 186 | for (it = ep.notifiers.begin(); it != ep.notifiers.end(); it++) |
paul@133 | 187 | { |
paul@133 | 188 | client_Notifier notifier(*it); |
paul@133 | 189 | |
paul@484 | 190 | notifier.notify(flags & ep.flags, values); |
paul@133 | 191 | } |
paul@133 | 192 | } |
paul@133 | 193 | } |
paul@133 | 194 | else |
paul@484 | 195 | { |
paul@484 | 196 | ep.deferred_flags = flags; |
paul@484 | 197 | ep.deferred_values = values; |
paul@484 | 198 | } |
paul@133 | 199 | } |
paul@133 | 200 | |
paul@211 | 201 | /* Notify all endpoints. */ |
paul@211 | 202 | |
paul@484 | 203 | void NotificationSupport::notify_all(notify_flags_t flags, notify_values_t values) |
paul@211 | 204 | { |
paul@211 | 205 | std::lock_guard<std::mutex> guard(_lock); |
paul@211 | 206 | |
paul@211 | 207 | for (unsigned int i = 0; i < _endpoints.size(); i++) |
paul@484 | 208 | _notify(i, flags, values); |
paul@211 | 209 | |
paul@484 | 210 | /* Accumulate flags, but only record the most recent values. */ |
paul@484 | 211 | |
paul@484 | 212 | _deferred_flags |= flags; |
paul@484 | 213 | _deferred_values = values; |
paul@211 | 214 | } |
paul@211 | 215 | |
paul@137 | 216 | /* Notify the other endpoints. */ |
paul@137 | 217 | |
paul@484 | 218 | void NotificationSupport::notify_others(unsigned int endpoint, notify_flags_t flags, |
paul@484 | 219 | notify_values_t values) |
paul@137 | 220 | { |
paul@140 | 221 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 222 | |
paul@138 | 223 | for (unsigned int i = 0; i < _endpoints.size(); i++) |
paul@137 | 224 | if (i != endpoint) |
paul@484 | 225 | _notify(i, flags, values); |
paul@140 | 226 | |
paul@484 | 227 | /* Accumulate flags, but only record the most recent values. */ |
paul@484 | 228 | |
paul@484 | 229 | _deferred_flags |= flags; |
paul@484 | 230 | _deferred_values = values; |
paul@137 | 231 | } |
paul@137 | 232 | |
paul@133 | 233 | /* Release notifiers for each endpoint. */ |
paul@133 | 234 | |
paul@133 | 235 | void NotificationSupport::release_notifiers() |
paul@133 | 236 | { |
paul@140 | 237 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 238 | |
paul@138 | 239 | for (unsigned int endpoint = 0; endpoint < _endpoints.size(); endpoint++) |
paul@133 | 240 | { |
paul@138 | 241 | NotificationEndpoint &ep = _endpoints[endpoint]; |
paul@138 | 242 | NotifierSet::iterator it; |
paul@133 | 243 | |
paul@138 | 244 | for (it = ep.notifiers.begin(); it != ep.notifiers.end(); it++) |
paul@133 | 245 | ipc_cap_free_um(*it); |
paul@133 | 246 | |
paul@138 | 247 | ep.notifiers.clear(); |
paul@138 | 248 | ep.flags = 0; |
paul@484 | 249 | ep.deferred_flags = 0; |
paul@484 | 250 | ep.deferred_values = NOTIFY_VALUES_NULL; |
paul@133 | 251 | } |
paul@133 | 252 | } |
paul@133 | 253 | |
paul@133 | 254 | // vim: tabstop=4 expandtab shiftwidth=4 |