Debian Codegraph @

crt/aws-c-io/source/future.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/io/future.h>
6
7 #include <aws/common/condition_variable.h>
8 #include <aws/common/mutex.h>
9 #include <aws/common/ref_count.h>
10 #include <aws/common/task_scheduler.h>
11 #include <aws/io/channel.h>
12 #include <aws/io/event_loop.h>
13
14 enum aws_future_type {
15 AWS_FUTURE_T_BY_VALUE,
16 AWS_FUTURE_T_BY_VALUE_WITH_CLEAN_UP,
17 AWS_FUTURE_T_POINTER,
18 AWS_FUTURE_T_POINTER_WITH_DESTROY,
19 AWS_FUTURE_T_POINTER_WITH_RELEASE,
20 };
21
22 struct aws_future_callback_data {
23 aws_future_callback_fn *fn;
24 void *user_data;
25 union aws_future_callback_union {
26 struct aws_event_loop *event_loop;
27 struct aws_channel *channel;
28 } u;
29 enum aws_future_callback_type {
30 AWS_FUTURE_IMMEDIATE_CALLBACK,
31 AWS_FUTURE_EVENT_LOOP_CALLBACK,
32 AWS_FUTURE_CHANNEL_CALLBACK,
33 } type;
34 };
35
36 /* When allocating aws_future<T> on the heap, we make 1 allocation containing:
37 * aws_future_impl followed by T */
38 struct aws_future_impl {
39 struct aws_allocator *alloc;
40 struct aws_ref_count ref_count;
41 struct aws_mutex lock;
42 struct aws_condition_variable wait_cvar;
43 struct aws_future_callback_data callback;
44 union {
45 aws_future_impl_result_clean_up_fn *clean_up;
46 aws_future_impl_result_destroy_fn *destroy;
47 aws_future_impl_result_release_fn *release;
48 } result_dtor;
49 int error_code;
50
51 size_t sizeof_result;
52 enum aws_future_type type;
53 bool is_done;
54 bool owns_result;
55 };
56
57 static void s_future_impl_result_dtor(struct aws_future_impl *future, void *result_addr) {
58
59 /*
60 * On ARM machines, the compiler complains about the array bounds warning for aws_future_bool, even though
61 * aws_future_bool will never go into any destroy or release branch. Disable the warning since it's a false positive.
62 */
63 #ifndef _MSC_VER
64 # pragma GCC diagnostic push
65 # pragma GCC diagnostic ignored "-Warray-bounds"
66 #endif
67 switch (future->type) {
68 case AWS_FUTURE_T_BY_VALUE_WITH_CLEAN_UP: {
69 future->result_dtor.clean_up(result_addr);
70 break;
71 } break;
72
73 case AWS_FUTURE_T_POINTER_WITH_DESTROY: {
74 void *result = *(void **)result_addr;
75 if (result) {
76 future->result_dtor.destroy(result);
77 }
78 } break;
79
80 case AWS_FUTURE_T_POINTER_WITH_RELEASE: {
81 void *result = *(void **)result_addr;
82 if (result) {
83 future->result_dtor.release(result);
84 }
85 } break;
86
87 default:
88 break;
89 }
90 #ifndef _MSC_VER
91 # pragma GCC diagnostic pop
92 #endif
93 }
94
95 static void s_future_impl_destroy(void *user_data) {
96 struct aws_future_impl *future = user_data;
97 if (future->owns_result && !future->error_code) {
98 s_future_impl_result_dtor(future, aws_future_impl_get_result_address(future));
99 }
100 aws_condition_variable_clean_up(&future->wait_cvar);
101 aws_mutex_clean_up(&future->lock);
102 aws_mem_release(future->alloc, future);
103 }
104
105 static struct aws_future_impl *s_future_impl_new(struct aws_allocator *alloc, size_t sizeof_result) {
106 size_t total_size = sizeof(struct aws_future_impl) + sizeof_result;
107 struct aws_future_impl *future = aws_mem_calloc(alloc, 1, total_size);
108 future->alloc = alloc;
109
110 future->sizeof_result = sizeof_result;
111
112 aws_ref_count_init(&future->ref_count, future, s_future_impl_destroy);
113 aws_mutex_init(&future->lock);
114 aws_condition_variable_init(&future->wait_cvar);
115 return future;
116 }
117
118 struct aws_future_impl *aws_future_impl_new_by_value(struct aws_allocator *alloc, size_t sizeof_result) {
119 struct aws_future_impl *future = s_future_impl_new(alloc, sizeof_result);
120 future->type = AWS_FUTURE_T_BY_VALUE;
121 return future;
122 }
123
124 struct aws_future_impl *aws_future_impl_new_by_value_with_clean_up(
125 struct aws_allocator *alloc,
126 size_t sizeof_result,
127 aws_future_impl_result_clean_up_fn *result_clean_up) {
128
129 AWS_ASSERT(result_clean_up);
130 struct aws_future_impl *future = s_future_impl_new(alloc, sizeof_result);
131 future->type = AWS_FUTURE_T_BY_VALUE_WITH_CLEAN_UP;
132 future->result_dtor.clean_up = result_clean_up;
133 return future;
134 }
135
136 struct aws_future_impl *aws_future_impl_new_pointer(struct aws_allocator *alloc) {
137 struct aws_future_impl *future = s_future_impl_new(alloc, sizeof(void *));
138 future->type = AWS_FUTURE_T_POINTER;
139 return future;
140 }
141
142 struct aws_future_impl *aws_future_impl_new_pointer_with_destroy(
143 struct aws_allocator *alloc,
144 aws_future_impl_result_destroy_fn *result_destroy) {
145
146 AWS_ASSERT(result_destroy);
147 struct aws_future_impl *future = s_future_impl_new(alloc, sizeof(void *));
148 future->type = AWS_FUTURE_T_POINTER_WITH_DESTROY;
149 future->result_dtor.destroy = result_destroy;
150 return future;
151 }
152
153 struct aws_future_impl *aws_future_impl_new_pointer_with_release(
154 struct aws_allocator *alloc,
155 aws_future_impl_result_release_fn *result_release) {
156
157 AWS_ASSERT(result_release);
158 struct aws_future_impl *future = s_future_impl_new(alloc, sizeof(void *));
159 future->type = AWS_FUTURE_T_POINTER_WITH_RELEASE;
160 future->result_dtor.release = result_release;
161 return future;
162 }
163
164 struct aws_future_impl *aws_future_impl_release(struct aws_future_impl *future) {
165 if (future != NULL) {
166 aws_ref_count_release(&future->ref_count);
167 }
168 return NULL;
169 }
170
171 struct aws_future_impl *aws_future_impl_acquire(struct aws_future_impl *future) {
172 if (future != NULL) {
173 aws_ref_count_acquire(&future->ref_count);
174 }
175 return future;
176 }
177
178 bool aws_future_impl_is_done(const struct aws_future_impl *future) {
179 AWS_ASSERT(future);
180
181 /* this function is conceptually const, but we need to hold the lock a moment */
182 struct aws_mutex *mutable_lock = (struct aws_mutex *)&future->lock;
183
184 /* BEGIN CRITICAL SECTION */
185 aws_mutex_lock(mutable_lock);
186 bool is_done = future->is_done;
187 aws_mutex_unlock(mutable_lock);
188 /* END CRITICAL SECTION */
189
190 return is_done;
191 }
192
193 int aws_future_impl_get_error(const struct aws_future_impl *future) {
194 AWS_ASSERT(future != NULL);
195 /* not bothering with lock, none of this can change after future is done */
196 AWS_FATAL_ASSERT(future->is_done && "Cannot get error before future is done");
197 return future->error_code;
198 }
199
200 void *aws_future_impl_get_result_address(const struct aws_future_impl *future) {
201 AWS_ASSERT(future != NULL);
202 /* not bothering with lock, none of this can change after future is done */
203 AWS_FATAL_ASSERT(future->is_done && "Cannot get result before future is done");
204 AWS_FATAL_ASSERT(!future->error_code && "Cannot get result from future that failed with an error");
205 AWS_FATAL_ASSERT(future->owns_result && "Result was already moved from future");
206
207 const struct aws_future_impl *address_of_memory_after_this_struct = future + 1;
208 void *result_addr = (void *)address_of_memory_after_this_struct;
209 return result_addr;
210 }
211
212 void aws_future_impl_get_result_by_move(struct aws_future_impl *future, void *dst_address) {
213 void *result_addr = aws_future_impl_get_result_address(future);
214 memcpy(dst_address, result_addr, future->sizeof_result);
215 memset(result_addr, 0, future->sizeof_result);
216 future->owns_result = false;
217 }
218
219 /* Data for invoking callback as a task on an event-loop */
220 struct aws_future_event_loop_callback_job {
221 struct aws_allocator *alloc;
222 struct aws_task task;
223 struct aws_event_loop *event_loop;
224 aws_future_callback_fn *callback;
225 void *user_data;
226 };
227
228 static void s_future_impl_event_loop_callback_task(struct aws_task *task, void *arg, enum aws_task_status status) {
229 (void)task;
230 (void)status;
231 struct aws_future_event_loop_callback_job *job = arg;
232 job->callback(job->user_data);
233 // TODO: aws_event_loop_release(job->event_loop);
234 aws_mem_release(job->alloc, job);
235 }
236
237 /* Data for invoking callback as a task on an aws_channel */
238 struct aws_future_channel_callback_job {
239 struct aws_allocator *alloc;
240 struct aws_channel_task task;
241 struct aws_channel *channel;
242 aws_future_callback_fn *callback;
243 void *user_data;
244 };
245
246 static void s_future_impl_channel_callback_task(struct aws_channel_task *task, void *arg, enum aws_task_status status) {
247 (void)task;
248 (void)status;
249 struct aws_future_channel_callback_job *job = arg;
250 job->callback(job->user_data);
251 aws_channel_release_hold(job->channel);
252 aws_mem_release(job->alloc, job);
253 }
254
255 static void s_future_impl_invoke_callback(struct aws_future_callback_data *callback, struct aws_allocator *alloc) {
256 AWS_ASSERT(callback->fn);
257
258 switch (callback->type) {
259 case AWS_FUTURE_IMMEDIATE_CALLBACK: {
260 callback->fn(callback->user_data);
261 } break;
262
263 case AWS_FUTURE_EVENT_LOOP_CALLBACK: {
264 /* Schedule the callback as a task on the event-loop */
265 struct aws_future_event_loop_callback_job *job =
266 aws_mem_calloc(alloc, 1, sizeof(struct aws_future_event_loop_callback_job));
267 job->alloc = alloc;
268 aws_task_init(&job->task, s_future_impl_event_loop_callback_task, job, "aws_future_event_loop_callback");
269 job->event_loop = callback->u.event_loop;
270 job->callback = callback->fn;
271 job->user_data = callback->user_data;
272
273 aws_event_loop_schedule_task_now(callback->u.event_loop, &job->task);
274 } break;
275
276 case AWS_FUTURE_CHANNEL_CALLBACK: {
277 /* Schedule the callback as a task on the channel */
278 struct aws_future_channel_callback_job *job =
279 aws_mem_calloc(alloc, 1, sizeof(struct aws_future_channel_callback_job));
280 job->alloc = alloc;
281 aws_channel_task_init(&job->task, s_future_impl_channel_callback_task, job, "aws_future_channel_callback");
282 job->channel = callback->u.channel;
283 job->callback = callback->fn;
284 job->user_data = callback->user_data;
285
286 aws_channel_schedule_task_now(callback->u.channel, &job->task);
287 } break;
288 }
289 }
290
291 static void s_future_impl_set_done(struct aws_future_impl *future, void *src_address, int error_code) {
292 bool is_error = error_code != 0;
293
294 /* BEGIN CRITICAL SECTION */
295 aws_mutex_lock(&future->lock);
296
297 struct aws_future_callback_data callback = future->callback;
298
299 bool first_time = !future->is_done;
300 if (first_time) {
301 future->is_done = true;
302 AWS_ZERO_STRUCT(future->callback);
303 if (is_error) {
304 future->error_code = error_code;
305 } else {
306 future->owns_result = true;
307 AWS_FATAL_ASSERT(src_address != NULL);
308 memcpy(aws_future_impl_get_result_address(future), src_address, future->sizeof_result);
309 }
310
311 aws_condition_variable_notify_all(&future->wait_cvar);
312 }
313
314 aws_mutex_unlock(&future->lock);
315 /* END CRITICAL SECTION */
316
317 if (first_time) {
318 /* if callback was registered, invoke it now, outside of critical section to avoid deadlock */
319 if (callback.fn != NULL) {
320 s_future_impl_invoke_callback(&callback, future->alloc);
321 }
322 } else if (!error_code) {
323 /* future was already done, so just destroy this newer result */
324 s_future_impl_result_dtor(future, src_address);
325 }
326 }
327
328 void aws_future_impl_set_error(struct aws_future_impl *future, int error_code) {
329 AWS_ASSERT(future);
330
331 /* handle recoverable usage error */
332 AWS_ASSERT(error_code != 0);
333 if (AWS_UNLIKELY(error_code == 0)) {
334 error_code = AWS_ERROR_UNKNOWN;
335 }
336
337 s_future_impl_set_done(future, NULL /*src_address*/, error_code);
338 }
339
340 void aws_future_impl_set_result_by_move(struct aws_future_impl *future, void *src_address) {
341 AWS_ASSERT(future);
342 AWS_ASSERT(src_address);
343 s_future_impl_set_done(future, src_address, 0 /*error_code*/);
344
345 /* the future takes ownership of the result.
346 * zero out memory at the src_address to reinforce this transfer of ownership. */
347 memset(src_address, 0, future->sizeof_result);
348 }
349
350 /* Returns true if callback was registered, or false if callback was ignored
351 * because the the future is already done and invoke_if_already_done==false */
352 static bool s_future_impl_register_callback(
353 struct aws_future_impl *future,
354 struct aws_future_callback_data *callback,
355 bool invoke_if_already_done) {
356
357 /* BEGIN CRITICAL SECTION */
358 aws_mutex_lock(&future->lock);
359
360 AWS_FATAL_ASSERT(future->callback.fn == NULL && "Future done callback must only be set once");
361
362 bool already_done = future->is_done;
363
364 /* if not done, store callback for later */
365 if (!already_done) {
366 future->callback = *callback;
367 }
368
369 aws_mutex_unlock(&future->lock);
370 /* END CRITICAL SECTION */
371
372 /* if already done, invoke callback now */
373 if (already_done && invoke_if_already_done) {
374 s_future_impl_invoke_callback(callback, future->alloc);
375 }
376
377 return !already_done || invoke_if_already_done;
378 }
379
380 void aws_future_impl_register_callback(
381 struct aws_future_impl *future,
382 aws_future_callback_fn *on_done,
383 void *user_data) {
384
385 AWS_ASSERT(future);
386 AWS_ASSERT(on_done);
387
388 struct aws_future_callback_data callback = {
389 .fn = on_done,
390 .user_data = user_data,
391 .type = AWS_FUTURE_IMMEDIATE_CALLBACK,
392 };
393 s_future_impl_register_callback(future, &callback, true /*invoke_if_already_done*/);
394 }
395
396 bool aws_future_impl_register_callback_if_not_done(
397 struct aws_future_impl *future,
398 aws_future_callback_fn *on_done,
399 void *user_data) {
400
401 AWS_ASSERT(future);
402 AWS_ASSERT(on_done);
403
404 struct aws_future_callback_data callback = {
405 .fn = on_done,
406 .user_data = user_data,
407 .type = AWS_FUTURE_IMMEDIATE_CALLBACK,
408 };
409 return s_future_impl_register_callback(future, &callback, false /*invoke_if_already_done*/);
410 }
411
412 void aws_future_impl_register_event_loop_callback(
413 struct aws_future_impl *future,
414 struct aws_event_loop *event_loop,
415 aws_future_callback_fn *on_done,
416 void *user_data) {
417
418 AWS_ASSERT(future);
419 AWS_ASSERT(event_loop);
420 AWS_ASSERT(on_done);
421
422 // TODO: aws_event_loop_acquire(event_loop);
423
424 struct aws_future_callback_data callback = {
425 .fn = on_done,
426 .user_data = user_data,
427 .type = AWS_FUTURE_EVENT_LOOP_CALLBACK,
428 .u = {.event_loop = event_loop},
429 };
430 s_future_impl_register_callback(future, &callback, true /*invoke_if_already_done*/);
431 }
432
433 void aws_future_impl_register_channel_callback(
434 struct aws_future_impl *future,
435 struct aws_channel *channel,
436 aws_future_callback_fn *on_done,
437 void *user_data) {
438
439 AWS_ASSERT(future);
440 AWS_ASSERT(channel);
441 AWS_ASSERT(on_done);
442
443 aws_channel_acquire_hold(channel);
444
445 struct aws_future_callback_data callback = {
446 .fn = on_done,
447 .user_data = user_data,
448 .type = AWS_FUTURE_CHANNEL_CALLBACK,
449 .u = {.channel = channel},
450 };
451 s_future_impl_register_callback(future, &callback, true /*invoke_if_already_done*/);
452 }
453
454 static bool s_future_impl_is_done_pred(void *user_data) {
455 struct aws_future_impl *future = user_data;
456 return future->is_done;
457 }
458
459 bool aws_future_impl_wait(const struct aws_future_impl *future, uint64_t timeout_ns) {
460 AWS_ASSERT(future);
461
462 /* this function is conceptually const, but we need to use synchronization primitives */
463 struct aws_future_impl *mutable_future = (struct aws_future_impl *)future;
464
465 /* condition-variable takes signed timeout, so clamp to INT64_MAX (292+ years) */
466 int64_t timeout_i64 = aws_min_u64(timeout_ns, INT64_MAX);
467
468 /* BEGIN CRITICAL SECTION */
469 aws_mutex_lock(&mutable_future->lock);
470
471 bool is_done = aws_condition_variable_wait_for_pred(
472 &mutable_future->wait_cvar,
473 &mutable_future->lock,
474 timeout_i64,
475 s_future_impl_is_done_pred,
476 mutable_future) == AWS_OP_SUCCESS;
477
478 aws_mutex_unlock(&mutable_future->lock);
479 /* END CRITICAL SECTION */
480
481 return is_done;
482 }
483
484 AWS_FUTURE_T_BY_VALUE_IMPLEMENTATION(aws_future_bool, bool)
485
486 AWS_FUTURE_T_BY_VALUE_IMPLEMENTATION(aws_future_size, size_t)
487
488 /**
489 * aws_future<void>
490 */
491 AWS_FUTURE_T_IMPLEMENTATION_BEGIN(aws_future_void)
492
493 struct aws_future_void *aws_future_void_new(struct aws_allocator *alloc) {
494 /* Use aws_future<bool> under the hood, to avoid edge-cases with 0-sized result */
495 return (struct aws_future_void *)aws_future_bool_new(alloc);
496 }
497
498 void aws_future_void_set_result(struct aws_future_void *future) {
499 aws_future_bool_set_result((struct aws_future_bool *)future, false);
500 }
501
502 AWS_FUTURE_T_IMPLEMENTATION_END(aws_future_void)