libfilezilla
Loading...
Searching...
No Matches
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
18
19namespace fz {
20
21class async_task;
22class event_handler;
23class thread_pool;
24
34class FZ_PUBLIC_SYMBOL event_loop final
35{
36public:
39
41 explicit event_loop(thread_pool & pool);
42
43 enum loop_option
44 {
45 threadless
46 };
47 explicit event_loop(loop_option);
48
51
52 event_loop(event_loop const&) = delete;
53 event_loop& operator=(event_loop const&) = delete;
54
66 void filter_events(std::function<bool (event_handler*&, event_base&)> const& filter);
67
71 void remove_events(event_source const* const source);
72
79 void stop(bool join = false);
80
82 void run();
83
84 bool running() const;
85
92
93private:
94 friend class event_handler;
95
96 void FZ_PRIVATE_SYMBOL remove_handler(event_handler* handler);
97
98 timer_id FZ_PRIVATE_SYMBOL add_timer(event_handler* handler, monotonic_clock const& deadline, duration const& interval);
99 void FZ_PRIVATE_SYMBOL stop_timer(timer_id id);
100 timer_id FZ_PRIVATE_SYMBOL stop_add_timer(timer_id id, event_handler* handler, monotonic_clock const& deadline, duration const& interval);
101
102 void send_event(event_handler* handler, event_base* evt, bool deletable);
103
104 // Process the next (if any) event. Returns true if an event has been processed
105 bool FZ_PRIVATE_SYMBOL process_event(scoped_lock & l);
106
107 // Process timers. Returns true if a timer has been triggered
108 bool FZ_PRIVATE_SYMBOL process_timers(scoped_lock & l);
109
110 void FZ_PRIVATE_SYMBOL entry();
111 void FZ_PRIVATE_SYMBOL timer_entry();
112
113 struct FZ_PRIVATE_SYMBOL timer_data final
114 {
115 event_handler* handler_{};
116 timer_id id_{};
117 monotonic_clock deadline_;
118 duration interval_{};
119 };
120
121 timer_id FZ_PRIVATE_SYMBOL setup_timer(scoped_lock &lock, timer_data &d, event_handler* handler, monotonic_clock const& deadline, duration const& interval);
122
123 typedef std::vector<timer_data> Timers;
124
125 typedef std::deque<std::tuple<event_handler*, event_base*, bool>> Events;
126 Events pending_events_;
127 Timers timers_;
128
129 mutable mutex sync_;
130 condition cond_;
131
132 condition timer_cond_;
133 bool do_timers_{};
134
135 bool active_handler_removed_{};
136 event_handler * active_handler_{};
137
138 monotonic_clock deadline_;
139 size_t deadline_index_{};
140
141 timer_id next_timer_id_{};
142
143 thread::id thread_id_{};
144
145 std::unique_ptr<thread> thread_;
146 std::unique_ptr<async_task> task_;
147
148 thread_pool* pool_{};
149 std::unique_ptr<thread> timer_thread_;
150 std::unique_ptr<async_task> timer_task_;
151
152 bool quit_{};
153 bool threadless_{};
154 bool resend_{};
155
156 enum class Mode {
157 thread,
158 tasks,
159 threadless
160 };
161 Mode mode_{};
162};
163
164}
165#endif
Template helper to call a function with its arguments extracted from a tuple.
Handle for asynchronous tasks.
Definition thread_pool.hpp:24
The duration class represents a time interval in milliseconds.
Definition time.hpp:291
Common base class for all events.
Definition event.hpp:23
Definition event_handler.hpp:63
A threaded event loop that supports sending events and timers.
Definition event_loop.hpp:35
~event_loop()
Stops the thread.
void run()
Starts the loop in the caller's thread.
void remove_events(event_source const *const source)
Removes all events of event_with_source type with the given source on this loop.
event_loop(thread_pool &pool)
Takes a thread from the pool and starts the loop.
void filter_events(std::function< bool(event_handler *&, event_base &)> const &filter)
Allows filtering of queued events.
void stop(bool join=false)
Stops the loop.
void resend_current_event()
event_loop()
Spawns a thread and starts the loop.
Definition event.hpp:101
A monotonic clock (aka steady clock) is independent from walltime.
Definition time.hpp:403
A simple scoped lock.
Definition mutex.hpp:117
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
Declares thread.
Assorted classes dealing with time.