Debian Codegraph @

crt/aws-c-io/include/aws/io/future.h

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #ifndef AWS_IO_FUTURE_H
7 #define AWS_IO_FUTURE_H
8
9 /*
10
11 --------------------------------------------------------------------------------
12 INTRO
13 --------------------------------------------------------------------------------
14 An aws_future is used to deliver the result of an asynchronous function.
15
16 When an async function is called, it creates a future and returns it to the caller.
17 When the async work is finished, it completes the future by setting an error or result value.
18 The caller waits until the future is done, checks for error, and then gets
19 the result if everything was OK. Typically, the caller waits by registering
20 a callback that the future invokes when it's done. There are many different
21 ways to register callbacks, depending on what thread you want it to fire on.
22
23 If result type T has a "destructor" (clean_up(), destroy(), or release() function),
24 then the future has set_result_by_move() and get_result_by_move() functions
25 that explicitly transfer ownership to and from the future.
26 If the future dies, and still "owns" the resource, it calls the destructor.
27 If T has no destructor, then the future has set_result() and get_result()
28 functions that simply copy T by value.
29
30 Macros are used to define a type-safe API for each result type T,
31 similar to C++ templates. This makes the API hard to browse, so functions
32 are documented in comments below.
33
34 --------------------------------------------------------------------------------
35 API (common to all aws_future<T>)
36 The set/get result functions are mildly different based on T's
37 destructor type, and are documented later.
38 --------------------------------------------------------------------------------
39
40 // Create a new future, with refcount of 1.
41 struct aws_future_T *aws_future_T_new(struct aws_allocator *alloc);
42
43 // Increment the refcount.
44 // You can pass NULL (has no effect).
45 // Returns the same pointer that was passed in.
46 struct aws_future_T *aws_future_T_acquire(struct aws_future_T *future);
47
48 // Decrement the refcount.
49 // You can pass NULL (has no effect).
50 // Always returns NULL.
51 struct aws_future_T *aws_future_T_release(struct aws_future_T *future);
52
53 // Set future as done, with an error_code.
54 // If the future is already done this call is ignored.
55 void aws_future_T_set_error(struct aws_future_T *future, int error_code);
56
57 // Return whether the future is done.
58 bool aws_future_T_is_done(const struct aws_future_T *future);
59
60 // Get the error-code of a completed future.
61 // If 0 is returned, then the future completed successfully,
62 // you may now get the result.
63 //
64 // WARNING: You MUST NOT call this until the future is done.
65 int aws_future_T_get_error(const struct aws_future_T *future);
66
67 // Register callback to be invoked when the future completes.
68 //
69 // If the future is already done, the callback runs synchronously on the calling thread.
70 // If the future isn't done yet, the callback is registered, and it
71 // will run synchronously on whatever thread completes the future.
72 //
73 // WARNING: You MUST NOT register more than one callback.
74 void aws_future_T_register_callback(struct aws_future_T *future, aws_future_callback_fn *on_done, void *user_data);
75
76 // If the future isn't done yet, then register the completion callback.
77 //
78 // Returns true if the callback was registered,
79 // or false if the future is already done.
80 //
81 // Use this when you can't risk the callback running synchronously.
82 // For example: If you're calling an async function repeatedly,
83 // and synchronous completion could lead to stack overflow due to recursion.
84 // Or if you are holding a non-recursive mutex, and the callback also
85 // needs the mutex, and an immediate callback would deadlock.
86 //
87 // WARNING: You MUST NOT register more than one callback.
88 bool aws_future_T_register_callback_if_not_done(
89 struct aws_future_T *future,
90 aws_future_callback_fn *on_done,
91 void *user_data);
92
93 // Register completion callback to run async on an event-loop thread.
94 //
95 // When the future completes, the callback is scheduled to run as an event-loop task.
96 //
97 // Use this when you want the callback to run on the event-loop's thread,
98 // or to ensure the callback runs async even if the future completed synchronously.
99 //
100 // WARNING: You MUST NOT register more than one callback.
101 void aws_future_T_register_event_loop_callback(
102 struct aws_future_T *future,
103 struct aws_event_loop *event_loop,
104 aws_future_callback_fn *on_done,
105 void *user_data);
106
107 // Register completion callback to run async on an aws_channel's thread.
108 //
109 // When the future completes, the callback is scheduled to run as a channel task.
110 //
111 // Use this when you want the callback to run on the channel's thread,
112 // or to ensure the callback runs async even if the future completed synchronously.
113 //
114 // WARNING: You MUST NOT register more than one callback.
115 void aws_future_T_register_channel_callback(
116 struct aws_future_T *future,
117 struct aws_channel *channel,
118 aws_future_callback_fn *on_done,
119 void *user_data);
120
121 // Wait (up to timeout_ns) for future to complete.
122 // Returns true if future completes in this time.
123 // This blocks the current thread. It is dangerous to use anywhere except
124 // the main thread of simple programs or tests.
125 bool aws_future_T_wait(struct aws_future_T *future, uint64_t timeout_ns);
126
127 --------------------------------------------------------------------------------
128 AWS_FUTURE_T_BY_VALUE set/get API
129 Where T is stored by value, with no destructor.
130 Use with simple types like bool, size_t, enums, etc.
131 --------------------------------------------------------------------------------
132
133 // Set the result.
134 //
135 // If the future is already done this call is ignored.
136 void aws_future_T_set_result(const struct aws_future_T *future, T result);
137
138 // Get the result of a completed future.
139 //
140 // WARNING: You MUST NOT call this until the future is done.
141 // WARNING: You MUST NOT call this unless get_error() returned 0.
142 T aws_future_T_get_result(const struct aws_future_T *future);
143
144 --------------------------------------------------------------------------------
145 AWS_FUTURE_T_BY_VALUE_WITH_CLEAN_UP set/get API
146 Where T is stored by value, with destructor like: void aws_T_clean_up(T*)
147 Use with types like aws_byte_buf.
148 --------------------------------------------------------------------------------
149
150 // Set the result, transferring ownership.
151 //
152 // The memory at `value_address` is memcpy'd into the future,
153 // and then zeroed out to help prevent accidental reuse.
154 // It is safe to call this multiple times. If the future is already done,
155 // the new result is destroyed instead of saved.
156 void aws_future_T_set_result_by_move(struct aws_future_T *future, T *value_address);
157
158 // Get the result, transferring ownership.
159 //
160 // WARNING: You MUST NOT call this until the future is done.
161 // WARNING: You MUST NOT call this unless get_error() returned 0.
162 // WARNING: You MUST NOT call this multiple times.
163 T aws_future_T_get_result_by_move(struct aws_future_T *future);
164
165 // Get the result, without transferring ownership.
166 //
167 // WARNING: You MUST NOT call this until the future is done.
168 // WARNING: You MUST NOT call this unless get_error() returned 0.
169 // WARNING: You MUST NOT call this multiple times.
170 T* aws_future_T_peek_result(const struct aws_future_T *future);
171
172 --------------------------------------------------------------------------------
173 AWS_FUTURE_T_POINTER_WITH_DESTROY set/get API
174 Where T is stored by pointer, with destructor like: void aws_T_destroy(T*)
175 Use with types like aws_string.
176 --------------------------------------------------------------------------------
177
178 // Set the result, transferring ownership.
179 //
180 // The value at `pointer_address` is copied into the future,
181 // and then set NULL to prevent accidental reuse.
182 // If the future is already done, this new result is destroyed instead of saved.
183 void aws_future_T_set_result_by_move(struct aws_future_T *future, T **pointer_address);
184
185 // Get the result, transferring ownership.
186 //
187 // WARNING: You MUST NOT call this until the future is done.
188 // WARNING: You MUST NOT call this unless get_error() returned 0.
189 // WARNING: You MUST NOT call this multiple times.
190 T* aws_future_T_get_result_by_move(struct aws_future_T *future);
191
192 // Get the result, without transferring ownership.
193 //
194 // WARNING: You MUST NOT call this until the future is done.
195 // WARNING: You MUST NOT call this unless get_error() returned 0.
196 // WARNING: You MUST NOT call this multiple times.
197 T* aws_future_T_peek_result(const struct aws_future_T *future);
198
199 --------------------------------------------------------------------------------
200 AWS_FUTURE_T_POINTER_WITH_RELEASE set/get API
201 Where T is stored by pointer, with destructor like: T* aws_T_release(T*)
202 Use with types like aws_http_message
203 --------------------------------------------------------------------------------
204
205 // Set the result, transferring ownership.
206 //
207 // The value at `pointer_address` is copied into the future,
208 // and then set NULL to prevent accidental reuse.
209 // If the future is already done, this new result is destroyed instead of saved.
210 void aws_future_T_set_result_by_move(struct aws_future_T *future, T **pointer_address);
211
212 // Get the result, transferring ownership.
213 //
214 // WARNING: You MUST NOT call this until the future is done.
215 // WARNING: You MUST NOT call this unless get_error() returned 0.
216 // WARNING: You MUST NOT call this multiple times.
217 T* aws_future_T_get_result_by_move(struct aws_future_T *future);
218
219 // Get the result, without transferring ownership.
220 //
221 // WARNING: You MUST NOT call this until the future is done.
222 // WARNING: You MUST NOT call this unless get_error() returned 0.
223 // WARNING: You MUST NOT call this multiple times.
224 T* aws_future_T_peek_result(const struct aws_future_T *future);
225
226 --------------------------------------------------------------------------------
227 AWS_FUTURE_T_POINTER set/get API
228 Where T is stored by pointer, with no destructor.
229 No known use cases, but it's here for completeness.
230 --------------------------------------------------------------------------------
231
232 // Set the result.
233 //
234 // If the future is already done this call is ignored.
235 void aws_future_T_set_result(const struct aws_future_T *future, T *result);
236
237 // Get the result of a completed future.
238 //
239 // WARNING: You MUST NOT call this until the future is done.
240 // WARNING: You MUST NOT call this unless get_error() returned 0.
241 T* aws_future_T_get_result(const struct aws_future_T *future);
242
243 --------------------------------------------------------------------------------
244 DEFINING NEW aws_future<T>
245 --------------------------------------------------------------------------------
246
247 Macros are used to define a type-safe API for each result type T, similar to C++ templates.
248
249 Use the _DECLARATION() macro in the same header where T's API is declared.
250 Use the _IMPLEMENTATION() macro in the corresponding .c file.
251 The macros can only be used once for each type T,
252 so it makes sense to put it where T is declared.
253 If T lives in a single .c file and has no header, you can simply use the
254 _IMPLEMENTATION() macro without a _DECLARATION().
255
256 You must pick the right macros for your particular T, based on whether T is stored
257 by value or by pointer, and which type of "destructor" function it has:
258
259 - AWS_FUTURE_T_BY_VALUE: A future that holds a simple T by value, that needs no destructor.
260 Use with types like bool, size_t, enums, etc.
261 Example:
262 AWS_FUTURE_T_BY_VALUE_DECLARATION(aws_future_size, size_t, AWS_IO_API)
263 AWS_FUTURE_T_BY_VALUE_IMPLEMENTATION(aws_future_size, size_t)
264
265 - AWS_FUTURE_T_BY_VALUE_WITH_CLEAN_UP: A future that holds T by value, with destructor like: void aws_T_clean_up(T*)
266 Use with types like aws_byte_buf.
267 Example:
268 AWS_FUTURE_T_BY_VALUE_WITH_CLEAN_UP_DECLARATION(aws_future_byte_buf, aws_byte_buf, AWS_IO_API)
269 AWS_FUTURE_T_BY_VALUE_WITH_CLEAN_UP_IMPLEMENTATION(aws_future_byte_buf, aws_byte_buf,
270 aws_byte_buf_clean_up);
271 - AWS_FUTURE_T_POINTER_WITH_DESTROY: A future that holds T*, with destructor like: void aws_T_destroy(T*)
272 Use with types like aws_string.
273 Example:
274 AWS_FUTURE_T_POINTER_WITH_DESTROY_DECLARATION(aws_future_string, aws_string, AWS_IO_API)
275 AWS_FUTURE_T_POINTER_WITH_DESTROY_DECLARATION(aws_future_string, aws_string, AWS_IO_API)
276
277 - AWS_FUTURE_T_POINTER_WITH_RELEASE: A future that holds T*, with destructor like: T* aws_T_release(T*)
278 Use with types like aws_http_message
279 Example:
280 AWS_FUTURE_T_POINTER_WITH_RELEASE_DECLARATION(aws_future_http_message, struct aws_http_message,
281 AWS_HTTP_API)
282 AWS_FUTURE_T_POINTER_WITH_RELEASE_IMPLEMENTATION(aws_future_http_message, struct aws_http_message,
283 aws_http_message_release)
284
285 - AWS_FUTURE_T_POINTER: A future that holds T*, with no destructor.
286 No known use cases, but it's here for completeness.
287 Example:
288 AWS_FUTURE_T_POINTER_DECLARATION(aws_future_file. FILE, AWS_IO_API)
289 AWS_FUTURE_T_POINTER_IMPLEMENTATION(aws_future_file. FILE)
290
291 --------------------------------------------------------------------------------
292 DESIGN (if you're curious)
293 --------------------------------------------------------------------------------
294 This class was developed to give the user more control over how the completion
295 callback is invoked. In the past, we passed completion callbacks to the async
296 function. But this could lead to issues when an async function "sometimes"
297 completed synchronously and "sometimes" completed async. The async function
298 would need to stress about how to schedule the callback so it was always async,
299 or more typically just invoke it whenever and leave the caller to figure it out.
300
301 This class is also an experiment with "templates/generics in C".
302 In order to make the class type-safe, we use macros to define a unique
303 API for each result type T we need to store in a future.
304 If we refer to aws_future<struct aws_byte_buf>, we mean a struct named
305 aws_future_byte_buf, which stores an aws_byte_buf by value.
306 This could lead to code bloat, but the type-safety seems worth it.
307
308 future is defined in aws-c-io, instead of aws-c-common, so it can
309 easily integrate with aws_event_loop and aws_channel.
310
311 It's legal to call set_error() or set_result() multiple times.
312 If the future is already done, it ignores the call.
313 If result T has a destructor, the new result is immediately freed instead of saved.
314 This design lets us deal with ambiguity where it's not 100% certain whether a handoff occurred.
315 For example: if we call from C->Java and an exception is thrown,
316 it's not clear whether Java got the handoff. In this case, we can safely
317 call set_error(), completing the future if necessary,
318 or being ignored if the future was already done.
319
320 */
321
322 #include <aws/io/io.h>
323
324 AWS_PUSH_SANE_WARNING_LEVEL
325
326 struct aws_channel;
327 struct aws_event_loop;
328 struct aws_future_impl;
329
330 /** Completion callback for aws_future<T> */
331 typedef void(aws_future_callback_fn)(void *user_data);
332
333 typedef void(aws_future_impl_result_clean_up_fn)(void *result_addr);
334 typedef void(aws_future_impl_result_destroy_fn)(void *result);
335 typedef void *(aws_future_impl_result_release_fn)(void *result);
336
337 AWS_EXTERN_C_BEGIN
338
339 AWS_IO_API
340 struct aws_future_impl *aws_future_impl_new_by_value(struct aws_allocator *alloc, size_t sizeof_result);
341
342 AWS_IO_API
343 struct aws_future_impl *aws_future_impl_new_by_value_with_clean_up(
344 struct aws_allocator *alloc,
345 size_t sizeof_result,
346 aws_future_impl_result_clean_up_fn *result_clean_up);
347
348 AWS_IO_API
349 struct aws_future_impl *aws_future_impl_new_pointer(struct aws_allocator *alloc);
350
351 AWS_IO_API
352 struct aws_future_impl *aws_future_impl_new_pointer_with_destroy(
353 struct aws_allocator *alloc,
354 aws_future_impl_result_destroy_fn *result_destroy);
355
356 AWS_IO_API
357 struct aws_future_impl *aws_future_impl_new_pointer_with_release(
358 struct aws_allocator *alloc,
359 aws_future_impl_result_release_fn *result_release);
360
361 AWS_IO_API
362 struct aws_future_impl *aws_future_impl_release(struct aws_future_impl *promise);
363
364 AWS_IO_API
365 struct aws_future_impl *aws_future_impl_acquire(struct aws_future_impl *promise);
366
367 AWS_IO_API
368 void aws_future_impl_set_error(struct aws_future_impl *promise, int error_code);
369
370 AWS_IO_API
371 void aws_future_impl_set_result_by_move(struct aws_future_impl *promise, void *src_address);
372
373 AWS_IO_API
374 bool aws_future_impl_is_done(const struct aws_future_impl *future);
375
376 AWS_IO_API
377 void aws_future_impl_register_callback(
378 struct aws_future_impl *future,
379 aws_future_callback_fn *on_done,
380 void *user_data);
381
382 AWS_IO_API
383 bool aws_future_impl_register_callback_if_not_done(
384 struct aws_future_impl *future,
385 aws_future_callback_fn *on_done,
386 void *user_data);
387
388 AWS_IO_API
389 void aws_future_impl_register_event_loop_callback(
390 struct aws_future_impl *future,
391 struct aws_event_loop *event_loop,
392 aws_future_callback_fn *on_done,
393 void *user_data);
394
395 AWS_IO_API
396 void aws_future_impl_register_channel_callback(
397 struct aws_future_impl *future,
398 struct aws_channel *channel,
399 aws_future_callback_fn *on_done,
400 void *user_data);
401
402 AWS_IO_API
403 bool aws_future_impl_wait(const struct aws_future_impl *future, uint64_t timeout_ns);
404
405 AWS_IO_API
406 int aws_future_impl_get_error(const struct aws_future_impl *future);
407
408 AWS_IO_API
409 void *aws_future_impl_get_result_address(const struct aws_future_impl *future);
410
411 AWS_IO_API
412 void aws_future_impl_get_result_by_move(struct aws_future_impl *future, void *dst_address);
413
414 /* Common beginning to all aws_future<T> declarations */
415 #define AWS_FUTURE_T_DECLARATION_BEGIN(FUTURE, API) struct FUTURE;
416
417 /* Common beginning to all aws_future<T> implementations */
418 #define AWS_FUTURE_T_IMPLEMENTATION_BEGIN(FUTURE)
419
420 /* Common end to all aws_future<T> declarations */
421 #define AWS_FUTURE_T_DECLARATION_END(FUTURE, API) \
422 API struct FUTURE *FUTURE##_acquire(struct FUTURE *future); \
423 API struct FUTURE *FUTURE##_release(struct FUTURE *future); \
424 API void FUTURE##_set_error(struct FUTURE *future, int error_code); \
425 API bool FUTURE##_is_done(const struct FUTURE *future); \
426 API int FUTURE##_get_error(const struct FUTURE *future); \
427 API void FUTURE##_register_callback(struct FUTURE *future, aws_future_callback_fn *on_done, void *user_data); \
428 API bool FUTURE##_register_callback_if_not_done( \
429 struct FUTURE *future, aws_future_callback_fn *on_done, void *user_data); \
430 API void FUTURE##_register_event_loop_callback( \
431 struct FUTURE *future, struct aws_event_loop *event_loop, aws_future_callback_fn *on_done, void *user_data); \
432 API void FUTURE##_register_channel_callback( \
433 struct FUTURE *future, struct aws_channel *channel, aws_future_callback_fn *on_done, void *user_data); \
434 API bool FUTURE##_wait(struct FUTURE *future, uint64_t timeout_ns);
435
436 /* Common end to all aws_future<T> implementations */
437 #define AWS_FUTURE_T_IMPLEMENTATION_END(FUTURE) \
438 struct FUTURE *FUTURE##_acquire(struct FUTURE *future) { \
439 return (struct FUTURE *)aws_future_impl_acquire((struct aws_future_impl *)future); \
440 } \
441 \
442 struct FUTURE *FUTURE##_release(struct FUTURE *future) { \
443 return (struct FUTURE *)aws_future_impl_release((struct aws_future_impl *)future); \
444 } \
445 \
446 void FUTURE##_set_error(struct FUTURE *future, int error_code) { \
447 aws_future_impl_set_error((struct aws_future_impl *)future, error_code); \
448 } \
449 \
450 bool FUTURE##_is_done(const struct FUTURE *future) { \
451 return aws_future_impl_is_done((const struct aws_future_impl *)future); \
452 } \
453 \
454 int FUTURE##_get_error(const struct FUTURE *future) { \
455 return aws_future_impl_get_error((const struct aws_future_impl *)future); \
456 } \
457 \
458 void FUTURE##_register_callback(struct FUTURE *future, aws_future_callback_fn *on_done, void *user_data) { \
459 aws_future_impl_register_callback((struct aws_future_impl *)future, on_done, user_data); \
460 } \
461 \
462 bool FUTURE##_register_callback_if_not_done( \
463 struct FUTURE *future, aws_future_callback_fn *on_done, void *user_data) { \
464 \
465 return aws_future_impl_register_callback_if_not_done((struct aws_future_impl *)future, on_done, user_data); \
466 } \
467 \
468 void FUTURE##_register_event_loop_callback( \
469 struct FUTURE *future, struct aws_event_loop *event_loop, aws_future_callback_fn *on_done, void *user_data) { \
470 \
471 aws_future_impl_register_event_loop_callback( \
472 (struct aws_future_impl *)future, event_loop, on_done, user_data); \
473 } \
474 \
475 void FUTURE##_register_channel_callback( \
476 struct FUTURE *future, struct aws_channel *channel, aws_future_callback_fn *on_done, void *user_data) { \
477 \
478 aws_future_impl_register_channel_callback((struct aws_future_impl *)future, channel, on_done, user_data); \
479 } \
480 \
481 bool FUTURE##_wait(struct FUTURE *future, uint64_t timeout_ns) { \
482 return aws_future_impl_wait((struct aws_future_impl *)future, timeout_ns); \
483 }
484
485 /**
486 * Declare a future that holds a simple T by value, that needs no destructor.
487 * Use with types like bool, size_t, etc.
488 *
489 * See top of future.h for docs.
490 */
491 #define AWS_FUTURE_T_BY_VALUE_DECLARATION(FUTURE, T, API) \
492 AWS_FUTURE_T_DECLARATION_BEGIN(FUTURE, API) \
493 API struct FUTURE *FUTURE##_new(struct aws_allocator *alloc); \
494 API void FUTURE##_set_result(struct FUTURE *future, T result); \
495 API T FUTURE##_get_result(const struct FUTURE *future); \
496 AWS_FUTURE_T_DECLARATION_END(FUTURE, API)
497
498 #define AWS_FUTURE_T_BY_VALUE_IMPLEMENTATION(FUTURE, T) \
499 AWS_FUTURE_T_IMPLEMENTATION_BEGIN(FUTURE) \
500 struct FUTURE *FUTURE##_new(struct aws_allocator *alloc) { \
501 return (struct FUTURE *)aws_future_impl_new_by_value(alloc, sizeof(T)); \
502 } \
503 \
504 void FUTURE##_set_result(struct FUTURE *future, T result) { \
505 aws_future_impl_set_result_by_move((struct aws_future_impl *)future, &result); \
506 } \
507 \
508 T FUTURE##_get_result(const struct FUTURE *future) { \
509 return *(T *)aws_future_impl_get_result_address((const struct aws_future_impl *)future); \
510 } \
511 AWS_FUTURE_T_IMPLEMENTATION_END(FUTURE)
512
513 /**
514 * Declares a future that holds T by value, with destructor like: void aws_T_clean_up(T*)
515 * Use with types like aws_byte_buf.
516 *
517 * See top of future.h for docs.
518 */
519 #define AWS_FUTURE_T_BY_VALUE_WITH_CLEAN_UP_DECLARATION(FUTURE, T, API) \
520 AWS_FUTURE_T_DECLARATION_BEGIN(FUTURE, API) \
521 API struct FUTURE *FUTURE##_new(struct aws_allocator *alloc); \
522 API void FUTURE##_set_result_by_move(struct FUTURE *future, T *value_address); \
523 API T *FUTURE##_peek_result(const struct FUTURE *future); \
524 API T FUTURE##_get_result_by_move(struct FUTURE *future); \
525 AWS_FUTURE_T_DECLARATION_END(FUTURE, API)
526
527 #define AWS_FUTURE_T_BY_VALUE_WITH_CLEAN_UP_IMPLEMENTATION(FUTURE, T, CLEAN_UP_FN) \
528 AWS_FUTURE_T_IMPLEMENTATION_BEGIN(FUTURE) \
529 \
530 struct FUTURE *FUTURE##_new(struct aws_allocator *alloc) { \
531 void (*clean_up_fn)(T *) = CLEAN_UP_FN; /* check clean_up() function signature */ \
532 return (struct FUTURE *)aws_future_impl_new_by_value_with_clean_up( \
533 alloc, sizeof(T), (aws_future_impl_result_clean_up_fn)clean_up_fn); \
534 } \
535 \
536 void FUTURE##_set_result_by_move(struct FUTURE *future, T *value_address) { \
537 aws_future_impl_set_result_by_move((struct aws_future_impl *)future, value_address); \
538 } \
539 \
540 T *FUTURE##_peek_result(const struct FUTURE *future) { \
541 return aws_future_impl_get_result_address((const struct aws_future_impl *)future); \
542 } \
543 \
544 T FUTURE##_get_result_by_move(struct FUTURE *future) { \
545 T value; \
546 aws_future_impl_get_result_by_move((struct aws_future_impl *)future, &value); \
547 return value; \
548 } \
549 \
550 AWS_FUTURE_T_IMPLEMENTATION_END(FUTURE)
551
552 /**
553 * Declares a future that holds T*, with no destructor.
554 *
555 * See top of future.h for docs.
556 */
557 #define AWS_FUTURE_T_POINTER_DECLARATION(FUTURE, T, API) \
558 AWS_FUTURE_T_DECLARATION_BEGIN(FUTURE, API) \
559 API struct FUTURE *FUTURE##_new(struct aws_allocator *alloc); \
560 API void FUTURE##_set_result(struct FUTURE *future, T *result); \
561 API T *FUTURE##_get_result(const struct FUTURE *future); \
562 AWS_FUTURE_T_DECLARATION_END(FUTURE, API)
563
564 #define AWS_FUTURE_T_POINTER_IMPLEMENTATION(FUTURE, T) \
565 AWS_FUTURE_T_IMPLEMENTATION_BEGIN(FUTURE) \
566 \
567 struct FUTURE *FUTURE##_new(struct aws_allocator *alloc) { \
568 return (struct FUTURE *)aws_future_impl_new_pointer(alloc); \
569 } \
570 \
571 void FUTURE##_set_result(struct FUTURE *future, T *result) { \
572 aws_future_impl_set_result_by_move((struct aws_future_impl *)future, &result); \
573 } \
574 \
575 T *FUTURE##_get_result(const struct FUTURE *future) { \
576 return *(T **)aws_future_impl_get_result_address((const struct aws_future_impl *)future); \
577 } \
578 \
579 AWS_FUTURE_T_IMPLEMENTATION_END(FUTURE)
580
581 /**
582 * Declares a future that holds T*, with destructor like: void aws_T_destroy(T*)
583 * Use with types like aws_string.
584 *
585 * See top of future.h for docs.
586 */
587 #define AWS_FUTURE_T_POINTER_WITH_DESTROY_DECLARATION(FUTURE, T, API) \
588 AWS_FUTURE_T_DECLARATION_BEGIN(FUTURE, API) \
589 API struct FUTURE *FUTURE##_new(struct aws_allocator *alloc); \
590 API void FUTURE##_set_result_by_move(struct FUTURE *future, T **pointer_address); \
591 API T *FUTURE##_get_result_by_move(struct FUTURE *future); \
592 API T *FUTURE##_peek_result(const struct FUTURE *future); \
593 AWS_FUTURE_T_DECLARATION_END(FUTURE, API)
594
595 #define AWS_FUTURE_T_POINTER_WITH_DESTROY_IMPLEMENTATION(FUTURE, T, DESTROY_FN) \
596 AWS_FUTURE_T_IMPLEMENTATION_BEGIN(FUTURE) \
597 \
598 struct FUTURE *FUTURE##_new(struct aws_allocator *alloc) { \
599 void (*destroy_fn)(T *) = DESTROY_FN; /* check destroy() function signature */ \
600 return (struct FUTURE *)aws_future_impl_new_pointer_with_destroy( \
601 alloc, (aws_future_impl_result_destroy_fn *)destroy_fn); \
602 } \
603 \
604 void FUTURE##_set_result_by_move(struct FUTURE *future, T **pointer_address) { \
605 aws_future_impl_set_result_by_move((struct aws_future_impl *)future, pointer_address); \
606 } \
607 \
608 T *FUTURE##_get_result_by_move(struct FUTURE *future) { \
609 T *pointer; \
610 aws_future_impl_get_result_by_move((struct aws_future_impl *)future, &pointer); \
611 return pointer; \
612 } \
613 \
614 T *FUTURE##_peek_result(const struct FUTURE *future) { \
615 return *(T **)aws_future_impl_get_result_address((const struct aws_future_impl *)future); \
616 } \
617 \
618 AWS_FUTURE_T_IMPLEMENTATION_END(FUTURE)
619
620 /**
621 * Declares a future that holds T*, with destructor like: T* aws_T_release(T*)
622 * Use with types like aws_http_message
623 *
624 * See top of future.h for docs.
625 */
626 #define AWS_FUTURE_T_POINTER_WITH_RELEASE_DECLARATION(FUTURE, T, API) \
627 AWS_FUTURE_T_DECLARATION_BEGIN(FUTURE, API) \
628 API struct FUTURE *FUTURE##_new(struct aws_allocator *alloc); \
629 API void FUTURE##_set_result_by_move(struct FUTURE *future, T **pointer_address); \
630 API T *FUTURE##_get_result_by_move(struct FUTURE *future); \
631 API T *FUTURE##_peek_result(const struct FUTURE *future); \
632 AWS_FUTURE_T_DECLARATION_END(FUTURE, API)
633
634 #define AWS_FUTURE_T_POINTER_WITH_RELEASE_IMPLEMENTATION(FUTURE, T, RELEASE_FN) \
635 AWS_FUTURE_T_IMPLEMENTATION_BEGIN(FUTURE) \
636 \
637 struct FUTURE *FUTURE##_new(struct aws_allocator *alloc) { \
638 T *(*release_fn)(T *) = RELEASE_FN; /* check release() function signature */ \
639 return (struct FUTURE *)aws_future_impl_new_pointer_with_release( \
640 alloc, (aws_future_impl_result_release_fn *)release_fn); \
641 } \
642 \
643 void FUTURE##_set_result_by_move(struct FUTURE *future, T **pointer_address) { \
644 aws_future_impl_set_result_by_move((struct aws_future_impl *)future, pointer_address); \
645 } \
646 \
647 T *FUTURE##_get_result_by_move(struct FUTURE *future) { \
648 T *pointer; \
649 aws_future_impl_get_result_by_move((struct aws_future_impl *)future, &pointer); \
650 return pointer; \
651 } \
652 \
653 T *FUTURE##_peek_result(const struct FUTURE *future) { \
654 return *(T **)aws_future_impl_get_result_address((const struct aws_future_impl *)future); \
655 } \
656 \
657 AWS_FUTURE_T_IMPLEMENTATION_END(FUTURE)
658
659 /**
660 * aws_future<size_t>
661 * See top of future.h for docs.
662 */
663 AWS_FUTURE_T_BY_VALUE_DECLARATION(aws_future_size, size_t, AWS_IO_API)
664
665 /**
666 * aws_future<bool>
667 * See top of future.h for docs.
668 */
669 AWS_FUTURE_T_BY_VALUE_DECLARATION(aws_future_bool, bool, AWS_IO_API)
670
671 /**
672 * aws_future<void>
673 * See top of future.h for docs.
674 */
675 AWS_FUTURE_T_DECLARATION_BEGIN(aws_future_void, AWS_IO_API)
676
677 AWS_IO_API struct aws_future_void *aws_future_void_new(struct aws_allocator *alloc);
678
679 AWS_IO_API void aws_future_void_set_result(struct aws_future_void *future);
680
681 AWS_FUTURE_T_DECLARATION_END(aws_future_void, AWS_IO_API)
682
683 AWS_EXTERN_C_END
684 AWS_POP_SANE_WARNING_LEVEL
685
686 #endif /* AWS_IO_FUTURE_H */