paul@133 | 1 | /* |
paul@133 | 2 | * Notification support. |
paul@133 | 3 | * |
paul@133 | 4 | * Copyright (C) 2021 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@133 | 26 | |
paul@133 | 27 | |
paul@133 | 28 | |
paul@133 | 29 | /* Initialise endpoints and flags for notifications. */ |
paul@133 | 30 | |
paul@133 | 31 | NotificationSupport::NotificationSupport(unsigned int endpoints) |
paul@138 | 32 | : _min_endpoints(endpoints) |
paul@133 | 33 | { |
paul@138 | 34 | _endpoints.resize(_min_endpoints); |
paul@133 | 35 | } |
paul@133 | 36 | |
paul@140 | 37 | /* Subscribe to notifications using a notification object, returning the |
paul@140 | 38 | endpoint number. */ |
paul@140 | 39 | |
paul@140 | 40 | unsigned int NotificationSupport::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) |
paul@140 | 41 | { |
paul@140 | 42 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 43 | |
paul@140 | 44 | unsigned int endpoint = _endpoints.size(); |
paul@140 | 45 | |
paul@140 | 46 | _endpoints.resize(endpoint + 1); |
paul@140 | 47 | |
paul@140 | 48 | /* Propagate deferred flags for new endpoints. */ |
paul@140 | 49 | |
paul@140 | 50 | if (_deferred) |
paul@140 | 51 | _endpoints[endpoint].deferred = _deferred; |
paul@140 | 52 | |
paul@140 | 53 | _subscribe(endpoint, notifier, flags); |
paul@140 | 54 | |
paul@140 | 55 | return endpoint; |
paul@140 | 56 | } |
paul@140 | 57 | |
paul@140 | 58 | /* Subscribe to a specific endpoint's notifications using a notification |
paul@140 | 59 | object. */ |
paul@133 | 60 | |
paul@133 | 61 | void NotificationSupport::subscribe(unsigned int endpoint, l4_cap_idx_t notifier, notify_flags_t flags) |
paul@133 | 62 | { |
paul@140 | 63 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 64 | |
paul@138 | 65 | if (endpoint >= _endpoints.size()) |
paul@140 | 66 | { |
paul@138 | 67 | _endpoints.resize(endpoint + 1); |
paul@138 | 68 | |
paul@140 | 69 | /* Propagate deferred flags for new endpoints. */ |
paul@140 | 70 | |
paul@140 | 71 | if (_deferred) |
paul@140 | 72 | _endpoints[endpoint].deferred = _deferred; |
paul@140 | 73 | } |
paul@140 | 74 | |
paul@140 | 75 | _subscribe(endpoint, notifier, flags); |
paul@140 | 76 | } |
paul@140 | 77 | |
paul@140 | 78 | /* Common subscription functionality. */ |
paul@140 | 79 | |
paul@140 | 80 | void NotificationSupport::_subscribe(unsigned int endpoint, l4_cap_idx_t notifier, notify_flags_t flags) |
paul@140 | 81 | { |
paul@138 | 82 | NotificationEndpoint &ep = _endpoints[endpoint]; |
paul@138 | 83 | |
paul@138 | 84 | ep.notifiers.insert(notifier); |
paul@138 | 85 | ep.flags = flags; |
paul@133 | 86 | |
paul@133 | 87 | /* Send deferred conditions held from before subscription occurred. */ |
paul@133 | 88 | |
paul@138 | 89 | if (ep.deferred) |
paul@133 | 90 | { |
paul@140 | 91 | _notify(endpoint, ep.deferred); |
paul@138 | 92 | ep.deferred = 0; |
paul@133 | 93 | } |
paul@133 | 94 | } |
paul@133 | 95 | |
paul@133 | 96 | /* Unsubscribe from an endpoint's notifications. */ |
paul@133 | 97 | |
paul@133 | 98 | void NotificationSupport::unsubscribe(unsigned int endpoint, l4_cap_idx_t notifier) |
paul@133 | 99 | { |
paul@140 | 100 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 101 | |
paul@138 | 102 | if (endpoint >= _endpoints.size()) |
paul@138 | 103 | return; |
paul@133 | 104 | |
paul@138 | 105 | NotificationEndpoint &ep = _endpoints[endpoint]; |
paul@138 | 106 | NotifierSet::iterator it = ep.notifiers.find(notifier); |
paul@138 | 107 | |
paul@138 | 108 | if (it != ep.notifiers.end()) |
paul@133 | 109 | { |
paul@138 | 110 | ep.notifiers.erase(it); |
paul@133 | 111 | ipc_cap_free_um(notifier); |
paul@138 | 112 | |
paul@138 | 113 | if (ep.notifiers.empty()) |
paul@138 | 114 | { |
paul@138 | 115 | ep.flags = 0; |
paul@138 | 116 | ep.deferred = 0; |
paul@138 | 117 | } |
paul@133 | 118 | } |
paul@133 | 119 | } |
paul@133 | 120 | |
paul@137 | 121 | /* Notify a particular endpoint. */ |
paul@133 | 122 | |
paul@137 | 123 | void NotificationSupport::notify(unsigned int endpoint, notify_flags_t flags) |
paul@133 | 124 | { |
paul@140 | 125 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 126 | |
paul@140 | 127 | _notify(endpoint, flags); |
paul@140 | 128 | } |
paul@140 | 129 | |
paul@140 | 130 | void NotificationSupport::_notify(unsigned int endpoint, notify_flags_t flags) |
paul@140 | 131 | { |
paul@138 | 132 | if (endpoint >= _endpoints.size()) |
paul@138 | 133 | return; |
paul@138 | 134 | |
paul@138 | 135 | NotificationEndpoint &ep = _endpoints[endpoint]; |
paul@138 | 136 | |
paul@133 | 137 | /* Notify the endpoint or hold any notification for potential future |
paul@133 | 138 | subscription. */ |
paul@133 | 139 | |
paul@138 | 140 | if (!ep.notifiers.empty()) |
paul@133 | 141 | { |
paul@138 | 142 | if (flags & ep.flags) |
paul@133 | 143 | { |
paul@138 | 144 | NotifierSet::iterator it; |
paul@133 | 145 | |
paul@138 | 146 | for (it = ep.notifiers.begin(); it != ep.notifiers.end(); it++) |
paul@133 | 147 | { |
paul@133 | 148 | client_Notifier notifier(*it); |
paul@133 | 149 | |
paul@138 | 150 | notifier.notify(flags & ep.flags); |
paul@133 | 151 | } |
paul@133 | 152 | } |
paul@133 | 153 | } |
paul@133 | 154 | else |
paul@138 | 155 | ep.deferred = flags; |
paul@133 | 156 | } |
paul@133 | 157 | |
paul@137 | 158 | /* Notify the other endpoints. */ |
paul@137 | 159 | |
paul@137 | 160 | void NotificationSupport::notify_others(unsigned int endpoint, notify_flags_t flags) |
paul@137 | 161 | { |
paul@140 | 162 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 163 | |
paul@138 | 164 | for (unsigned int i = 0; i < _endpoints.size(); i++) |
paul@137 | 165 | if (i != endpoint) |
paul@140 | 166 | _notify(i, flags); |
paul@140 | 167 | |
paul@140 | 168 | _deferred |= flags; |
paul@137 | 169 | } |
paul@137 | 170 | |
paul@133 | 171 | /* Release notifiers for each endpoint. */ |
paul@133 | 172 | |
paul@133 | 173 | void NotificationSupport::release_notifiers() |
paul@133 | 174 | { |
paul@140 | 175 | std::lock_guard<std::mutex> guard(_lock); |
paul@140 | 176 | |
paul@138 | 177 | for (unsigned int endpoint = 0; endpoint < _endpoints.size(); endpoint++) |
paul@133 | 178 | { |
paul@138 | 179 | NotificationEndpoint &ep = _endpoints[endpoint]; |
paul@138 | 180 | NotifierSet::iterator it; |
paul@133 | 181 | |
paul@138 | 182 | for (it = ep.notifiers.begin(); it != ep.notifiers.end(); it++) |
paul@133 | 183 | ipc_cap_free_um(*it); |
paul@133 | 184 | |
paul@138 | 185 | ep.notifiers.clear(); |
paul@138 | 186 | ep.flags = 0; |
paul@138 | 187 | ep.deferred = 0; |
paul@133 | 188 | } |
paul@133 | 189 | } |
paul@133 | 190 | |
paul@133 | 191 | // vim: tabstop=4 expandtab shiftwidth=4 |