1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libfsserver/lib/generic/notification.cc Sun Jul 11 23:21:08 2021 +0200
1.3 @@ -0,0 +1,129 @@
1.4 +/*
1.5 + * Notification support.
1.6 + *
1.7 + * Copyright (C) 2021 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 +#include <ipc/cap_alloc.h>
1.26 +
1.27 +#include "notification.h"
1.28 +#include "notifier_client.h"
1.29 +
1.30 +
1.31 +
1.32 +/* Initialise endpoints and flags for notifications. */
1.33 +
1.34 +NotificationSupport::NotificationSupport(unsigned int endpoints)
1.35 +: _max_endpoints(endpoints)
1.36 +{
1.37 + _notifiers = new std::set<l4_cap_idx_t>[endpoints];
1.38 + _flags = new notify_flags_t[endpoints];
1.39 + _deferred = new notify_flags_t[endpoints];
1.40 +
1.41 + for (unsigned int endpoint = 0; endpoint < _max_endpoints; endpoint++)
1.42 + {
1.43 + _flags[endpoint] = 0;
1.44 + _deferred[endpoint] = 0;
1.45 + }
1.46 +}
1.47 +
1.48 +NotificationSupport::~NotificationSupport()
1.49 +{
1.50 + delete _notifiers;
1.51 + delete _flags;
1.52 + delete _deferred;
1.53 +}
1.54 +
1.55 +/* Subscribe to an endpoint's notifications using a notification endpoint. */
1.56 +
1.57 +void NotificationSupport::subscribe(unsigned int endpoint, l4_cap_idx_t notifier, notify_flags_t flags)
1.58 +{
1.59 + _notifiers[endpoint].insert(notifier);
1.60 + _flags[endpoint] = flags;
1.61 +
1.62 + /* Send deferred conditions held from before subscription occurred. */
1.63 +
1.64 + if (_deferred[endpoint])
1.65 + {
1.66 + notify(endpoint, _deferred[endpoint]);
1.67 + _deferred[endpoint] = 0;
1.68 + }
1.69 +}
1.70 +
1.71 +/* Unsubscribe from an endpoint's notifications. */
1.72 +
1.73 +void NotificationSupport::unsubscribe(unsigned int endpoint, l4_cap_idx_t notifier)
1.74 +{
1.75 + std::set<l4_cap_idx_t>::iterator it = _notifiers[endpoint].find(notifier);
1.76 +
1.77 + if (it != _notifiers[endpoint].end())
1.78 + {
1.79 + _notifiers[endpoint].erase(it);
1.80 + _flags[endpoint] = 0;
1.81 + _deferred[endpoint] = 0;
1.82 + ipc_cap_free_um(notifier);
1.83 + }
1.84 +}
1.85 +
1.86 +/* Notify the other endpoint. */
1.87 +
1.88 +void NotificationSupport::notify(unsigned int endpoint, notify_flags_t flags, l4_cap_idx_t source)
1.89 +{
1.90 + /* Notify the endpoint or hold any notification for potential future
1.91 + subscription. */
1.92 +
1.93 + if (!_notifiers[endpoint].empty())
1.94 + {
1.95 + if (flags & _flags[endpoint])
1.96 + {
1.97 + std::set<l4_cap_idx_t>::iterator it;
1.98 +
1.99 + for (it = _notifiers[endpoint].begin(); it != _notifiers[endpoint].end(); it++)
1.100 + {
1.101 + /* Avoid notifying the source of the notification if it is
1.102 + present in the list. */
1.103 +
1.104 + if (l4_is_valid_cap(source) && (*it == source))
1.105 + continue;
1.106 +
1.107 + client_Notifier notifier(*it);
1.108 +
1.109 + notifier.notify(flags & _flags[endpoint]);
1.110 + }
1.111 + }
1.112 + }
1.113 + else
1.114 + _deferred[endpoint] = flags;
1.115 +}
1.116 +
1.117 +/* Release notifiers for each endpoint. */
1.118 +
1.119 +void NotificationSupport::release_notifiers()
1.120 +{
1.121 + for (unsigned int endpoint = 0; endpoint < _max_endpoints; endpoint++)
1.122 + {
1.123 + std::set<l4_cap_idx_t>::iterator it;
1.124 +
1.125 + for (it = _notifiers[endpoint].begin(); it != _notifiers[endpoint].end(); it++)
1.126 + ipc_cap_free_um(*it);
1.127 +
1.128 + _notifiers[endpoint].clear();
1.129 + }
1.130 +}
1.131 +
1.132 +// vim: tabstop=4 expandtab shiftwidth=4