paul@133 | 1 | /* |
paul@133 | 2 | * Notification support. |
paul@133 | 3 | * |
paul@288 | 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 | #pragma once |
paul@133 | 23 | |
paul@288 | 24 | #include <map> |
paul@140 | 25 | #include <mutex> |
paul@133 | 26 | #include <set> |
paul@138 | 27 | #include <vector> |
paul@133 | 28 | |
paul@133 | 29 | #include <systypes/base.h> |
paul@133 | 30 | |
paul@133 | 31 | |
paul@133 | 32 | |
paul@288 | 33 | /* Forward declaration. */ |
paul@288 | 34 | |
paul@288 | 35 | class NotifierPeerEndpoint; |
paul@288 | 36 | |
paul@138 | 37 | /* Collection data types. */ |
paul@138 | 38 | |
paul@138 | 39 | typedef std::set<l4_cap_idx_t> NotifierSet; |
paul@290 | 40 | typedef std::map<void *, l4_cap_idx_t> SubscriberNotifierMap; |
paul@138 | 41 | |
paul@138 | 42 | /* Notification endpoint details. */ |
paul@138 | 43 | |
paul@138 | 44 | class NotificationEndpoint |
paul@138 | 45 | { |
paul@138 | 46 | public: |
paul@138 | 47 | NotifierSet notifiers; |
paul@140 | 48 | notify_flags_t flags = 0, deferred = 0; |
paul@138 | 49 | }; |
paul@138 | 50 | |
paul@138 | 51 | typedef std::vector<NotificationEndpoint> NotificationEndpointList; |
paul@138 | 52 | |
paul@138 | 53 | |
paul@138 | 54 | |
paul@133 | 55 | /* Notification support, allowing pipe or file users to receive events due to |
paul@133 | 56 | activity on those objects. */ |
paul@133 | 57 | |
paul@133 | 58 | class NotificationSupport |
paul@133 | 59 | { |
paul@133 | 60 | protected: |
paul@140 | 61 | std::mutex _lock; |
paul@140 | 62 | |
paul@133 | 63 | /* Endpoint count. */ |
paul@133 | 64 | |
paul@138 | 65 | unsigned int _min_endpoints; |
paul@133 | 66 | |
paul@138 | 67 | /* Notification endpoint details. */ |
paul@133 | 68 | |
paul@138 | 69 | NotificationEndpointList _endpoints; |
paul@133 | 70 | |
paul@290 | 71 | /* Notifier subscriber details. */ |
paul@288 | 72 | |
paul@290 | 73 | SubscriberNotifierMap _subscribers; |
paul@288 | 74 | |
paul@140 | 75 | /* Deferred notifications for new endpoints. */ |
paul@140 | 76 | |
paul@140 | 77 | notify_flags_t _deferred = 0; |
paul@140 | 78 | |
paul@133 | 79 | virtual void release_notifiers(); |
paul@133 | 80 | |
paul@140 | 81 | virtual void _notify(unsigned int endpoint, notify_flags_t flags); |
paul@140 | 82 | |
paul@288 | 83 | virtual long _subscribe(unsigned int endpoint, l4_cap_idx_t notifier, |
paul@290 | 84 | notify_flags_t flags, void *subscriber); |
paul@288 | 85 | |
paul@290 | 86 | virtual void _unsubscribe(unsigned int endpoint, void *subscriber); |
paul@140 | 87 | |
paul@133 | 88 | public: |
paul@138 | 89 | explicit NotificationSupport(unsigned int endpoints=0); |
paul@133 | 90 | |
paul@193 | 91 | virtual ~NotificationSupport(); |
paul@193 | 92 | |
paul@137 | 93 | virtual void notify(unsigned int endpoint, notify_flags_t flags); |
paul@137 | 94 | |
paul@211 | 95 | virtual void notify_all(notify_flags_t flags); |
paul@211 | 96 | |
paul@137 | 97 | virtual void notify_others(unsigned int endpoint, notify_flags_t flags); |
paul@133 | 98 | |
paul@288 | 99 | virtual long subscribe(l4_cap_idx_t notifier, notify_flags_t flags, |
paul@290 | 100 | void *subscriber, unsigned int *endpoint_number); |
paul@140 | 101 | |
paul@288 | 102 | virtual long subscribe(unsigned int endpoint, l4_cap_idx_t notifier, |
paul@290 | 103 | notify_flags_t flags, void *subscriber); |
paul@288 | 104 | |
paul@290 | 105 | virtual void unsubscribe(unsigned int endpoint, void *subscriber); |
paul@133 | 106 | }; |
paul@133 | 107 | |
paul@133 | 108 | // vim: tabstop=4 expandtab shiftwidth=4 |