Skip to content

Commit 47f4d5e

Browse files
committed
document event_loop
1 parent 59f879a commit 47f4d5e

File tree

1 file changed

+113
-6
lines changed

1 file changed

+113
-6
lines changed

src/util/event_loop.h

Lines changed: 113 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
/*
33
* Event Loop
44
*
5-
* - multithreaded event loop
5+
* - multithreaded event loop.
6+
* - based on sd_event by default, but with locks so tasks can be posted
7+
* and event listeners added from any thread.
68
*
79
* Copyright (c) 2023, Hannes Winkler <hanneswinkler2000@web.de>
810
*/
@@ -17,40 +19,145 @@
1719
#include "util/refcounting.h"
1820
#include "util/collection.h"
1921

22+
/**
23+
* @brief An event loop.
24+
*
25+
*/
2026
struct evloop;
2127

28+
/**
29+
* @brief Creates a new event loop.
30+
*
31+
* The event loop is not running yet. Any tasks added using @ref evloop_post_task,
32+
* @ref evloop_post_delayed_task or any fd callbacks added using @ref evloop_add_io
33+
* will not be executed yet.
34+
*
35+
* Unless explicitly stated otherwise, all functions in this file are thread-safe
36+
* and can be called from any thread.
37+
*
38+
* @returns A new event loop or NULL on error.
39+
*/
2240
struct evloop *evloop_new();
2341

2442
DECLARE_REF_OPS(evloop)
2543

26-
int evloop_get_fd(struct evloop *loop);
27-
44+
/**
45+
* @brief Run the event loop.
46+
*
47+
* This function will actually call the (delayed) task callbacks and fd callbacks,
48+
* when they are ready.
49+
*
50+
* This function will run until exit is scheduled using @ref evloop_schedule_exit.
51+
*/
2852
int evloop_run(struct evloop *loop);
2953

54+
/**
55+
* @brief Schedule the event loop to exit.
56+
*
57+
*/
3058
int evloop_schedule_exit(struct evloop *loop);
3159

60+
/**
61+
* @brief Post a task to the event loop to be executed as soon as possible.
62+
*
63+
* @param loop The event loop.
64+
* @param callback The task to execute.
65+
* @param userdata The userdata to pass to the task
66+
*/
3267
int evloop_post_task(struct evloop *loop, void_callback_t callback, void *userdata);
3368

69+
/**
70+
* @brief Post a task to the event loop to be executed not sooner than target_time_usec.
71+
*
72+
* @param loop The event loop.
73+
* @param callback The task to execute.
74+
* @param userdata The userdata to pass to the task
75+
* @param target_time_usec The time in microseconds (of CLOCK_MONOTONIC) when the task should be executed.
76+
*/
3477
int evloop_post_delayed_task(struct evloop *loop, void_callback_t callback, void *userdata, uint64_t target_time_usec);
3578

79+
80+
/**
81+
* @brief An event source that was added to the event loop,
82+
* and can be disabled & destroyed using @ref evsrc_destroy.
83+
*/
3684
struct evsrc;
3785

86+
/**
87+
* @brief Destroy an event source.
88+
*
89+
* After this function returns, the callback registered for the event source
90+
* will not be called anymore.
91+
*
92+
* @param src The event source to destroy.
93+
*/
3894
void evsrc_destroy(struct evsrc *src);
3995

40-
enum event_handler_return { EVENT_HANDLER_CONTINUE, EVENT_HANDLER_CANCEL };
41-
96+
/**
97+
* @brief The return value of an event handler.
98+
*
99+
*/
100+
enum event_handler_return {
101+
/**
102+
* @brief Continue watching the event source (No change basically)
103+
*/
104+
EVENT_HANDLER_CONTINUE,
105+
106+
/**
107+
* @brief Stop watching the event source and destroy it.
108+
*
109+
* This can just be used as a shorthand to @ref evsrc_destroy inside an event handler callback.
110+
*
111+
* NOTE: Calling @ref evsrc_destroy inside an fd callback AND returning this value
112+
* is invalid and will result in undefined behavior.
113+
*/
114+
EVENT_HANDLER_CANCEL
115+
};
116+
117+
/**
118+
* @brief A callback that is called by the event loop when a file descriptor is ready.
119+
*
120+
* @param fd The file descriptor that is ready.
121+
* @param revents The events that are ready.
122+
* @param userdata The userdata passed to @ref evloop_add_io.
123+
* @returns Whether the event source should be kept or destroyed.
124+
*/
42125
typedef enum event_handler_return (*evloop_io_handler_t)(int fd, uint32_t revents, void *userdata);
43126

127+
/**
128+
* @brief Watch a file-descriptor and call a callback when it is ready.
129+
*
130+
* The event loop will call the callback on the thread that it executing @ref evloop_run
131+
* when fd is ready to read/write (depending on @ref events).
132+
*
133+
* To stop watching the fd, call @ref evsrc_destroy on the returned evsrc,
134+
* or return @ref EVENT_HANDLER_CANCEL from the callback.
135+
*
136+
* @param loop The event loop.
137+
* @param fd The file descriptor to watch.
138+
* @param events The events to watch for (EPOLLIN, EPOLLOUT, etc).
139+
* @param callback The callback to call when the fd is ready.
140+
* @param userdata The userdata to pass to the callback.
141+
*/
44142
struct evsrc *evloop_add_io(struct evloop *loop, int fd, uint32_t events, evloop_io_handler_t callback, void *userdata);
45143

46144

47145
struct evthread;
48146

147+
/**
148+
* @brief Start a new thread just running `evloop_run(loop)`.
149+
*/
49150
struct evthread *evthread_start_with_loop(struct evloop *loop);
50151

152+
/**
153+
* @brief Get the thread id of the event thread.
154+
*/
51155
pthread_t evthread_get_pthread(struct evthread *thread);
52156

157+
/**
158+
* @brief Stops the event loop that the thread is running,
159+
* and waits for the event thread to quit.
160+
*/
53161
void evthread_stop(struct evthread *thread);
54162

55163
#endif // _FLUTTERPI_SRC_EVENT_LOOP_H
56-

0 commit comments

Comments
 (0)