~ubuntu-branches/ubuntu/wily/mir/wily-proposed

« back to all changes in this revision

Viewing changes to include/common/mir/dispatch/multiplexing_dispatchable.h

  • Committer: Package Import Robot
  • Author(s): CI Train Bot
  • Date: 2015-05-12 13:12:55 UTC
  • mto: This revision was merged to the branch mainline in revision 96.
  • Revision ID: package-import@ubuntu.com-20150512131255-y7z12i8n4pbvo70x
Tags: upstream-0.13.0+15.10.20150512
Import upstream version 0.13.0+15.10.20150512

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2015 Canonical Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authored by: Christopher James Halse Rogers <christopher.halse.rogers@canonical.com>
 
17
 */
 
18
 
 
19
#ifndef MIR_DISPATCH_MULTIPLEXING_DISPATCHABLE_H_
 
20
#define MIR_DISPATCH_MULTIPLEXING_DISPATCHABLE_H_
 
21
 
 
22
#include "mir/dispatch/dispatchable.h"
 
23
 
 
24
#include <functional>
 
25
#include <initializer_list>
 
26
#include <list>
 
27
#include <mutex>
 
28
 
 
29
#include <pthread.h>
 
30
 
 
31
namespace mir
 
32
{
 
33
namespace dispatch
 
34
{
 
35
/**
 
36
 * \brief How concurrent dispatch should be handled
 
37
 */
 
38
enum class DispatchReentrancy
 
39
{
 
40
    sequential,     /**< The dispatch function is guaranteed not to be called
 
41
                     *   while a thread is currently running it.
 
42
                     */
 
43
    reentrant       /**< The dispatch function may be called on multiple threads
 
44
                     *   simultaneously
 
45
                     */
 
46
};
 
47
 
 
48
/**
 
49
 * \brief An adaptor that combines multiple Dispatchables into a single Dispatchable
 
50
 * \note Instances are fully thread-safe.
 
51
 */
 
52
class MultiplexingDispatchable final : public Dispatchable
 
53
{
 
54
public:
 
55
    MultiplexingDispatchable();
 
56
    MultiplexingDispatchable(std::initializer_list<std::shared_ptr<Dispatchable>> dispatchees);
 
57
    virtual ~MultiplexingDispatchable() noexcept;
 
58
 
 
59
    MultiplexingDispatchable& operator=(MultiplexingDispatchable const&) = delete;
 
60
    MultiplexingDispatchable(MultiplexingDispatchable const&) = delete;
 
61
 
 
62
    Fd watch_fd() const override;
 
63
    bool dispatch(FdEvents events) override;
 
64
    FdEvents relevant_events() const override;
 
65
 
 
66
    /**
 
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.
 
70
     */
 
71
    void add_watch(std::shared_ptr<Dispatchable> const& dispatchee);
 
72
    /**
 
73
     * \brief Add a dispatchable to the adaptor, specifying the reentrancy of dispatch()
 
74
     */
 
75
    void add_watch(std::shared_ptr<Dispatchable> const& dispatchee, DispatchReentrancy reentrancy);
 
76
 
 
77
    /**
 
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.
 
82
     */
 
83
    void add_watch(Fd const& fd, std::function<void()> const& callback);
 
84
 
 
85
    /**
 
86
     * \brief Remove a watch from the dispatchable
 
87
     * \param [in] dispatchee   Dispatchable to remove
 
88
     */
 
89
    void remove_watch(std::shared_ptr<Dispatchable> const& dispatchee);
 
90
 
 
91
    /**
 
92
     * \brief Remove a watch by file-descriptor
 
93
     * \param [in] fd   File descriptor of watch to remove.
 
94
     */
 
95
    void remove_watch(Fd const& fd);
 
96
private:
 
97
    pthread_rwlock_t lifetime_mutex;
 
98
    std::list<std::pair<std::shared_ptr<Dispatchable>, bool>> dispatchee_holder;
 
99
 
 
100
    Fd epoll_fd;
 
101
};
 
102
}
 
103
}
 
104
#endif // MIR_DISPATCH_MULTIPLEXING_DISPATCHABLE_H_