Debian Codegraph @

crt/aws-c-io/include/aws/io/private/event_loop_impl.h

raw symbols SCIP
1 #ifndef AWS_IO_EVENT_LOOP_IMPL_H
2 #define AWS_IO_EVENT_LOOP_IMPL_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 #include <aws/common/atomics.h>
12 #include <aws/common/hash_table.h>
13 #include <aws/common/ref_count.h>
14 #include <aws/io/event_loop.h>
15
16 AWS_PUSH_SANE_WARNING_LEVEL
17
18 struct aws_event_loop;
19 struct aws_overlapped;
20
21 typedef void(aws_event_loop_on_completion_fn)(
22 struct aws_event_loop *event_loop,
23 struct aws_overlapped *overlapped,
24 int status_code,
25 size_t num_bytes_transferred);
26
27 /**
28 * The aws_win32_OVERLAPPED struct is layout-compatible with OVERLAPPED as defined in <windows.h>. It is used
29 * here to avoid pulling in a dependency on <windows.h> which would also bring along a lot of bad macros, such
30 * as redefinitions of GetMessage and GetObject. Note that the OVERLAPPED struct layout in the Windows SDK can
31 * never be altered without breaking binary compatibility for every existing third-party executable, so there
32 * is no need to worry about keeping this definition in sync.
33 */
34 struct aws_win32_OVERLAPPED {
35 uintptr_t Internal;
36 uintptr_t InternalHigh;
37 union {
38 struct {
39 uint32_t Offset;
40 uint32_t OffsetHigh;
41 } s;
42 void *Pointer;
43 } u;
44 void *hEvent;
45 };
46
47 /**
48 * Use aws_overlapped when a handle connected to the event loop needs an OVERLAPPED struct.
49 * OVERLAPPED structs are needed to make OS-level async I/O calls.
50 * When the I/O completes, the assigned aws_event_loop_on_completion_fn is called from the event_loop's thread.
51 * While the I/O is pending, it is not safe to modify or delete aws_overlapped.
52 * Call aws_overlapped_init() before first use. If the aws_overlapped will be used multiple times, call
53 * aws_overlapped_reset() or aws_overlapped_init() between uses.
54 */
55 struct aws_overlapped {
56 struct aws_win32_OVERLAPPED overlapped;
57 aws_event_loop_on_completion_fn *on_completion;
58 void *user_data;
59 };
60
61 enum aws_io_event_type {
62 AWS_IO_EVENT_TYPE_READABLE = 1,
63 AWS_IO_EVENT_TYPE_WRITABLE = 2,
64 AWS_IO_EVENT_TYPE_REMOTE_HANG_UP = 4,
65 AWS_IO_EVENT_TYPE_CLOSED = 8,
66 AWS_IO_EVENT_TYPE_ERROR = 16,
67 };
68
69 struct aws_event_loop {
70 struct aws_event_loop_vtable *vtable;
71 struct aws_allocator *alloc;
72 aws_io_clock_fn *clock;
73 struct aws_hash_table local_data;
74 struct aws_atomic_var current_load_factor;
75 uint64_t latest_tick_start;
76 size_t current_tick_latency_sum;
77 struct aws_atomic_var next_flush_time;
78 struct aws_event_loop_group *base_elg;
79 void *impl_data;
80 };
81
82 struct aws_event_loop_local_object;
83 typedef void(aws_event_loop_on_local_object_removed_fn)(struct aws_event_loop_local_object *);
84
85 struct aws_event_loop_local_object {
86 const void *key;
87 void *object;
88 aws_event_loop_on_local_object_removed_fn *on_object_removed;
89 };
90
91 struct aws_event_loop_options {
92 aws_io_clock_fn *clock;
93 struct aws_thread_options *thread_options;
94
95 /**
96 * Event loop type. If the event loop type is set to AWS_EVENT_LOOP_PLATFORM_DEFAULT, the
97 * creation function will automatically use the platform’s default event loop type.
98 */
99 enum aws_event_loop_type type;
100
101 /**
102 * The parent `aws_event_loop_group` needs to be accessible from its individual `aws_event_loop` children when using
103 * dispatch queue event loops. Apple dispatch queue event loops are async and so we must insure that the event loops
104 * they use are alive during socket shutdown for the entirety of its shutdown process. To this end, we acquire a
105 * refcount to the parent elg when using Apple network sockets and release the refcount to the parent elg when the
106 * socket is shutdown and cleaned up.
107 */
108 struct aws_event_loop_group *parent_elg;
109 };
110
111 struct aws_event_loop *aws_event_loop_new_with_iocp(
112 struct aws_allocator *alloc,
113 const struct aws_event_loop_options *options);
114
115 struct aws_event_loop *aws_event_loop_new_with_dispatch_queue(
116 struct aws_allocator *alloc,
117 const struct aws_event_loop_options *options);
118
119 struct aws_event_loop *aws_event_loop_new_with_kqueue(
120 struct aws_allocator *alloc,
121 const struct aws_event_loop_options *options);
122
123 struct aws_event_loop *aws_event_loop_new_with_epoll(
124 struct aws_allocator *alloc,
125 const struct aws_event_loop_options *options);
126
127 typedef struct aws_event_loop *(aws_new_event_loop_fn)(struct aws_allocator *alloc,
128 const struct aws_event_loop_options *options,
129 void *new_loop_user_data);
130
131 struct aws_event_loop_group {
132 struct aws_allocator *allocator;
133 struct aws_array_list event_loops;
134 struct aws_ref_count ref_count;
135 struct aws_shutdown_callback_options shutdown_options;
136 enum aws_event_loop_type event_loop_type;
137 };
138
139 AWS_EXTERN_C_BEGIN
140
141 #ifdef AWS_ENABLE_IO_COMPLETION_PORTS
142
143 /**
144 * Prepares aws_overlapped for use, and sets a function to call when the overlapped operation completes.
145 */
146 AWS_IO_API
147 void aws_overlapped_init(
148 struct aws_overlapped *overlapped,
149 aws_event_loop_on_completion_fn *on_completion,
150 void *user_data);
151
152 /**
153 * Prepares aws_overlapped for re-use without changing the assigned aws_event_loop_on_completion_fn.
154 * Call aws_overlapped_init(), instead of aws_overlapped_reset(), to change the aws_event_loop_on_completion_fn.
155 */
156 AWS_IO_API
157 void aws_overlapped_reset(struct aws_overlapped *overlapped);
158
159 /**
160 * Casts an aws_overlapped pointer for use as a LPOVERLAPPED parameter to Windows API functions
161 */
162 AWS_IO_API
163 struct _OVERLAPPED *aws_overlapped_to_windows_overlapped(struct aws_overlapped *overlapped);
164 #endif /* AWS_ENABLE_IO_COMPLETION_PORTS */
165
166 /**
167 * @internal - Don't use outside of testing.
168 *
169 * Return the default event loop type. If the return value is `AWS_ELT_PLATFORM_DEFAULT`, the function failed to
170 * retrieve the default type value.
171 * If `aws_event_loop_override_default_type` has been called, return the override default type.
172 */
173 AWS_IO_API
174 enum aws_event_loop_type aws_event_loop_get_default_type(void);
175
176 /**
177 * Associates an aws_io_handle with the event loop's I/O Completion Port.
178 *
179 * The handle must use aws_overlapped for all async operations requiring an OVERLAPPED struct.
180 * When the operation completes, the aws_overlapped's completion function will run on the event loop thread.
181 * Note that completion functions will not be invoked while the event loop is stopped. Users should wait for all async
182 * operations on connected handles to complete before cleaning up or destroying the event loop.
183 *
184 * A handle may only be connected to one event loop in its lifetime.
185 */
186 AWS_IO_API
187 int aws_event_loop_connect_handle_to_io_completion_port(
188 struct aws_event_loop *event_loop,
189 struct aws_io_handle *handle);
190
191 /**
192 * Subscribes on_event to events on the event-loop for handle. events is a bitwise concatenation of the events that were
193 * received. The definition for these values can be found in aws_io_event_type. Currently, only
194 * AWS_IO_EVENT_TYPE_READABLE and AWS_IO_EVENT_TYPE_WRITABLE are honored. You always are registered for error conditions
195 * and closure. This function may be called from outside or inside the event loop thread. However, the unsubscribe
196 * function must be called inside the event-loop's thread.
197 */
198 AWS_IO_API
199 int aws_event_loop_subscribe_to_io_events(
200 struct aws_event_loop *event_loop,
201 struct aws_io_handle *handle,
202 int events,
203 aws_event_loop_on_event_fn *on_event,
204 void *user_data);
205
206 /**
207 * Creates an instance of the default event loop implementation for the current architecture and operating system.
208 */
209 AWS_IO_API
210 struct aws_event_loop *aws_event_loop_new_default(struct aws_allocator *alloc, aws_io_clock_fn *clock);
211
212 /**
213 * Creates an instance of the default event loop implementation for the current architecture and operating system using
214 * extendable options.
215 */
216 AWS_IO_API
217 struct aws_event_loop *aws_event_loop_new(struct aws_allocator *alloc, const struct aws_event_loop_options *options);
218
219 /**
220 * Initializes common event-loop data structures.
221 * This is only called from the *new() function of event loop implementations.
222 */
223 AWS_IO_API
224 int aws_event_loop_init_base(struct aws_event_loop *event_loop, struct aws_allocator *alloc, aws_io_clock_fn *clock);
225
226 /**
227 * Fetches an object from the event-loop's data store. Key will be taken as the memory address of the memory pointed to
228 * by key. This function is not thread safe and should be called inside the event-loop's thread.
229 */
230 AWS_IO_API
231 int aws_event_loop_fetch_local_object(
232 struct aws_event_loop *event_loop,
233 void *key,
234 struct aws_event_loop_local_object *obj);
235
236 /**
237 * Puts an item object the event-loop's data store. Key will be taken as the memory address of the memory pointed to by
238 * key. The lifetime of item must live until remove or a put item overrides it. This function is not thread safe and
239 * should be called inside the event-loop's thread.
240 */
241 AWS_IO_API
242 int aws_event_loop_put_local_object(struct aws_event_loop *event_loop, struct aws_event_loop_local_object *obj);
243
244 /**
245 * Removes an object from the event-loop's data store. Key will be taken as the memory address of the memory pointed to
246 * by key. If removed_item is not null, the removed item will be moved to it if it exists. Otherwise, the default
247 * deallocation strategy will be used. This function is not thread safe and should be called inside the event-loop's
248 * thread.
249 */
250 AWS_IO_API
251 int aws_event_loop_remove_local_object(
252 struct aws_event_loop *event_loop,
253 void *key,
254 struct aws_event_loop_local_object *removed_obj);
255
256 /**
257 * Triggers the running of the event loop. This function must not block. The event loop is not active until this
258 * function is invoked. This function can be called again on an event loop after calling aws_event_loop_stop() and
259 * aws_event_loop_wait_for_stop_completion().
260 */
261 AWS_IO_API
262 int aws_event_loop_run(struct aws_event_loop *event_loop);
263
264 /**
265 * Triggers the event loop to stop, but does not wait for the loop to stop completely.
266 * This function may be called from outside or inside the event loop thread. It is safe to call multiple times.
267 * This function is called from destroy().
268 *
269 * If you do not call destroy(), an event loop can be run again by calling stop(), wait_for_stop_completion(), run().
270 */
271 AWS_IO_API
272 int aws_event_loop_stop(struct aws_event_loop *event_loop);
273
274 /**
275 * For event-loop implementations to use for providing metrics info to the base event-loop. This enables the
276 * event-loop load balancer to take into account load when vending another event-loop to a caller.
277 *
278 * Call this function at the beginning of your event-loop tick: after wake-up, but before processing any IO or tasks.
279 */
280 AWS_IO_API
281 void aws_event_loop_register_tick_start(struct aws_event_loop *event_loop);
282
283 /**
284 * For event-loop implementations to use for providing metrics info to the base event-loop. This enables the
285 * event-loop load balancer to take into account load when vending another event-loop to a caller.
286 *
287 * Call this function at the end of your event-loop tick: after processing IO and tasks.
288 */
289 AWS_IO_API
290 void aws_event_loop_register_tick_end(struct aws_event_loop *event_loop);
291
292 /**
293 * Returns the current load factor (however that may be calculated). If the event-loop is not invoking
294 * aws_event_loop_register_tick_start() and aws_event_loop_register_tick_end(), this value will always be 0.
295 */
296 AWS_IO_API
297 size_t aws_event_loop_get_load_factor(struct aws_event_loop *event_loop);
298
299 /**
300 * Blocks until the event loop stops completely.
301 * If you want to call aws_event_loop_run() again, you must call this after aws_event_loop_stop().
302 * It is not safe to call this function from inside the event loop thread.
303 */
304 AWS_IO_API
305 int aws_event_loop_wait_for_stop_completion(struct aws_event_loop *event_loop);
306
307 /**
308 * Unsubscribes handle from event-loop notifications.
309 * This function is not thread safe and should be called inside the event-loop's thread.
310 *
311 * NOTE: if you are using io completion ports, this is a risky call. We use it in places, but only when we're certain
312 * there's no pending events. If you want to use it, it's your job to make sure you don't have pending events before
313 * calling it.
314 */
315 AWS_IO_API
316 int aws_event_loop_unsubscribe_from_io_events(struct aws_event_loop *event_loop, struct aws_io_handle *handle);
317
318 /**
319 * Cleans up resources (user_data) associated with the I/O eventing subsystem for a given handle. This should only
320 * ever be necessary in the case where you are cleaning up an event loop during shutdown and its thread has already
321 * been joined.
322 */
323 AWS_IO_API
324 void aws_event_loop_free_io_event_resources(struct aws_event_loop *event_loop, struct aws_io_handle *handle);
325
326 AWS_IO_API
327 struct aws_event_loop_group *aws_event_loop_group_new_internal(
328 struct aws_allocator *allocator,
329 const struct aws_event_loop_group_options *options,
330 aws_new_event_loop_fn *new_loop_fn,
331 void *new_loop_user_data);
332
333 AWS_EXTERN_C_END
334
335 AWS_POP_SANE_WARNING_LEVEL
336
337 #endif /* AWS_IO_EVENT_LOOP_IMPL_H */