41
41
InvalidTimerId = 0
42
42
};
43
43
44
+ /* *
45
+ * @brief As the name implies, this class represents an event loop that runs in
46
+ * a perticular thread. The event loop can handle network I/O events and timers
47
+ * in asynchronous mode.
48
+ * @note An event loop object always belongs to a separate thread, and there is
49
+ * one event loop object at most in a thread. We can call an event loop object
50
+ * the event loop of the thread it belongs to, or call that thread the thread of
51
+ * the event loop.
52
+ */
44
53
class EventLoop : NonCopyable
45
54
{
46
55
public:
47
56
EventLoop ();
48
57
~EventLoop ();
58
+
59
+ /* *
60
+ * @brief Run the event loop. This method will be blocked until the event
61
+ * loop exits.
62
+ *
63
+ */
49
64
void loop ();
65
+
66
+ /* *
67
+ * @brief Let the event loop quit.
68
+ *
69
+ */
50
70
void quit ();
71
+
72
+ /* *
73
+ * @brief Assertion that the current thread is the thread to which the event
74
+ * loop belongs. If the assertion fails, the program aborts.
75
+ */
51
76
void assertInLoopThread ()
52
77
{
53
78
if (!isInLoopThread ())
@@ -56,47 +81,82 @@ class EventLoop : NonCopyable
56
81
}
57
82
};
58
83
#ifdef __linux__
84
+ /* *
85
+ * @brief Make the timer queue works after calling the fork() function.
86
+ *
87
+ */
59
88
void resetTimerQueue ();
60
89
#endif
90
+ /* *
91
+ * @brief Make the event loop works after calling the fork() function.
92
+ *
93
+ */
61
94
void resetAfterFork ();
95
+
96
+ /* *
97
+ * @brief Return true if the current thread is the thread to which the event
98
+ * loop belongs.
99
+ *
100
+ * @return true
101
+ * @return false
102
+ */
62
103
bool isInLoopThread () const
63
104
{
64
105
return threadId_ == std::this_thread::get_id ();
65
106
};
107
+
108
+ /* *
109
+ * @brief Get the event loop of the current thread. Return nullptr if there
110
+ * is no event loop in the current thread.
111
+ *
112
+ * @return EventLoop*
113
+ */
66
114
static EventLoop *getEventLoopOfCurrentThread ();
67
- void updateChannel (Channel *chl);
68
- void removeChannel (Channel *chl);
115
+
116
+ /* *
117
+ * @brief Run the function f in the thread of the event loop.
118
+ *
119
+ * @param f
120
+ * @note If the current thread is the thread of the event loop, the function
121
+ * f is executed directly before the method exiting.
122
+ */
69
123
void runInLoop (const Func &f);
70
124
void runInLoop (Func &&f);
125
+
126
+ /* *
127
+ * @brief Run the function f in the thread of the event loop.
128
+ *
129
+ * @param f
130
+ * @note The difference between this method and the runInLoop() method is
131
+ * that the function f is executed after the method exiting no matter if the
132
+ * current thread is the thread of the event loop.
133
+ */
71
134
void queueInLoop (const Func &f);
72
135
void queueInLoop (Func &&f);
73
- void wakeup ();
74
- void wakeupRead ();
75
- size_t index ()
76
- {
77
- return index_;
78
- }
79
- void setIndex (size_t index)
80
- {
81
- index_ = index;
82
- }
83
136
84
- // /
85
- // / Runs callback at 'time'.
86
- // / Safe to call from other threads.
87
- // /
137
+ /* *
138
+ * @brief Run a function at a time point.
139
+ *
140
+ * @param time The time to run the function.
141
+ * @param cb The function to run.
142
+ * @return TimerId The ID of the timer.
143
+ */
88
144
TimerId runAt (const Date &time, const Func &cb);
89
145
TimerId runAt (const Date &time, Func &&cb);
90
- // /
91
- // / Runs callback after @c delay seconds.
92
- // / Safe to call from other threads.
93
- // /
146
+
147
+ /* *
148
+ * @brief Run a function after a period of time.
149
+ *
150
+ * @param delay Represent the period of time in seconds.
151
+ * @param cb The function to run.
152
+ * @return TimerId The ID of the timer.
153
+ */
94
154
TimerId runAfter (double delay, const Func &cb);
95
155
TimerId runAfter (double delay, Func &&cb);
96
156
97
- // / Runs @param cb after @param delay
98
157
/* *
99
- * Users could use chrono literals to represent a time duration
158
+ * @brief Run a function after a period of time.
159
+ * @note Users could use chrono literals to represent a time duration
100
160
* For example:
101
161
* @code
102
162
runAfter(5s, task);
@@ -112,15 +172,19 @@ class EventLoop : NonCopyable
112
172
{
113
173
return runAfter (delay.count (), std::move (cb));
114
174
}
115
- // /
116
- // / Runs callback every @c interval seconds.
117
- // / Safe to call from other threads.
118
- // /
175
+
176
+ /* *
177
+ * @brief Repeatedly run a function every period of time.
178
+ *
179
+ * @param interval The duration in seconds.
180
+ * @param cb The function to run.
181
+ * @return TimerId The ID of the timer.
182
+ */
119
183
TimerId runEvery (double interval, const Func &cb);
120
184
TimerId runEvery (double interval, Func &&cb);
121
185
122
- // / Runs @param cb every @param interval time
123
186
/* *
187
+ * @brief Repeatedly run a function every period of time.
124
188
* Users could use chrono literals to represent a time duration
125
189
* For example:
126
190
* @code
@@ -139,27 +203,82 @@ class EventLoop : NonCopyable
139
203
{
140
204
return runEvery (interval.count (), std::move (cb));
141
205
}
142
- // int getAioEventFd();
143
- // io_context_t getAioContext() {return ctx_;};
206
+
207
+ /* *
208
+ * @brief Invalidate the timer identified by the given ID.
209
+ *
210
+ * @param id The ID of the timer.
211
+ */
144
212
void invalidateTimer (TimerId id);
145
213
214
+ /* *
215
+ * @brief Move the EventLoop to the current thread, this method must be
216
+ * called before the loop is running.
217
+ *
218
+ */
219
+ void moveToCurrentThread ();
220
+
221
+ /* *
222
+ * @brief Update channel status. This method is usually used internally.
223
+ *
224
+ * @param chl
225
+ */
226
+ void updateChannel (Channel *chl);
227
+
228
+ /* *
229
+ * @brief Remove a channel from the event loop. This method is usually used
230
+ * internally.
231
+ *
232
+ * @param chl
233
+ */
234
+ void removeChannel (Channel *chl);
235
+
236
+ /* *
237
+ * @brief Return the index of the event loop.
238
+ *
239
+ * @return size_t
240
+ */
241
+ size_t index ()
242
+ {
243
+ return index_;
244
+ }
245
+
246
+ /* *
247
+ * @brief Set the index of the event loop.
248
+ *
249
+ * @param index
250
+ */
251
+ void setIndex (size_t index)
252
+ {
253
+ index_ = index;
254
+ }
255
+
256
+ /* *
257
+ * @brief Return true if the event loop is running.
258
+ *
259
+ * @return true
260
+ * @return false
261
+ */
146
262
bool isRunning ()
147
263
{
148
264
return looping_ && (!quit_);
149
265
}
266
+
267
+ /* *
268
+ * @brief Check if the event loop is calling a function.
269
+ *
270
+ * @return true
271
+ * @return false
272
+ */
150
273
bool isCallingFunctions ()
151
274
{
152
275
return callingFuncs_;
153
276
}
154
- /* *
155
- * @brief Move the EventLoop to the current thread, this method must be
156
- * called before the loop is running.
157
- *
158
- */
159
- void moveToCurrentThread ();
160
277
161
278
private:
162
279
void abortNotInLoopThread ();
280
+ void wakeup ();
281
+ void wakeupRead ();
163
282
bool looping_;
164
283
std::thread::id threadId_;
165
284
bool quit_;
0 commit comments