libfilezilla
event_loop.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_EVENT_LOOP_HEADER
2 #define LIBFILEZILLA_EVENT_LOOP_HEADER
3 
4 #include "apply.hpp"
5 #include "event.hpp"
6 #include "mutex.hpp"
7 #include "time.hpp"
8 #include "thread.hpp"
9 
10 #include <deque>
11 #include <functional>
12 #include <memory>
13 #include <vector>
14 
19 namespace fz {
20 
21 class async_task;
22 class event_handler;
23 class thread_pool;
24 
33 class FZ_PUBLIC_SYMBOL event_loop final
34 {
35 public:
36  typedef std::deque<std::pair<event_handler*, event_base*>> Events;
37 
39  event_loop();
40 
42  explicit event_loop(thread_pool & pool);
43 
44  enum loop_option
45  {
46  threadless
47  };
48  explicit event_loop(loop_option);
49 
51  ~event_loop();
52 
53  event_loop(event_loop const&) = delete;
54  event_loop& operator=(event_loop const&) = delete;
55 
67  void filter_events(std::function<bool (Events::value_type&)> const& filter);
68 
75  void stop(bool join = false);
76 
78  void run();
79 
80 private:
81  friend class event_handler;
82 
83  void FZ_PRIVATE_SYMBOL remove_handler(event_handler* handler);
84 
85  timer_id FZ_PRIVATE_SYMBOL add_timer(event_handler* handler, duration const& interval, bool one_shot);
86  void FZ_PRIVATE_SYMBOL stop_timer(timer_id id);
87 
88  void send_event(event_handler* handler, event_base* evt);
89 
90  // Process the next (if any) event. Returns true if an event has been processed
91  bool FZ_PRIVATE_SYMBOL process_event(scoped_lock & l);
92 
93  // Process timers. Returns true if a timer has been triggered
94  bool FZ_PRIVATE_SYMBOL process_timers(scoped_lock & l, monotonic_clock& now);
95 
96  void FZ_PRIVATE_SYMBOL entry();
97 
98  struct FZ_PRIVATE_SYMBOL timer_data final
99  {
100  event_handler* handler_{};
101  timer_id id_{};
102  monotonic_clock deadline_;
103  duration interval_{};
104  };
105 
106  typedef std::vector<timer_data> Timers;
107 
108  Events pending_events_;
109  Timers timers_;
110 
111  mutex sync_;
112  condition cond_;
113 
114  event_handler * active_handler_{};
115 
116  monotonic_clock deadline_;
117 
118  timer_id next_timer_id_{};
119 
120  thread::id thread_id_{};
121 
122  std::unique_ptr<thread> thread_;
123  std::unique_ptr<async_task> task_;
124 
125  bool quit_{};
126 };
127 
128 }
129 #endif
Thread synchronization primitives: mutex, scoped_lock and condition.
A simple scoped lock.
Definition: mutex.hpp:64
Simple handler for asynchronous event processing.
Definition: event_handler.hpp:54
Waitable condition variable.
Definition: mutex.hpp:154
Assorted classes dealing with time.
A threaded event loop that supports sending events and timers.
Definition: event_loop.hpp:33
Declares thread.
A monotonic clock (aka steady clock) is independent from walltime.
Definition: time.hpp:389
The namespace used by libfilezilla.
Definition: apply.hpp:17
The duration class represents a time interval in milliseconds.
Definition: time.hpp:288
Declares event_base and simple_event<>
Template helper to call a function with its arguments extracted from a tuple.
Lean replacement for std::(recursive_)mutex.
Definition: mutex.hpp:27
A dumb thread-pool for asynchronous tasks.
Definition: thread_pool.hpp:62
Common base class for all events.
Definition: event.hpp:22