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 
40 
42  explicit event_loop(thread_pool & pool);
43 
44  enum loop_option
45  {
46  threadless
47  };
48  explicit event_loop(loop_option);
49 
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  bool running() const;
81 private:
82  friend class event_handler;
83 
84  void FZ_PRIVATE_SYMBOL remove_handler(event_handler* handler);
85 
86  timer_id FZ_PRIVATE_SYMBOL add_timer(event_handler* handler, monotonic_clock const& deadline, duration const& interval);
87  void FZ_PRIVATE_SYMBOL stop_timer(timer_id id);
88  timer_id FZ_PRIVATE_SYMBOL stop_add_timer(timer_id id, event_handler* handler, monotonic_clock const& deadline, duration const& interval);
89 
90  void send_event(event_handler* handler, event_base* evt);
91 
92  // Process the next (if any) event. Returns true if an event has been processed
93  bool FZ_PRIVATE_SYMBOL process_event(scoped_lock & l);
94 
95  // Process timers. Returns true if a timer has been triggered
96  bool FZ_PRIVATE_SYMBOL process_timers(scoped_lock & l, monotonic_clock& now);
97 
98  void FZ_PRIVATE_SYMBOL entry();
99 
100  struct FZ_PRIVATE_SYMBOL timer_data final
101  {
102  event_handler* handler_{};
103  timer_id id_{};
104  monotonic_clock deadline_;
105  duration interval_{};
106  };
107 
108  timer_id FZ_PRIVATE_SYMBOL setup_timer(scoped_lock &lock, timer_data &d, event_handler* handler, monotonic_clock const& deadline, duration const& interval);
109 
110  typedef std::vector<timer_data> Timers;
111 
112  Events pending_events_;
113  Timers timers_;
114 
115  mutable mutex sync_;
116  condition cond_;
117 
118  event_handler * active_handler_{};
119 
120  monotonic_clock deadline_;
121 
122  timer_id next_timer_id_{};
123 
124  thread::id thread_id_{};
125 
126  std::unique_ptr<thread> thread_;
127  std::unique_ptr<async_task> task_;
128 
129  bool quit_{};
130  bool threadless_{};
131 };
132 
133 }
134 #endif
Template helper to call a function with its arguments extracted from a tuple.
The duration class represents a time interval in milliseconds.
Definition: time.hpp:291
Common base class for all events.
Definition: event.hpp:23
Simple handler for asynchronous event processing.
Definition: event_handler.hpp:55
A threaded event loop that supports sending events and timers.
Definition: event_loop.hpp:34
~event_loop()
Stops the thread.
void run()
Starts the loop in the caller's thread.
void filter_events(std::function< bool(Events::value_type &)> const &filter)
Allows filtering of queued events.
event_loop(thread_pool &pool)
Takes a thread from the pool and starts the loop.
void stop(bool join=false)
Stops the loop.
event_loop()
Spawns a thread and starts the loop.
A monotonic clock (aka steady clock) is independent from walltime.
Definition: time.hpp:403
A simple scoped lock.
Definition: mutex.hpp:93
A dumb thread-pool for asynchronous tasks.
Definition: thread_pool.hpp:64
Declares event_base and simple_event<>
Thread synchronization primitives: mutex, scoped_lock and condition.
The namespace used by libfilezilla.
Definition: apply.hpp:17
simple_event< process_event_type, process *, process_event_flag > process_event
Definition: process.hpp:37
Declares thread.
Assorted classes dealing with time.