2
* Copyright © 2015 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License version 3,
6
* as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Christopher James Halse Rogers <christopher.halse.rogers@canonical.com>
19
#ifndef MIR_DISPATCH_MULTIPLEXING_DISPATCHABLE_H_
20
#define MIR_DISPATCH_MULTIPLEXING_DISPATCHABLE_H_
22
#include "mir/dispatch/dispatchable.h"
25
#include <initializer_list>
36
* \brief How concurrent dispatch should be handled
38
enum class DispatchReentrancy
40
sequential, /**< The dispatch function is guaranteed not to be called
41
* while a thread is currently running it.
43
reentrant /**< The dispatch function may be called on multiple threads
49
* \brief An adaptor that combines multiple Dispatchables into a single Dispatchable
50
* \note Instances are fully thread-safe.
52
class MultiplexingDispatchable final : public Dispatchable
55
MultiplexingDispatchable();
56
MultiplexingDispatchable(std::initializer_list<std::shared_ptr<Dispatchable>> dispatchees);
57
virtual ~MultiplexingDispatchable() noexcept;
59
MultiplexingDispatchable& operator=(MultiplexingDispatchable const&) = delete;
60
MultiplexingDispatchable(MultiplexingDispatchable const&) = delete;
62
Fd watch_fd() const override;
63
bool dispatch(FdEvents events) override;
64
FdEvents relevant_events() const override;
67
* \brief Add a dispatchable to the adaptor
68
* \param [in] dispatchee Dispatchable to add. The Dispatchable's dispatch()
69
* function will not be called reentrantly.
71
void add_watch(std::shared_ptr<Dispatchable> const& dispatchee);
73
* \brief Add a dispatchable to the adaptor, specifying the reentrancy of dispatch()
75
void add_watch(std::shared_ptr<Dispatchable> const& dispatchee, DispatchReentrancy reentrancy);
78
* \brief Add a simple callback to the adaptor
79
* \param [in] fd File descriptor to monitor for readability
80
* \param [in] callback Callback to fire when \ref fd becomes readable.
81
* This callback is not called reentrantly.
83
void add_watch(Fd const& fd, std::function<void()> const& callback);
86
* \brief Remove a watch from the dispatchable
87
* \param [in] dispatchee Dispatchable to remove
89
void remove_watch(std::shared_ptr<Dispatchable> const& dispatchee);
92
* \brief Remove a watch by file-descriptor
93
* \param [in] fd File descriptor of watch to remove.
95
void remove_watch(Fd const& fd);
97
pthread_rwlock_t lifetime_mutex;
98
std::list<std::pair<std::shared_ptr<Dispatchable>, bool>> dispatchee_holder;
104
#endif // MIR_DISPATCH_MULTIPLEXING_DISPATCHABLE_H_