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