1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libnotifier/include/notifier/notifier.h Mon Mar 20 22:28:37 2023 +0100
1.3 @@ -0,0 +1,174 @@
1.4 +/*
1.5 + * Generic object event notification support.
1.6 + *
1.7 + * Copyright (C) 2021, 2022, 2023 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#pragma once
1.26 +
1.27 +#include <condition_variable>
1.28 +#include <list>
1.29 +#include <map>
1.30 +#include <mutex>
1.31 +
1.32 +#include <systypes/base.h>
1.33 +
1.34 +
1.35 +
1.36 +/* Object-specific notification details. */
1.37 +
1.38 +class ObjectNotificationState
1.39 +{
1.40 +public:
1.41 + /* Synchronisation primitives for state access and notification. */
1.42 +
1.43 + std::mutex lock;
1.44 + std::condition_variable condition;
1.45 +
1.46 + /* Pending notifications for monitored objects. */
1.47 +
1.48 + notify_flags_t pending_notifications = 0;
1.49 + notify_values_t pending_values = NOTIFY_VALUES_NULL;
1.50 +
1.51 + /* Endpoints associated with monitored objects. */
1.52 +
1.53 + l4_cap_idx_t endpoint = L4_INVALID_CAP;
1.54 +
1.55 + bool is_null() { return l4_is_invalid_cap(endpoint); }
1.56 +};
1.57 +
1.58 +
1.59 +
1.60 +/* Collection types. */
1.61 +
1.62 +typedef std::map<notifiable_t *, ObjectNotificationState> ObjectNotificationStates;
1.63 +typedef std::map<notifiable_t *, std::mutex> ObjectStateLocks;
1.64 +
1.65 +
1.66 +
1.67 +/* An object for monitoring object event notifications. */
1.68 +
1.69 +class ObjectNotifier
1.70 +{
1.71 +protected:
1.72 + /* General state access locking. */
1.73 +
1.74 + std::mutex _state_lock;
1.75 +
1.76 + /* Object-specific state locking. */
1.77 +
1.78 + ObjectStateLocks _object_locks;
1.79 +
1.80 + /* Notification state. */
1.81 +
1.82 + ObjectNotificationStates _state;
1.83 +
1.84 + /* Notifier thread details. */
1.85 +
1.86 + l4_cap_idx_t _thread = L4_INVALID_CAP;
1.87 + bool _started = false;
1.88 +
1.89 + /* Convenience method to access object state. */
1.90 +
1.91 + virtual ObjectNotificationState &object_state(notifiable_t *object, bool create);
1.92 +
1.93 + /* Helper methods. */
1.94 +
1.95 + virtual void _notify(notifiable_t *object, notify_flags_t flags,
1.96 + notify_values_t values) = 0;
1.97 +
1.98 + virtual bool _transfer(ObjectNotificationState &state, notifiable_t *object);
1.99 +
1.100 +public:
1.101 + virtual ~ObjectNotifier();
1.102 +
1.103 + /* Local operations. */
1.104 +
1.105 + virtual long start();
1.106 +
1.107 + virtual long subscribe(notifiable_t *object, notify_flags_t flags);
1.108 +
1.109 + virtual long unsubscribe(notifiable_t *object);
1.110 +
1.111 + virtual long get_endpoint(notifiable_t *object, l4_cap_idx_t *endpoint, bool create);
1.112 +
1.113 + virtual long remove_endpoint(notifiable_t *object, l4_cap_idx_t endpoint);
1.114 +
1.115 + /* Event handling support. */
1.116 +
1.117 + virtual void mainloop();
1.118 +};
1.119 +
1.120 +
1.121 +
1.122 +/* An object monitoring notifications for a collection of different objects. */
1.123 +
1.124 +class GeneralObjectNotifier : public ObjectNotifier
1.125 +{
1.126 +protected:
1.127 + /* Locking to protect pending notification members and to coordinate access
1.128 + to notifications. */
1.129 +
1.130 + std::mutex _general_lock;
1.131 +
1.132 + /* General lock synchronisation. */
1.133 +
1.134 + std::condition_variable _general_condition;
1.135 +
1.136 + /* Objects affected by notifications. */
1.137 +
1.138 + std::list<notifiable_t *> _affected;
1.139 +
1.140 + /* Helper methods. */
1.141 +
1.142 + virtual void _notify(notifiable_t *object, notify_flags_t flags,
1.143 + notify_values_t values);
1.144 +
1.145 + virtual bool _retrieve(notifiable_t **object);
1.146 +
1.147 + virtual bool _retrieve_for_object(notifiable_t *object);
1.148 +
1.149 +public:
1.150 + virtual long wait(notifiable_t **object);
1.151 +};
1.152 +
1.153 +
1.154 +
1.155 +/* An object monitoring notifications for specific objects. */
1.156 +
1.157 +class SpecificObjectNotifier : public ObjectNotifier
1.158 +{
1.159 +protected:
1.160 + /* Helper methods. */
1.161 +
1.162 + virtual void _notify(notifiable_t *object, notify_flags_t flags,
1.163 + notify_values_t values);
1.164 +
1.165 +public:
1.166 + virtual long wait_object(notifiable_t *object);
1.167 +};
1.168 +
1.169 +
1.170 +
1.171 +/* Helper functions. */
1.172 +
1.173 +SpecificObjectNotifier *notifier_get_task_notifier();
1.174 +
1.175 +GeneralObjectNotifier *notifier_get_local_notifier();
1.176 +
1.177 +// vim: tabstop=4 expandtab shiftwidth=4