paul@133 | 1 | /* |
paul@133 | 2 | * Notification support. |
paul@133 | 3 | * |
paul@285 | 4 | * Copyright (C) 2021, 2022 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 | #include "notifier_peer_endpoint.h" |
paul@288 | 27 | |
paul@288 | 28 | #include "resource_server.h" |
paul@133 | 29 | |
paul@133 | 30 | |
paul@133 | 31 | |
paul@133 | 32 | /* Initialise endpoints and flags for notifications. */ |
paul@133 | 33 | |
paul@133 | 34 | NotificationSupport::NotificationSupport(unsigned int endpoints) |
paul@138 | 35 | : _min_endpoints(endpoints) |
paul@133 | 36 | { |
paul@138 | 37 | _endpoints.resize(_min_endpoints); |
paul@133 | 38 | } |
paul@133 | 39 | |
paul@193 | 40 | NotificationSupport::~NotificationSupport() |
paul@193 | 41 | { |
paul@193 | 42 | } |
paul@193 | 43 | |
paul@140 | 44 | /* Subscribe to notifications using a notification object, returning the |
paul@140 | 45 | endpoint number. */ |
paul@140 | 46 | |
paul@288 | 47 | long NotificationSupport::subscribe(l4_cap_idx_t notifier, notify_flags_t flags, |
paul@288 | 48 | l4_cap_idx_t *peer, unsigned int *endpoint_number) |
paul@140 | 49 | { |
paul@140 | 50 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 51 | |
paul@140 | 52 | unsigned int endpoint = _endpoints.size(); |
paul@140 | 53 | _endpoints.resize(endpoint + 1); |
paul@140 | 54 | |
paul@140 | 55 | /* Propagate deferred flags for new endpoints. */ |
paul@140 | 56 | |
paul@140 | 57 | if (_deferred) |
paul@140 | 58 | _endpoints[endpoint].deferred = _deferred; |
paul@140 | 59 | |
paul@288 | 60 | *endpoint_number = endpoint; |
paul@288 | 61 | return _subscribe(endpoint, notifier, flags, peer); |
paul@140 | 62 | } |
paul@140 | 63 | |
paul@140 | 64 | /* Subscribe to a specific endpoint's notifications using a notification |
paul@140 | 65 | object. */ |
paul@133 | 66 | |
paul@288 | 67 | long NotificationSupport::subscribe(unsigned int endpoint, l4_cap_idx_t notifier, |
paul@288 | 68 | notify_flags_t flags, l4_cap_idx_t *peer) |
paul@133 | 69 | { |
paul@140 | 70 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 71 | |
paul@138 | 72 | if (endpoint >= _endpoints.size()) |
paul@140 | 73 | { |
paul@138 | 74 | _endpoints.resize(endpoint + 1); |
paul@138 | 75 | |
paul@140 | 76 | /* Propagate deferred flags for new endpoints. */ |
paul@140 | 77 | |
paul@140 | 78 | if (_deferred) |
paul@140 | 79 | _endpoints[endpoint].deferred = _deferred; |
paul@140 | 80 | } |
paul@140 | 81 | |
paul@288 | 82 | return _subscribe(endpoint, notifier, flags, peer); |
paul@288 | 83 | } |
paul@288 | 84 | |
paul@288 | 85 | /* Unsubscribe from an endpoint's notifications. */ |
paul@288 | 86 | |
paul@288 | 87 | void NotificationSupport::unsubscribe(unsigned int endpoint, l4_cap_idx_t notifier) |
paul@288 | 88 | { |
paul@288 | 89 | std::lock_guard<std::mutex> guard(_lock); |
paul@288 | 90 | |
paul@288 | 91 | _unsubscribe(endpoint, notifier); |
paul@140 | 92 | } |
paul@140 | 93 | |
paul@140 | 94 | /* Common subscription functionality. */ |
paul@140 | 95 | |
paul@288 | 96 | long NotificationSupport::_subscribe(unsigned int endpoint, l4_cap_idx_t notifier, |
paul@288 | 97 | notify_flags_t flags, l4_cap_idx_t *peer) |
paul@140 | 98 | { |
paul@288 | 99 | /* Record details of the notifier itself. */ |
paul@288 | 100 | |
paul@138 | 101 | NotificationEndpoint &ep = _endpoints[endpoint]; |
paul@138 | 102 | |
paul@138 | 103 | ep.notifiers.insert(notifier); |
paul@138 | 104 | ep.flags = flags; |
paul@133 | 105 | |
paul@133 | 106 | /* Send deferred conditions held from before subscription occurred. */ |
paul@133 | 107 | |
paul@138 | 108 | if (ep.deferred) |
paul@133 | 109 | { |
paul@140 | 110 | _notify(endpoint, ep.deferred); |
paul@138 | 111 | ep.deferred = 0; |
paul@133 | 112 | } |
paul@288 | 113 | |
paul@288 | 114 | /* Create a notifier peer for unsubscribing. */ |
paul@288 | 115 | |
paul@288 | 116 | NotifierPeerEndpoint *peer_ep = new NotifierPeerEndpoint(this, endpoint, notifier); |
paul@288 | 117 | |
paul@288 | 118 | _peers[notifier] = peer_ep; |
paul@288 | 119 | |
paul@288 | 120 | long err = ResourceServer(peer_ep).start_thread(peer); |
paul@288 | 121 | |
paul@288 | 122 | if (err) |
paul@288 | 123 | _unsubscribe(endpoint, notifier); |
paul@288 | 124 | |
paul@288 | 125 | return err; |
paul@288 | 126 | } |
paul@288 | 127 | |
paul@288 | 128 | void NotificationSupport::_unsubscribe(unsigned int endpoint, l4_cap_idx_t notifier) |
paul@288 | 129 | { |
paul@288 | 130 | if (endpoint >= _endpoints.size()) |
paul@288 | 131 | return; |
paul@288 | 132 | |
paul@288 | 133 | NotificationEndpoint &ep = _endpoints[endpoint]; |
paul@288 | 134 | NotifierSet::iterator it = ep.notifiers.find(notifier); |
paul@288 | 135 | |
paul@288 | 136 | if (it != ep.notifiers.end()) |
paul@288 | 137 | { |
paul@288 | 138 | ep.notifiers.erase(it); |
paul@288 | 139 | ipc_cap_free_um(notifier); |
paul@288 | 140 | |
paul@288 | 141 | if (ep.notifiers.empty()) |
paul@288 | 142 | { |
paul@288 | 143 | ep.flags = 0; |
paul@288 | 144 | ep.deferred = 0; |
paul@288 | 145 | } |
paul@288 | 146 | } |
paul@288 | 147 | |
paul@288 | 148 | /* Remove the notifier peer. */ |
paul@288 | 149 | |
paul@288 | 150 | NotifierPeerMap::iterator itp = _peers.find(notifier); |
paul@288 | 151 | |
paul@288 | 152 | if (itp != _peers.end()) |
paul@288 | 153 | _peers.erase(itp); |
paul@133 | 154 | } |
paul@133 | 155 | |
paul@137 | 156 | /* Notify a particular endpoint. */ |
paul@133 | 157 | |
paul@137 | 158 | void NotificationSupport::notify(unsigned int endpoint, notify_flags_t flags) |
paul@133 | 159 | { |
paul@140 | 160 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 161 | |
paul@140 | 162 | _notify(endpoint, flags); |
paul@140 | 163 | } |
paul@140 | 164 | |
paul@140 | 165 | void NotificationSupport::_notify(unsigned int endpoint, notify_flags_t flags) |
paul@140 | 166 | { |
paul@138 | 167 | if (endpoint >= _endpoints.size()) |
paul@138 | 168 | return; |
paul@138 | 169 | |
paul@138 | 170 | NotificationEndpoint &ep = _endpoints[endpoint]; |
paul@138 | 171 | |
paul@133 | 172 | /* Notify the endpoint or hold any notification for potential future |
paul@133 | 173 | subscription. */ |
paul@133 | 174 | |
paul@138 | 175 | if (!ep.notifiers.empty()) |
paul@133 | 176 | { |
paul@138 | 177 | if (flags & ep.flags) |
paul@133 | 178 | { |
paul@138 | 179 | NotifierSet::iterator it; |
paul@133 | 180 | |
paul@138 | 181 | for (it = ep.notifiers.begin(); it != ep.notifiers.end(); it++) |
paul@133 | 182 | { |
paul@133 | 183 | client_Notifier notifier(*it); |
paul@133 | 184 | |
paul@138 | 185 | notifier.notify(flags & ep.flags); |
paul@133 | 186 | } |
paul@133 | 187 | } |
paul@133 | 188 | } |
paul@133 | 189 | else |
paul@138 | 190 | ep.deferred = flags; |
paul@133 | 191 | } |
paul@133 | 192 | |
paul@211 | 193 | /* Notify all endpoints. */ |
paul@211 | 194 | |
paul@211 | 195 | void NotificationSupport::notify_all(notify_flags_t flags) |
paul@211 | 196 | { |
paul@211 | 197 | std::lock_guard<std::mutex> guard(_lock); |
paul@211 | 198 | |
paul@211 | 199 | for (unsigned int i = 0; i < _endpoints.size(); i++) |
paul@211 | 200 | _notify(i, flags); |
paul@211 | 201 | |
paul@211 | 202 | _deferred |= flags; |
paul@211 | 203 | } |
paul@211 | 204 | |
paul@137 | 205 | /* Notify the other endpoints. */ |
paul@137 | 206 | |
paul@137 | 207 | void NotificationSupport::notify_others(unsigned int endpoint, notify_flags_t flags) |
paul@137 | 208 | { |
paul@140 | 209 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 210 | |
paul@138 | 211 | for (unsigned int i = 0; i < _endpoints.size(); i++) |
paul@137 | 212 | if (i != endpoint) |
paul@140 | 213 | _notify(i, flags); |
paul@140 | 214 | |
paul@140 | 215 | _deferred |= flags; |
paul@137 | 216 | } |
paul@137 | 217 | |
paul@133 | 218 | /* Release notifiers for each endpoint. */ |
paul@133 | 219 | |
paul@133 | 220 | void NotificationSupport::release_notifiers() |
paul@133 | 221 | { |
paul@140 | 222 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 223 | |
paul@138 | 224 | for (unsigned int endpoint = 0; endpoint < _endpoints.size(); endpoint++) |
paul@133 | 225 | { |
paul@138 | 226 | NotificationEndpoint &ep = _endpoints[endpoint]; |
paul@138 | 227 | NotifierSet::iterator it; |
paul@133 | 228 | |
paul@138 | 229 | for (it = ep.notifiers.begin(); it != ep.notifiers.end(); it++) |
paul@133 | 230 | ipc_cap_free_um(*it); |
paul@133 | 231 | |
paul@138 | 232 | ep.notifiers.clear(); |
paul@138 | 233 | ep.flags = 0; |
paul@138 | 234 | ep.deferred = 0; |
paul@133 | 235 | } |
paul@133 | 236 | } |
paul@133 | 237 | |
paul@133 | 238 | // vim: tabstop=4 expandtab shiftwidth=4 |