Mir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
asio_main_loop.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2013 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU 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 General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Alexandros Frantzis <alexandros.frantzis@canonical.com>
17  */
18 
19 #ifndef MIR_ASIO_MAIN_LOOP_H_
20 #define MIR_ASIO_MAIN_LOOP_H_
21 
22 #include "mir/main_loop.h"
23 
24 #include <boost/asio.hpp>
25 #include <boost/optional.hpp>
26 #include <memory>
27 #include <vector>
28 #include <thread>
29 #include <mutex>
30 #include <utility>
31 #include <deque>
32 #include <set>
33 
34 namespace mir
35 {
36 
37 namespace time
38 {
39 class Clock;
40 }
41 
42 class AsioMainLoop : public MainLoop
43 {
44 public:
45  explicit AsioMainLoop(std::shared_ptr<time::Clock> const& clock);
46  ~AsioMainLoop() noexcept(true);
47 
48  void run();
49  void stop();
50 
52  std::initializer_list<int> signals,
53  std::function<void(int)> const& handler);
54 
56  std::initializer_list<int> fd,
57  void const* owner,
58  std::function<void(int)> const& handler) override;
59 
60  void unregister_fd_handler(void const* owner) override;
61 
62  std::unique_ptr<time::Alarm> notify_in(std::chrono::milliseconds delay,
63  std::function<void()> callback) override;
64  std::unique_ptr<time::Alarm> notify_at(mir::time::Timestamp time_point,
65  std::function<void()> callback) override;
66  std::unique_ptr<time::Alarm> create_alarm(std::function<void()> callback) override;
67 
68  void enqueue(void const* owner, ServerAction const& action);
69  void pause_processing_for(void const* owner);
70  void resume_processing_for(void const* owner);
71 
72 private:
73  class SignalHandler;
74  class FDHandler;
75  bool should_process(void const*);
76  void process_server_actions();
77 
78  boost::asio::io_service io;
79  boost::asio::io_service::work work;
80  boost::optional<std::thread::id> main_loop_thread;
81  std::vector<std::unique_ptr<SignalHandler>> signal_handlers;
82  std::vector<std::shared_ptr<FDHandler>> fd_handlers;
83  std::mutex fd_handlers_mutex;
84  std::mutex server_actions_mutex;
85  std::deque<std::pair<void const*,ServerAction>> server_actions;
86  std::set<void const*> do_not_process;
87  std::shared_ptr<time::Clock> const clock;
88 };
89 
90 }
91 
92 #endif /* MIR_ASIO_MAIN_LOOP_H */
Definition: asio_main_loop.h:42
All things Mir.
Definition: aging_buffer.h:24
std::chrono::high_resolution_clock::time_point Timestamp
Definition: clock.h:29
void resume_processing_for(void const *owner)
Definition: asio_main_loop.cpp:518
void unregister_fd_handler(void const *owner) override
Definition: asio_main_loop.cpp:299
void register_signal_handler(std::initializer_list< int > signals, std::function< void(int)> const &handler)
Definition: asio_main_loop.cpp:270
void run()
Definition: asio_main_loop.cpp:258
std::unique_ptr< time::Alarm > create_alarm(std::function< void()> callback) override
Create an Alarm that will not fire until scheduled.
Definition: asio_main_loop.cpp:497
void pause_processing_for(void const *owner)
Definition: asio_main_loop.cpp:512
void stop()
Definition: asio_main_loop.cpp:264
std::function< void()> ServerAction
Definition: server_action_queue.h:27
Definition: main_loop.h:29
~AsioMainLoop() noexcept(true)
Definition: asio_main_loop.cpp:254
std::unique_ptr< time::Alarm > notify_in(std::chrono::milliseconds delay, std::function< void()> callback) override
Create an Alarm that calls the callback after the specified delay.
Definition: asio_main_loop.cpp:485
std::unique_ptr< time::Alarm > notify_at(mir::time::Timestamp time_point, std::function< void()> callback) override
Create an Alarm that calls the callback at the specified time.
Definition: asio_main_loop.cpp:491
void enqueue(void const *owner, ServerAction const &action)
Definition: asio_main_loop.cpp:502
void register_fd_handler(std::initializer_list< int > fd, void const *owner, std::function< void(int)> const &handler) override
Definition: asio_main_loop.cpp:284
AsioMainLoop(std::shared_ptr< time::Clock > const &clock)
Definition: asio_main_loop.cpp:248

Copyright © 2012,2013 Canonical Ltd.
Generated on Fri Oct 10 14:07:14 UTC 2014