paul@124 | 1 | /* |
paul@479 | 2 | * Generic object event notification support. |
paul@124 | 3 | * |
paul@479 | 4 | * Copyright (C) 2021, 2022, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@124 | 5 | * |
paul@124 | 6 | * This program is free software; you can redistribute it and/or |
paul@124 | 7 | * modify it under the terms of the GNU General Public License as |
paul@124 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@124 | 9 | * the License, or (at your option) any later version. |
paul@124 | 10 | * |
paul@124 | 11 | * This program is distributed in the hope that it will be useful, |
paul@124 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@124 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@124 | 14 | * GNU General Public License for more details. |
paul@124 | 15 | * |
paul@124 | 16 | * You should have received a copy of the GNU General Public License |
paul@124 | 17 | * along with this program; if not, write to the Free Software |
paul@124 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@124 | 19 | * Boston, MA 02110-1301, USA |
paul@124 | 20 | */ |
paul@124 | 21 | |
paul@124 | 22 | #pragma once |
paul@124 | 23 | |
paul@124 | 24 | #include <condition_variable> |
paul@124 | 25 | #include <list> |
paul@124 | 26 | #include <mutex> |
paul@555 | 27 | #include <set> |
paul@537 | 28 | #include <vector> |
paul@124 | 29 | |
paul@535 | 30 | #include <resource/resource.h> |
paul@124 | 31 | #include <systypes/base.h> |
paul@124 | 32 | |
paul@534 | 33 | #include "notifier_interface.h" |
paul@534 | 34 | |
paul@124 | 35 | |
paul@124 | 36 | |
paul@125 | 37 | /* Collection types. */ |
paul@125 | 38 | |
paul@555 | 39 | typedef std::list<notifiable_t *> NotifiableObjectQueue; |
paul@555 | 40 | typedef std::set<notifiable_t *> NotifiableObjects; |
paul@537 | 41 | |
paul@125 | 42 | |
paul@125 | 43 | |
paul@479 | 44 | /* An object for monitoring object event notifications. */ |
paul@124 | 45 | |
paul@479 | 46 | class ObjectNotifier |
paul@124 | 47 | { |
paul@124 | 48 | protected: |
paul@555 | 49 | /* Locking for the affected queue and monitored object registry. */ |
paul@177 | 50 | |
paul@555 | 51 | std::mutex _affected_lock, _monitored_lock; |
paul@555 | 52 | std::condition_variable _condition; |
paul@177 | 53 | |
paul@555 | 54 | /* Objects affected by notifications. */ |
paul@124 | 55 | |
paul@555 | 56 | NotifiableObjectQueue _queued; |
paul@555 | 57 | NotifiableObjects _affected, _monitored; |
paul@177 | 58 | |
paul@124 | 59 | public: |
paul@479 | 60 | virtual ~ObjectNotifier(); |
paul@180 | 61 | |
paul@124 | 62 | /* Local operations. */ |
paul@124 | 63 | |
paul@479 | 64 | virtual long subscribe(notifiable_t *object, notify_flags_t flags); |
paul@124 | 65 | |
paul@479 | 66 | virtual long unsubscribe(notifiable_t *object); |
paul@124 | 67 | |
paul@557 | 68 | virtual void update(notifiable_t *object); |
paul@557 | 69 | |
paul@555 | 70 | virtual long wait(notifiable_t **object); |
paul@555 | 71 | |
paul@555 | 72 | virtual long wait_object(notifiable_t *object); |
paul@483 | 73 | |
paul@534 | 74 | /* Helper methods. */ |
paul@124 | 75 | |
paul@555 | 76 | virtual void notify(notifiable_t *object); |
paul@555 | 77 | |
paul@555 | 78 | virtual void release(notifiable_t *object); |
paul@124 | 79 | }; |
paul@124 | 80 | |
paul@124 | 81 | |
paul@124 | 82 | |
paul@555 | 83 | /* Collection types. */ |
paul@177 | 84 | |
paul@555 | 85 | typedef std::set<ObjectNotifier *> ObjectNotifiers; |
paul@534 | 86 | |
paul@534 | 87 | |
paul@534 | 88 | |
paul@534 | 89 | /* Object-specific notifier resource. */ |
paul@534 | 90 | |
paul@534 | 91 | class NotifierResource : public Notifier, public Resource |
paul@534 | 92 | { |
paul@534 | 93 | protected: |
paul@555 | 94 | /* Locking for the resource. */ |
paul@555 | 95 | |
paul@555 | 96 | std::mutex _lock; |
paul@555 | 97 | std::condition_variable _condition; |
paul@555 | 98 | |
paul@555 | 99 | /* Notifiers and the monitored object. */ |
paul@555 | 100 | |
paul@555 | 101 | ObjectNotifiers _notifiers; |
paul@534 | 102 | notifiable_t *_object; |
paul@177 | 103 | |
paul@555 | 104 | /* Utility methods. */ |
paul@555 | 105 | |
paul@555 | 106 | virtual void _notify(); |
paul@555 | 107 | |
paul@555 | 108 | virtual void _release(); |
paul@555 | 109 | |
paul@177 | 110 | public: |
paul@555 | 111 | l4_cap_idx_t endpoint = L4_INVALID_CAP; |
paul@555 | 112 | |
paul@555 | 113 | explicit NotifierResource(notifiable_t *object) |
paul@555 | 114 | : _object(object) |
paul@534 | 115 | { |
paul@534 | 116 | } |
paul@534 | 117 | |
paul@555 | 118 | virtual ~NotifierResource(); |
paul@555 | 119 | |
paul@555 | 120 | /* Resource methods. */ |
paul@555 | 121 | |
paul@555 | 122 | virtual void close(); |
paul@534 | 123 | |
paul@534 | 124 | /* Server details. */ |
paul@534 | 125 | |
paul@534 | 126 | virtual ipc_server_default_config_type config(); |
paul@534 | 127 | |
paul@534 | 128 | virtual void *interface() |
paul@534 | 129 | { return static_cast<Notifier *>(this); } |
paul@534 | 130 | |
paul@534 | 131 | /* Notifier methods. */ |
paul@534 | 132 | |
paul@534 | 133 | virtual long notify(notify_flags_t flags, notify_values_t values); |
paul@555 | 134 | |
paul@555 | 135 | /* Local operations. */ |
paul@555 | 136 | |
paul@555 | 137 | virtual long add(ObjectNotifier *notifier, notify_flags_t flags); |
paul@555 | 138 | |
paul@555 | 139 | virtual long remove(ObjectNotifier *notifier); |
paul@177 | 140 | }; |
paul@177 | 141 | |
paul@177 | 142 | |
paul@177 | 143 | |
paul@124 | 144 | /* Helper functions. */ |
paul@124 | 145 | |
paul@555 | 146 | ObjectNotifier *notifier_get_task_notifier(); |
paul@177 | 147 | |
paul@555 | 148 | ObjectNotifier *notifier_get_local_notifier(); |
paul@124 | 149 | |
paul@124 | 150 | // vim: tabstop=4 expandtab shiftwidth=4 |