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:
37  event_loop();
38 
40  explicit event_loop(thread_pool & pool);
41 
42  enum loop_option
43  {
44  threadless
45  };
46  explicit event_loop(loop_option);
47 
49  ~event_loop();
50 
51  event_loop(event_loop const&) = delete;
52  event_loop& operator=(event_loop const&) = delete;
53 
65  void filter_events(std::function<bool (event_handler*&, event_base&)> const& filter);
66 
70  void remove_events(event_source const* const source);
71 
78  void stop(bool join = false);
79 
81  void run();
82 
83  bool running() const;
84 
85  void resend_current_event() {
86  resend_ = true;
87  }
88 
89 private:
90  friend class event_handler;
91 
92  void FZ_PRIVATE_SYMBOL remove_handler(event_handler* handler);
93 
94  timer_id FZ_PRIVATE_SYMBOL add_timer(event_handler* handler, monotonic_clock const& deadline, duration const& interval);
95  void FZ_PRIVATE_SYMBOL stop_timer(timer_id id);
96  timer_id FZ_PRIVATE_SYMBOL stop_add_timer(timer_id id, event_handler* handler, monotonic_clock const& deadline, duration const& interval);
97 
98  void send_event(event_handler* handler, event_base* evt, bool deletable);
99 
100  // Process the next (if any) event. Returns true if an event has been processed
101  bool FZ_PRIVATE_SYMBOL process_event(scoped_lock & l);
102 
103  // Process timers. Returns true if a timer has been triggered
104  bool FZ_PRIVATE_SYMBOL process_timers(scoped_lock & l);
105 
106  void FZ_PRIVATE_SYMBOL entry();
107  void FZ_PRIVATE_SYMBOL timer_entry();
108 
109  struct FZ_PRIVATE_SYMBOL timer_data final
110  {
111  event_handler* handler_{};
112  timer_id id_{};
113  monotonic_clock deadline_;
114  duration interval_{};
115  };
116 
117  timer_id FZ_PRIVATE_SYMBOL setup_timer(scoped_lock &lock, timer_data &d, event_handler* handler, monotonic_clock const& deadline, duration const& interval);
118 
119  typedef std::vector<timer_data> Timers;
120 
121  typedef std::deque<std::tuple<event_handler*, event_base*, bool>> Events;
122  Events pending_events_;
123  Timers timers_;
124 
125  mutable mutex sync_;
126  condition cond_;
127 
128  condition timer_cond_;
129  bool do_timers_{};
130 
131  bool active_handler_removed_{};
132  event_handler * active_handler_{};
133 
134  monotonic_clock deadline_;
135 
136  timer_id next_timer_id_{};
137 
138  thread::id thread_id_{};
139 
140  std::unique_ptr<thread> thread_;
141  std::unique_ptr<async_task> task_;
142 
143  thread_pool* pool_{};
144  std::unique_ptr<thread> timer_thread_;
145  std::unique_ptr<async_task> timer_task_;
146 
147  bool quit_{};
148  bool threadless_{};
149  bool resend_{};
150 
151  enum class Mode {
152  thread,
153  tasks,
154  threadless
155  };
156  Mode mode_{};
157 };
158 
159 }
160 #endif
Spawns and represents a new thread of execution.
Definition: thread.hpp:29
Thread synchronization primitives: mutex, scoped_lock and condition.
A simple scoped lock.
Definition: mutex.hpp:116
simple_event< process_event_type, process *, process_event_flag > process_event
Definition: process.hpp:37
Definition: event_handler.hpp:60
Waitable condition variable.
Definition: mutex.hpp:233
Assorted classes dealing with time.
Definition: event.hpp:101
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:402
The namespace used by libfilezilla.
Definition: apply.hpp:17
The duration class represents a time interval in milliseconds.
Definition: time.hpp:290
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:74
A dumb thread-pool for asynchronous tasks.
Definition: thread_pool.hpp:63
Common base class for all events.
Definition: event.hpp:22