| 1 | #ifndef AWS_IO_EVENT_LOOP_H |
| 2 | #define AWS_IO_EVENT_LOOP_H |
| 3 | |
| 4 | /** |
| 5 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | * SPDX-License-Identifier: Apache-2.0. |
| 7 | */ |
| 8 | |
| 9 | #include <aws/io/io.h> |
| 10 | |
| 11 | AWS_PUSH_SANE_WARNING_LEVEL |
| 12 | |
| 13 | struct aws_event_loop; |
| 14 | struct aws_event_loop_group; |
| 15 | struct aws_event_loop_options; |
| 16 | struct aws_shutdown_callback_options; |
| 17 | struct aws_task; |
| 18 | |
| 19 | /** |
| 20 | * @internal |
| 21 | */ |
| 22 | typedef void(aws_event_loop_on_event_fn)( |
| 23 | struct aws_event_loop *event_loop, |
| 24 | struct aws_io_handle *handle, |
| 25 | int events, |
| 26 | void *user_data); |
| 27 | |
| 28 | /** |
| 29 | * @internal |
| 30 | */ |
| 31 | struct aws_event_loop_vtable { |
| 32 | void (*start_destroy)(struct aws_event_loop *event_loop); |
| 33 | void (*complete_destroy)(struct aws_event_loop *event_loop); |
| 34 | int (*run)(struct aws_event_loop *event_loop); |
| 35 | int (*stop)(struct aws_event_loop *event_loop); |
| 36 | int (*wait_for_stop_completion)(struct aws_event_loop *event_loop); |
| 37 | void (*schedule_task_now)(struct aws_event_loop *event_loop, struct aws_task *task); |
| 38 | void (*schedule_task_now_serialized)(struct aws_event_loop *event_loop, struct aws_task *task); |
| 39 | void (*schedule_task_future)(struct aws_event_loop *event_loop, struct aws_task *task, uint64_t run_at_nanos); |
| 40 | void (*cancel_task)(struct aws_event_loop *event_loop, struct aws_task *task); |
| 41 | int (*connect_to_io_completion_port)(struct aws_event_loop *event_loop, struct aws_io_handle *handle); |
| 42 | int (*subscribe_to_io_events)( |
| 43 | struct aws_event_loop *event_loop, |
| 44 | struct aws_io_handle *handle, |
| 45 | int events, |
| 46 | aws_event_loop_on_event_fn *on_event, |
| 47 | void *user_data); |
| 48 | int (*unsubscribe_from_io_events)(struct aws_event_loop *event_loop, struct aws_io_handle *handle); |
| 49 | void (*free_io_event_resources)(void *user_data); |
| 50 | bool (*is_on_callers_thread)(struct aws_event_loop *event_loop); |
| 51 | }; |
| 52 | |
| 53 | /** |
| 54 | * Event Loop Type. If set to `AWS_EVENT_LOOP_PLATFORM_DEFAULT`, the event loop will automatically use the platform’s |
| 55 | * default. |
| 56 | * |
| 57 | * Default Event Loop Type |
| 58 | * Linux | AWS_EVENT_LOOP_EPOLL |
| 59 | * Windows | AWS_EVENT_LOOP_IOCP |
| 60 | * BSD Variants| AWS_EVENT_LOOP_KQUEUE |
| 61 | * macOS | AWS_EVENT_LOOP_KQUEUE |
| 62 | * iOS | AWS_EVENT_LOOP_DISPATCH_QUEUE |
| 63 | */ |
| 64 | enum aws_event_loop_type { |
| 65 | AWS_EVENT_LOOP_PLATFORM_DEFAULT = 0, |
| 66 | AWS_EVENT_LOOP_EPOLL, |
| 67 | AWS_EVENT_LOOP_IOCP, |
| 68 | AWS_EVENT_LOOP_KQUEUE, |
| 69 | AWS_EVENT_LOOP_DISPATCH_QUEUE, |
| 70 | }; |
| 71 | |
| 72 | /** |
| 73 | * Event loop group configuration options |
| 74 | */ |
| 75 | struct aws_event_loop_group_options { |
| 76 | |
| 77 | /** |
| 78 | * How many event loops that event loop group should contain. For most group types, this implies |
| 79 | * the creation and management of an analagous amount of managed threads |
| 80 | */ |
| 81 | uint16_t loop_count; |
| 82 | |
| 83 | /** |
| 84 | * Event loop type. If the event loop type is set to AWS_EVENT_LOOP_PLATFORM_DEFAULT, the |
| 85 | * creation function will automatically use the platform’s default event loop type. |
| 86 | */ |
| 87 | enum aws_event_loop_type type; |
| 88 | |
| 89 | /** |
| 90 | * Optional callback to invoke when the event loop group finishes destruction. |
| 91 | */ |
| 92 | const struct aws_shutdown_callback_options *shutdown_options; |
| 93 | |
| 94 | /** |
| 95 | * Optional configuration to control how the event loop group's threads bind to CPU groups |
| 96 | */ |
| 97 | const uint16_t *cpu_group; |
| 98 | |
| 99 | /** |
| 100 | * Override for the clock function that event loops should use. Defaults to the system's high resolution |
| 101 | * timer. |
| 102 | * |
| 103 | * Do not bind this value to managed code; it is only used in timing-sensitive tests. |
| 104 | */ |
| 105 | aws_io_clock_fn *clock_override; |
| 106 | }; |
| 107 | |
| 108 | AWS_EXTERN_C_BEGIN |
| 109 | |
| 110 | /** |
| 111 | * The event loop will schedule the task and run it on the event loop thread as soon as possible. |
| 112 | * Note that cancelled tasks may execute outside the event loop thread. |
| 113 | * This function may be called from outside or inside the event loop thread. |
| 114 | * |
| 115 | * The task should not be cleaned up or modified until its function is executed. |
| 116 | */ |
| 117 | AWS_IO_API |
| 118 | void aws_event_loop_schedule_task_now(struct aws_event_loop *event_loop, struct aws_task *task); |
| 119 | |
| 120 | /** |
| 121 | * Variant of aws_event_loop_schedule_task_now that forces all tasks to go through the cross thread task queue, |
| 122 | * guaranteeing that order-of-submission is order-of-execution. If you need this guarantee, you must use this |
| 123 | * function; the base function contains short-circuiting logic that breaks ordering invariants. Beyond that, all |
| 124 | * properties of aws_event_loop_schedule_task_now apply to this function as well. |
| 125 | * |
| 126 | * Serialization guarantee does not apply to task cancellation (which can occur out-of-order or even out-of-thread in |
| 127 | * certain cases). |
| 128 | */ |
| 129 | AWS_IO_API |
| 130 | void aws_event_loop_schedule_task_now_serialized(struct aws_event_loop *event_loop, struct aws_task *task); |
| 131 | |
| 132 | /** |
| 133 | * The event loop will schedule the task and run it at the specified time. |
| 134 | * Use aws_event_loop_current_clock_time() to query the current time in nanoseconds. |
| 135 | * Note that cancelled tasks may execute outside the event loop thread. |
| 136 | * This function may be called from outside or inside the event loop thread. |
| 137 | * |
| 138 | * The task should not be cleaned up or modified until its function is executed. |
| 139 | */ |
| 140 | AWS_IO_API |
| 141 | void aws_event_loop_schedule_task_future( |
| 142 | struct aws_event_loop *event_loop, |
| 143 | struct aws_task *task, |
| 144 | uint64_t run_at_nanos); |
| 145 | |
| 146 | /** |
| 147 | * Cancels task. |
| 148 | * This function must be called from the event loop's thread, and is only guaranteed |
| 149 | * to work properly on tasks scheduled from within the event loop's thread. |
| 150 | * The task will be executed with the AWS_TASK_STATUS_CANCELED status inside this call. |
| 151 | */ |
| 152 | AWS_IO_API |
| 153 | void aws_event_loop_cancel_task(struct aws_event_loop *event_loop, struct aws_task *task); |
| 154 | |
| 155 | /** |
| 156 | * Returns true if the event loop's thread is the same thread that called this function, otherwise false. |
| 157 | */ |
| 158 | AWS_IO_API |
| 159 | bool aws_event_loop_thread_is_callers_thread(struct aws_event_loop *event_loop); |
| 160 | |
| 161 | /** |
| 162 | * Gets the current timestamp for the event loop's clock, in nanoseconds. This function is thread-safe. |
| 163 | */ |
| 164 | AWS_IO_API |
| 165 | int aws_event_loop_current_clock_time(const struct aws_event_loop *event_loop, uint64_t *time_nanos); |
| 166 | |
| 167 | /** |
| 168 | * Creation function for event loop groups. |
| 169 | */ |
| 170 | AWS_IO_API |
| 171 | struct aws_event_loop_group *aws_event_loop_group_new( |
| 172 | struct aws_allocator *allocator, |
| 173 | const struct aws_event_loop_group_options *options); |
| 174 | |
| 175 | /** |
| 176 | * Increments the reference count on the event loop group, allowing the caller to take a reference to it. |
| 177 | * |
| 178 | * Returns the same event loop group passed in. |
| 179 | */ |
| 180 | AWS_IO_API |
| 181 | struct aws_event_loop_group *aws_event_loop_group_acquire(struct aws_event_loop_group *el_group); |
| 182 | |
| 183 | /** |
| 184 | * Decrements an event loop group's ref count. When the ref count drops to zero, the event loop group will be |
| 185 | * destroyed. |
| 186 | */ |
| 187 | AWS_IO_API |
| 188 | void aws_event_loop_group_release(struct aws_event_loop_group *el_group); |
| 189 | |
| 190 | /** |
| 191 | * Increments the reference count on the event loop group from event loop, allowing the caller to take a reference to |
| 192 | * it. |
| 193 | * |
| 194 | * Returns the base event loop group of the event loop, or null if the event loop does not belong to a group. |
| 195 | */ |
| 196 | AWS_IO_API |
| 197 | struct aws_event_loop_group *aws_event_loop_group_acquire_from_event_loop(struct aws_event_loop *event_loop); |
| 198 | |
| 199 | /** |
| 200 | * Decrements the ref count of the event loop's base event loop group. When the ref count drops to zero, the event loop |
| 201 | * group will be destroyed. |
| 202 | */ |
| 203 | AWS_IO_API |
| 204 | void aws_event_loop_group_release_from_event_loop(struct aws_event_loop *event_loop); |
| 205 | |
| 206 | /** |
| 207 | * Returns the event loop at a particular index. If the index is out of bounds, null is returned. |
| 208 | */ |
| 209 | AWS_IO_API |
| 210 | struct aws_event_loop *aws_event_loop_group_get_loop_at(struct aws_event_loop_group *el_group, size_t index); |
| 211 | |
| 212 | /** |
| 213 | * Gets the number of event loops managed by an event loop group. |
| 214 | */ |
| 215 | AWS_IO_API |
| 216 | size_t aws_event_loop_group_get_loop_count(const struct aws_event_loop_group *el_group); |
| 217 | |
| 218 | /** |
| 219 | * Gets the event loop type used by an event loop group. |
| 220 | */ |
| 221 | AWS_IO_API |
| 222 | enum aws_event_loop_type aws_event_loop_group_get_type(const struct aws_event_loop_group *el_group); |
| 223 | |
| 224 | /** |
| 225 | * Fetches the next loop for use. The purpose is to enable load balancing across loops. You should not depend on how |
| 226 | * this load balancing is done as it is subject to change in the future. Currently it uses the "best-of-two" algorithm |
| 227 | * based on the load factor of each loop. |
| 228 | */ |
| 229 | AWS_IO_API |
| 230 | struct aws_event_loop *aws_event_loop_group_get_next_loop(struct aws_event_loop_group *el_group); |
| 231 | |
| 232 | /** |
| 233 | * @deprecated - use aws_event_loop_group_new() instead |
| 234 | */ |
| 235 | AWS_IO_API |
| 236 | struct aws_event_loop_group *aws_event_loop_group_new_default( |
| 237 | struct aws_allocator *alloc, |
| 238 | uint16_t max_threads, |
| 239 | const struct aws_shutdown_callback_options *shutdown_options); |
| 240 | |
| 241 | /** |
| 242 | * @deprecated - use aws_event_loop_group_new() instead |
| 243 | */ |
| 244 | AWS_IO_API |
| 245 | struct aws_event_loop_group *aws_event_loop_group_new_default_pinned_to_cpu_group( |
| 246 | struct aws_allocator *alloc, |
| 247 | uint16_t max_threads, |
| 248 | uint16_t cpu_group, |
| 249 | const struct aws_shutdown_callback_options *shutdown_options); |
| 250 | |
| 251 | /** |
| 252 | * @internal - Don't use outside of testing. |
| 253 | * |
| 254 | * Returns the opaque internal user data of an event loop. Can be cast into a specific implementation by |
| 255 | * privileged consumers. |
| 256 | */ |
| 257 | AWS_IO_API |
| 258 | void *aws_event_loop_get_impl(struct aws_event_loop *event_loop); |
| 259 | |
| 260 | /** |
| 261 | * @internal - Don't use outside of testing. |
| 262 | * |
| 263 | * Initializes the base structure used by all event loop implementations with test-oriented overrides. |
| 264 | */ |
| 265 | AWS_IO_API |
| 266 | struct aws_event_loop *aws_event_loop_new_base( |
| 267 | struct aws_allocator *allocator, |
| 268 | aws_io_clock_fn *clock, |
| 269 | struct aws_event_loop_vtable *vtable, |
| 270 | void *impl); |
| 271 | |
| 272 | /** |
| 273 | * @internal - Don't use outside of testing. |
| 274 | * |
| 275 | * Common cleanup code for all implementations. |
| 276 | * This is only called from the *destroy() function of event loop implementations. |
| 277 | */ |
| 278 | AWS_IO_API |
| 279 | void aws_event_loop_clean_up_base(struct aws_event_loop *event_loop); |
| 280 | |
| 281 | /** |
| 282 | * @internal - Don't use outside of testing. |
| 283 | * |
| 284 | * Destroys an event loop implementation. |
| 285 | * If the event loop is still in a running state, this function will block waiting on the event loop to shutdown. |
| 286 | * If the event loop is shared by multiple threads then destroy must be called by exactly one thread. All other threads |
| 287 | * must ensure their API calls to the event loop happen-before the call to destroy. |
| 288 | * |
| 289 | * Internally, this calls aws_event_loop_start_destroy() followed by aws_event_loop_complete_destroy() |
| 290 | */ |
| 291 | AWS_IO_API |
| 292 | void aws_event_loop_destroy(struct aws_event_loop *event_loop); |
| 293 | |
| 294 | /** |
| 295 | * @internal |
| 296 | * |
| 297 | * Signals an event loop to begin its destruction process. If an event loop's implementation of this API does anything, |
| 298 | * it must be quick and non-blocking. Most event loop implementations have an empty implementation for this function. |
| 299 | */ |
| 300 | AWS_IO_API |
| 301 | void aws_event_loop_start_destroy(struct aws_event_loop *event_loop); |
| 302 | |
| 303 | /** |
| 304 | * @internal |
| 305 | * |
| 306 | * Waits for an event loop to complete its destruction process. aws_event_loop_start_destroy() must have been called |
| 307 | * previously for this function to not deadlock. |
| 308 | */ |
| 309 | AWS_IO_API |
| 310 | void aws_event_loop_complete_destroy(struct aws_event_loop *event_loop); |
| 311 | |
| 312 | AWS_EXTERN_C_END |
| 313 | |
| 314 | AWS_POP_SANE_WARNING_LEVEL |
| 315 | |
| 316 | #endif /* AWS_IO_EVENT_LOOP_H */ |