|
2 | 2 | /* |
3 | 3 | * Event Loop |
4 | 4 | * |
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. |
6 | 8 | * |
7 | 9 | * Copyright (c) 2023, Hannes Winkler <hanneswinkler2000@web.de> |
8 | 10 | */ |
|
17 | 19 | #include "util/refcounting.h" |
18 | 20 | #include "util/collection.h" |
19 | 21 |
|
| 22 | +/** |
| 23 | + * @brief An event loop. |
| 24 | + * |
| 25 | + */ |
20 | 26 | struct evloop; |
21 | 27 |
|
| 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 | + */ |
22 | 40 | struct evloop *evloop_new(); |
23 | 41 |
|
24 | 42 | DECLARE_REF_OPS(evloop) |
25 | 43 |
|
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 | + */ |
28 | 52 | int evloop_run(struct evloop *loop); |
29 | 53 |
|
| 54 | +/** |
| 55 | + * @brief Schedule the event loop to exit. |
| 56 | + * |
| 57 | + */ |
30 | 58 | int evloop_schedule_exit(struct evloop *loop); |
31 | 59 |
|
| 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 | + */ |
32 | 67 | int evloop_post_task(struct evloop *loop, void_callback_t callback, void *userdata); |
33 | 68 |
|
| 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 | + */ |
34 | 77 | int evloop_post_delayed_task(struct evloop *loop, void_callback_t callback, void *userdata, uint64_t target_time_usec); |
35 | 78 |
|
| 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 | + */ |
36 | 84 | struct evsrc; |
37 | 85 |
|
| 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 | + */ |
38 | 94 | void evsrc_destroy(struct evsrc *src); |
39 | 95 |
|
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 | + */ |
42 | 125 | typedef enum event_handler_return (*evloop_io_handler_t)(int fd, uint32_t revents, void *userdata); |
43 | 126 |
|
| 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 | + */ |
44 | 142 | struct evsrc *evloop_add_io(struct evloop *loop, int fd, uint32_t events, evloop_io_handler_t callback, void *userdata); |
45 | 143 |
|
46 | 144 |
|
47 | 145 | struct evthread; |
48 | 146 |
|
| 147 | +/** |
| 148 | + * @brief Start a new thread just running `evloop_run(loop)`. |
| 149 | + */ |
49 | 150 | struct evthread *evthread_start_with_loop(struct evloop *loop); |
50 | 151 |
|
| 152 | +/** |
| 153 | + * @brief Get the thread id of the event thread. |
| 154 | + */ |
51 | 155 | pthread_t evthread_get_pthread(struct evthread *thread); |
52 | 156 |
|
| 157 | +/** |
| 158 | + * @brief Stops the event loop that the thread is running, |
| 159 | + * and waits for the event thread to quit. |
| 160 | + */ |
53 | 161 | void evthread_stop(struct evthread *thread); |
54 | 162 |
|
55 | 163 | #endif // _FLUTTERPI_SRC_EVENT_LOOP_H |
56 | | - |
|
0 commit comments