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@485 | 43 | /* Return an endpoint number, removing the mapping from the given notifier if |
paul@485 | 44 | indicated. */ |
paul@485 | 45 | |
paul@485 | 46 | long NotificationSupport::_get_endpoint(l4_cap_idx_t notifier, bool remove, unsigned int *endpoint) |
paul@485 | 47 | { |
paul@485 | 48 | NotifierEndpointMap::iterator itp = _subscribers.find(notifier); |
paul@485 | 49 | |
paul@485 | 50 | if (itp == _subscribers.end()) |
paul@485 | 51 | return -L4_ENOENT; |
paul@485 | 52 | |
paul@485 | 53 | *endpoint = itp->second; |
paul@485 | 54 | |
paul@485 | 55 | if (remove) |
paul@485 | 56 | _subscribers.erase(itp); |
paul@485 | 57 | |
paul@485 | 58 | return L4_EOK; |
paul@485 | 59 | } |
paul@485 | 60 | |
paul@140 | 61 | /* Subscribe to notifications using a notification object, returning the |
paul@140 | 62 | endpoint number. */ |
paul@140 | 63 | |
paul@485 | 64 | long NotificationSupport::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) |
paul@140 | 65 | { |
paul@140 | 66 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 67 | |
paul@140 | 68 | unsigned int endpoint = _endpoints.size(); |
paul@140 | 69 | _endpoints.resize(endpoint + 1); |
paul@140 | 70 | |
paul@484 | 71 | /* Propagate deferred flags and values for new endpoints. */ |
paul@140 | 72 | |
paul@484 | 73 | if (_deferred_flags) |
paul@484 | 74 | { |
paul@484 | 75 | _endpoints[endpoint].deferred_flags = _deferred_flags; |
paul@484 | 76 | _endpoints[endpoint].deferred_values = _deferred_values; |
paul@484 | 77 | } |
paul@140 | 78 | |
paul@485 | 79 | return _subscribe(endpoint, notifier, flags); |
paul@140 | 80 | } |
paul@140 | 81 | |
paul@140 | 82 | /* Subscribe to a specific endpoint's notifications using a notification |
paul@140 | 83 | object. */ |
paul@133 | 84 | |
paul@288 | 85 | long NotificationSupport::subscribe(unsigned int endpoint, l4_cap_idx_t notifier, |
paul@485 | 86 | notify_flags_t flags) |
paul@133 | 87 | { |
paul@140 | 88 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 89 | |
paul@138 | 90 | if (endpoint >= _endpoints.size()) |
paul@138 | 91 | _endpoints.resize(endpoint + 1); |
paul@138 | 92 | |
paul@400 | 93 | /* Propagate deferred flags for new endpoints. */ |
paul@140 | 94 | |
paul@484 | 95 | if (_deferred_flags) |
paul@484 | 96 | { |
paul@484 | 97 | _endpoints[endpoint].deferred_flags = _deferred_flags; |
paul@484 | 98 | _endpoints[endpoint].deferred_values = _deferred_values; |
paul@484 | 99 | } |
paul@140 | 100 | |
paul@485 | 101 | return _subscribe(endpoint, notifier, flags); |
paul@288 | 102 | } |
paul@288 | 103 | |
paul@288 | 104 | /* Unsubscribe from an endpoint's notifications. */ |
paul@288 | 105 | |
paul@488 | 106 | long NotificationSupport::unsubscribe(l4_cap_idx_t notifier) |
paul@288 | 107 | { |
paul@288 | 108 | std::lock_guard<std::mutex> guard(_lock); |
paul@288 | 109 | |
paul@485 | 110 | _unsubscribe(notifier); |
paul@488 | 111 | return L4_EOK; |
paul@140 | 112 | } |
paul@140 | 113 | |
paul@140 | 114 | /* Common subscription functionality. */ |
paul@140 | 115 | |
paul@288 | 116 | long NotificationSupport::_subscribe(unsigned int endpoint, l4_cap_idx_t notifier, |
paul@485 | 117 | notify_flags_t flags) |
paul@140 | 118 | { |
paul@288 | 119 | /* Record details of the notifier itself. */ |
paul@288 | 120 | |
paul@138 | 121 | NotificationEndpoint &ep = _endpoints[endpoint]; |
paul@138 | 122 | |
paul@138 | 123 | ep.notifiers.insert(notifier); |
paul@138 | 124 | ep.flags = flags; |
paul@133 | 125 | |
paul@133 | 126 | /* Send deferred conditions held from before subscription occurred. */ |
paul@133 | 127 | |
paul@484 | 128 | if (ep.deferred_flags) |
paul@133 | 129 | { |
paul@484 | 130 | _notify(endpoint, ep.deferred_flags, ep.deferred_values); |
paul@484 | 131 | ep.deferred_flags = 0; |
paul@484 | 132 | ep.deferred_values = NOTIFY_VALUES_NULL; |
paul@133 | 133 | } |
paul@288 | 134 | |
paul@485 | 135 | /* Record the notifier's endpoint. */ |
paul@288 | 136 | |
paul@485 | 137 | _subscribers[notifier] = endpoint; |
paul@290 | 138 | return L4_EOK; |
paul@288 | 139 | } |
paul@288 | 140 | |
paul@485 | 141 | void NotificationSupport::_unsubscribe(l4_cap_idx_t notifier) |
paul@288 | 142 | { |
paul@485 | 143 | /* Find and remove the notifier endpoint record. */ |
paul@290 | 144 | |
paul@485 | 145 | unsigned int endpoint; |
paul@485 | 146 | long err = _get_endpoint(notifier, true, &endpoint); |
paul@290 | 147 | |
paul@485 | 148 | if (err) |
paul@290 | 149 | return; |
paul@290 | 150 | |
paul@290 | 151 | /* Release the notifier. */ |
paul@290 | 152 | |
paul@288 | 153 | NotificationEndpoint &ep = _endpoints[endpoint]; |
paul@288 | 154 | NotifierSet::iterator it = ep.notifiers.find(notifier); |
paul@288 | 155 | |
paul@288 | 156 | if (it != ep.notifiers.end()) |
paul@288 | 157 | { |
paul@288 | 158 | ep.notifiers.erase(it); |
paul@288 | 159 | ipc_cap_free_um(notifier); |
paul@288 | 160 | |
paul@288 | 161 | if (ep.notifiers.empty()) |
paul@288 | 162 | { |
paul@288 | 163 | ep.flags = 0; |
paul@484 | 164 | ep.deferred_flags = 0; |
paul@484 | 165 | ep.deferred_values = NOTIFY_VALUES_NULL; |
paul@288 | 166 | } |
paul@288 | 167 | } |
paul@133 | 168 | } |
paul@133 | 169 | |
paul@137 | 170 | /* Notify a particular endpoint. */ |
paul@133 | 171 | |
paul@484 | 172 | void NotificationSupport::notify(unsigned int endpoint, notify_flags_t flags, |
paul@484 | 173 | notify_values_t values) |
paul@133 | 174 | { |
paul@140 | 175 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 176 | |
paul@484 | 177 | _notify(endpoint, flags, values); |
paul@140 | 178 | } |
paul@140 | 179 | |
paul@484 | 180 | void NotificationSupport::_notify(unsigned int endpoint, notify_flags_t flags, |
paul@484 | 181 | notify_values_t values) |
paul@140 | 182 | { |
paul@138 | 183 | if (endpoint >= _endpoints.size()) |
paul@138 | 184 | return; |
paul@138 | 185 | |
paul@138 | 186 | NotificationEndpoint &ep = _endpoints[endpoint]; |
paul@138 | 187 | |
paul@133 | 188 | /* Notify the endpoint or hold any notification for potential future |
paul@133 | 189 | subscription. */ |
paul@133 | 190 | |
paul@138 | 191 | if (!ep.notifiers.empty()) |
paul@133 | 192 | { |
paul@138 | 193 | if (flags & ep.flags) |
paul@133 | 194 | { |
paul@138 | 195 | NotifierSet::iterator it; |
paul@133 | 196 | |
paul@138 | 197 | for (it = ep.notifiers.begin(); it != ep.notifiers.end(); it++) |
paul@133 | 198 | { |
paul@133 | 199 | client_Notifier notifier(*it); |
paul@133 | 200 | |
paul@484 | 201 | notifier.notify(flags & ep.flags, values); |
paul@133 | 202 | } |
paul@133 | 203 | } |
paul@133 | 204 | } |
paul@133 | 205 | else |
paul@484 | 206 | { |
paul@484 | 207 | ep.deferred_flags = flags; |
paul@484 | 208 | ep.deferred_values = values; |
paul@484 | 209 | } |
paul@133 | 210 | } |
paul@133 | 211 | |
paul@211 | 212 | /* Notify all endpoints. */ |
paul@211 | 213 | |
paul@484 | 214 | void NotificationSupport::notify_all(notify_flags_t flags, notify_values_t values) |
paul@211 | 215 | { |
paul@211 | 216 | std::lock_guard<std::mutex> guard(_lock); |
paul@211 | 217 | |
paul@211 | 218 | for (unsigned int i = 0; i < _endpoints.size(); i++) |
paul@484 | 219 | _notify(i, flags, values); |
paul@211 | 220 | |
paul@484 | 221 | /* Accumulate flags, but only record the most recent values. */ |
paul@484 | 222 | |
paul@484 | 223 | _deferred_flags |= flags; |
paul@484 | 224 | _deferred_values = values; |
paul@211 | 225 | } |
paul@211 | 226 | |
paul@137 | 227 | /* Notify the other endpoints. */ |
paul@137 | 228 | |
paul@484 | 229 | void NotificationSupport::notify_others(unsigned int endpoint, notify_flags_t flags, |
paul@484 | 230 | notify_values_t values) |
paul@137 | 231 | { |
paul@140 | 232 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 233 | |
paul@485 | 234 | _notify_others(endpoint, flags, values); |
paul@485 | 235 | } |
paul@485 | 236 | |
paul@485 | 237 | /* Notify the other endpoints given a notifier. */ |
paul@485 | 238 | |
paul@485 | 239 | void NotificationSupport::notify_others(l4_cap_idx_t notifier, notify_flags_t flags, |
paul@485 | 240 | notify_values_t values) |
paul@485 | 241 | { |
paul@485 | 242 | std::lock_guard<std::mutex> guard(_lock); |
paul@485 | 243 | |
paul@485 | 244 | unsigned int endpoint; |
paul@485 | 245 | long err = _get_endpoint(notifier, false, &endpoint); |
paul@485 | 246 | |
paul@485 | 247 | if (err) |
paul@485 | 248 | return; |
paul@485 | 249 | |
paul@485 | 250 | _notify_others(endpoint, flags, values); |
paul@485 | 251 | } |
paul@485 | 252 | |
paul@485 | 253 | void NotificationSupport::_notify_others(unsigned int endpoint, |
paul@485 | 254 | notify_flags_t flags, |
paul@485 | 255 | notify_values_t values) |
paul@485 | 256 | { |
paul@138 | 257 | for (unsigned int i = 0; i < _endpoints.size(); i++) |
paul@137 | 258 | if (i != endpoint) |
paul@484 | 259 | _notify(i, flags, values); |
paul@140 | 260 | |
paul@484 | 261 | /* Accumulate flags, but only record the most recent values. */ |
paul@484 | 262 | |
paul@484 | 263 | _deferred_flags |= flags; |
paul@484 | 264 | _deferred_values = values; |
paul@137 | 265 | } |
paul@137 | 266 | |
paul@133 | 267 | /* Release notifiers for each endpoint. */ |
paul@133 | 268 | |
paul@133 | 269 | void NotificationSupport::release_notifiers() |
paul@133 | 270 | { |
paul@140 | 271 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 272 | |
paul@138 | 273 | for (unsigned int endpoint = 0; endpoint < _endpoints.size(); endpoint++) |
paul@133 | 274 | { |
paul@138 | 275 | NotificationEndpoint &ep = _endpoints[endpoint]; |
paul@138 | 276 | NotifierSet::iterator it; |
paul@133 | 277 | |
paul@138 | 278 | for (it = ep.notifiers.begin(); it != ep.notifiers.end(); it++) |
paul@133 | 279 | ipc_cap_free_um(*it); |
paul@133 | 280 | |
paul@138 | 281 | ep.notifiers.clear(); |
paul@138 | 282 | ep.flags = 0; |
paul@484 | 283 | ep.deferred_flags = 0; |
paul@484 | 284 | ep.deferred_values = NOTIFY_VALUES_NULL; |
paul@133 | 285 | } |
paul@133 | 286 | } |
paul@133 | 287 | |
paul@133 | 288 | // vim: tabstop=4 expandtab shiftwidth=4 |