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@133 | 32 | : _max_endpoints(endpoints) |
paul@133 | 33 | { |
paul@133 | 34 | _notifiers = new std::set<l4_cap_idx_t>[endpoints]; |
paul@133 | 35 | _flags = new notify_flags_t[endpoints]; |
paul@133 | 36 | _deferred = new notify_flags_t[endpoints]; |
paul@133 | 37 | |
paul@133 | 38 | for (unsigned int endpoint = 0; endpoint < _max_endpoints; endpoint++) |
paul@133 | 39 | { |
paul@133 | 40 | _flags[endpoint] = 0; |
paul@133 | 41 | _deferred[endpoint] = 0; |
paul@133 | 42 | } |
paul@133 | 43 | } |
paul@133 | 44 | |
paul@133 | 45 | NotificationSupport::~NotificationSupport() |
paul@133 | 46 | { |
paul@133 | 47 | delete _notifiers; |
paul@133 | 48 | delete _flags; |
paul@133 | 49 | delete _deferred; |
paul@133 | 50 | } |
paul@133 | 51 | |
paul@133 | 52 | /* Subscribe to an endpoint's notifications using a notification endpoint. */ |
paul@133 | 53 | |
paul@133 | 54 | void NotificationSupport::subscribe(unsigned int endpoint, l4_cap_idx_t notifier, notify_flags_t flags) |
paul@133 | 55 | { |
paul@133 | 56 | _notifiers[endpoint].insert(notifier); |
paul@133 | 57 | _flags[endpoint] = flags; |
paul@133 | 58 | |
paul@133 | 59 | /* Send deferred conditions held from before subscription occurred. */ |
paul@133 | 60 | |
paul@133 | 61 | if (_deferred[endpoint]) |
paul@133 | 62 | { |
paul@133 | 63 | notify(endpoint, _deferred[endpoint]); |
paul@133 | 64 | _deferred[endpoint] = 0; |
paul@133 | 65 | } |
paul@133 | 66 | } |
paul@133 | 67 | |
paul@133 | 68 | /* Unsubscribe from an endpoint's notifications. */ |
paul@133 | 69 | |
paul@133 | 70 | void NotificationSupport::unsubscribe(unsigned int endpoint, l4_cap_idx_t notifier) |
paul@133 | 71 | { |
paul@133 | 72 | std::set<l4_cap_idx_t>::iterator it = _notifiers[endpoint].find(notifier); |
paul@133 | 73 | |
paul@133 | 74 | if (it != _notifiers[endpoint].end()) |
paul@133 | 75 | { |
paul@133 | 76 | _notifiers[endpoint].erase(it); |
paul@133 | 77 | _flags[endpoint] = 0; |
paul@133 | 78 | _deferred[endpoint] = 0; |
paul@133 | 79 | ipc_cap_free_um(notifier); |
paul@133 | 80 | } |
paul@133 | 81 | } |
paul@133 | 82 | |
paul@133 | 83 | /* Notify the other endpoint. */ |
paul@133 | 84 | |
paul@133 | 85 | void NotificationSupport::notify(unsigned int endpoint, notify_flags_t flags, l4_cap_idx_t source) |
paul@133 | 86 | { |
paul@133 | 87 | /* Notify the endpoint or hold any notification for potential future |
paul@133 | 88 | subscription. */ |
paul@133 | 89 | |
paul@133 | 90 | if (!_notifiers[endpoint].empty()) |
paul@133 | 91 | { |
paul@133 | 92 | if (flags & _flags[endpoint]) |
paul@133 | 93 | { |
paul@133 | 94 | std::set<l4_cap_idx_t>::iterator it; |
paul@133 | 95 | |
paul@133 | 96 | for (it = _notifiers[endpoint].begin(); it != _notifiers[endpoint].end(); it++) |
paul@133 | 97 | { |
paul@133 | 98 | /* Avoid notifying the source of the notification if it is |
paul@133 | 99 | present in the list. */ |
paul@133 | 100 | |
paul@133 | 101 | if (l4_is_valid_cap(source) && (*it == source)) |
paul@133 | 102 | continue; |
paul@133 | 103 | |
paul@133 | 104 | client_Notifier notifier(*it); |
paul@133 | 105 | |
paul@133 | 106 | notifier.notify(flags & _flags[endpoint]); |
paul@133 | 107 | } |
paul@133 | 108 | } |
paul@133 | 109 | } |
paul@133 | 110 | else |
paul@133 | 111 | _deferred[endpoint] = flags; |
paul@133 | 112 | } |
paul@133 | 113 | |
paul@133 | 114 | /* Release notifiers for each endpoint. */ |
paul@133 | 115 | |
paul@133 | 116 | void NotificationSupport::release_notifiers() |
paul@133 | 117 | { |
paul@133 | 118 | for (unsigned int endpoint = 0; endpoint < _max_endpoints; endpoint++) |
paul@133 | 119 | { |
paul@133 | 120 | std::set<l4_cap_idx_t>::iterator it; |
paul@133 | 121 | |
paul@133 | 122 | for (it = _notifiers[endpoint].begin(); it != _notifiers[endpoint].end(); it++) |
paul@133 | 123 | ipc_cap_free_um(*it); |
paul@133 | 124 | |
paul@133 | 125 | _notifiers[endpoint].clear(); |
paul@133 | 126 | } |
paul@133 | 127 | } |
paul@133 | 128 | |
paul@133 | 129 | // vim: tabstop=4 expandtab shiftwidth=4 |