1.1 --- a/libfsclient/include/fsclient/notifier.h Mon Mar 20 14:52:03 2023 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,175 +0,0 @@
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 <fsclient/file.h>
1.33 -#include <systypes/base.h>
1.34 -
1.35 -
1.36 -
1.37 -/* Object-specific notification details. */
1.38 -
1.39 -class ObjectNotificationState
1.40 -{
1.41 -public:
1.42 - /* Synchronisation primitives for state access and notification. */
1.43 -
1.44 - std::mutex lock;
1.45 - std::condition_variable condition;
1.46 -
1.47 - /* Pending notifications for monitored objects. */
1.48 -
1.49 - notify_flags_t pending_notifications = 0;
1.50 - notify_values_t pending_values = NOTIFY_VALUES_NULL;
1.51 -
1.52 - /* Endpoints associated with monitored objects. */
1.53 -
1.54 - l4_cap_idx_t endpoint = L4_INVALID_CAP;
1.55 -
1.56 - bool is_null() { return l4_is_invalid_cap(endpoint); }
1.57 -};
1.58 -
1.59 -
1.60 -
1.61 -/* Collection types. */
1.62 -
1.63 -typedef std::map<notifiable_t *, ObjectNotificationState> ObjectNotificationStates;
1.64 -typedef std::map<notifiable_t *, std::mutex> ObjectStateLocks;
1.65 -
1.66 -
1.67 -
1.68 -/* An object for monitoring object event notifications. */
1.69 -
1.70 -class ObjectNotifier
1.71 -{
1.72 -protected:
1.73 - /* General state access locking. */
1.74 -
1.75 - std::mutex _state_lock;
1.76 -
1.77 - /* Object-specific state locking. */
1.78 -
1.79 - ObjectStateLocks _object_locks;
1.80 -
1.81 - /* Notification state. */
1.82 -
1.83 - ObjectNotificationStates _state;
1.84 -
1.85 - /* Notifier thread details. */
1.86 -
1.87 - l4_cap_idx_t _thread = L4_INVALID_CAP;
1.88 - bool _started = false;
1.89 -
1.90 - /* Convenience method to access object state. */
1.91 -
1.92 - virtual ObjectNotificationState &object_state(notifiable_t *object, bool create);
1.93 -
1.94 - /* Helper methods. */
1.95 -
1.96 - virtual void _notify(notifiable_t *object, notify_flags_t flags,
1.97 - notify_values_t values) = 0;
1.98 -
1.99 - virtual bool _transfer(ObjectNotificationState &state, notifiable_t *object);
1.100 -
1.101 -public:
1.102 - virtual ~ObjectNotifier();
1.103 -
1.104 - /* Local operations. */
1.105 -
1.106 - virtual long start();
1.107 -
1.108 - virtual long subscribe(notifiable_t *object, notify_flags_t flags);
1.109 -
1.110 - virtual long unsubscribe(notifiable_t *object);
1.111 -
1.112 - virtual long get_endpoint(notifiable_t *object, l4_cap_idx_t *endpoint, bool create);
1.113 -
1.114 - virtual long remove_endpoint(notifiable_t *object, l4_cap_idx_t endpoint);
1.115 -
1.116 - /* Event handling support. */
1.117 -
1.118 - virtual void mainloop();
1.119 -};
1.120 -
1.121 -
1.122 -
1.123 -/* An object monitoring notifications for a collection of different objects. */
1.124 -
1.125 -class GeneralObjectNotifier : public ObjectNotifier
1.126 -{
1.127 -protected:
1.128 - /* Locking to protect pending notification members and to coordinate access
1.129 - to notifications. */
1.130 -
1.131 - std::mutex _general_lock;
1.132 -
1.133 - /* General lock synchronisation. */
1.134 -
1.135 - std::condition_variable _general_condition;
1.136 -
1.137 - /* Objects affected by notifications. */
1.138 -
1.139 - std::list<notifiable_t *> _affected;
1.140 -
1.141 - /* Helper methods. */
1.142 -
1.143 - virtual void _notify(notifiable_t *object, notify_flags_t flags,
1.144 - notify_values_t values);
1.145 -
1.146 - virtual bool _retrieve(notifiable_t **object);
1.147 -
1.148 - virtual bool _retrieve_for_object(notifiable_t *object);
1.149 -
1.150 -public:
1.151 - virtual long wait(notifiable_t **object);
1.152 -};
1.153 -
1.154 -
1.155 -
1.156 -/* An object monitoring notifications for specific objects. */
1.157 -
1.158 -class SpecificObjectNotifier : public ObjectNotifier
1.159 -{
1.160 -protected:
1.161 - /* Helper methods. */
1.162 -
1.163 - virtual void _notify(notifiable_t *object, notify_flags_t flags,
1.164 - notify_values_t values);
1.165 -
1.166 -public:
1.167 - virtual long wait_object(notifiable_t *object);
1.168 -};
1.169 -
1.170 -
1.171 -
1.172 -/* Helper functions. */
1.173 -
1.174 -SpecificObjectNotifier *notifier_get_task_notifier();
1.175 -
1.176 -GeneralObjectNotifier *notifier_get_local_notifier();
1.177 -
1.178 -// vim: tabstop=4 expandtab shiftwidth=4