Reduct  v4.0.5-1-g4851deb
A functional and immutable language.
Loading...
Searching...
No Matches
future.h
Go to the documentation of this file.
1#ifndef REDUCT_FUTURE_H
2#define REDUCT_FUTURE_H 1
3
4#include <reduct/defs.h>
5#include <reduct/task.h>
6
7#include <stdatomic.h>
8#include <stdbool.h>
9#include <threads.h>
10
11struct reduct;
12
13/**
14 * @file future.h
15 * @brief Future primitives.
16 * @defgroup future Future
17 *
18 * @{
19 */
20
21#define REDUCT_FUTURE_SMALL_MAX 2 ///< The maximum number of small arguments in a future.
22
23/**
24 * @brief Future structure.
25 * @struct reduct_future_t
26 */
27typedef struct reduct_future
28{
29 reduct_error_t* error; ///< Error structure, will be `NULL` if no error occured.
30 union {
31 reduct_handle_t callable; ///< The callable to execute.
32 reduct_handle_t result; ///< The result of the execution.
33 };
35 reduct_handle_t* argv; ///< The arguments for the callable.
36 uint32_t argc; ///< The number of arguments.
37 _Atomic(bool) done; ///< Whether the future is finished.
38 reduct_task_id_t taskId; ///< The task ID in the task system.
40
41/**
42 * @brief Create a new future and start its execution.
43 *
44 * @param reduct Pointer to the Reduct structure.
45 * @param callable The callable handle.
46 * @param argc The number of arguments.
47 * @param argv Pointer to the arguments array.
48 * @return Pointer to the new future.
49 */
50REDUCT_API reduct_future_t* reduct_future_new(struct reduct* reduct, reduct_handle_t callable, size_t argc,
51 reduct_handle_t* argv);
52
53/**
54 * @brief Wait for the future to complete and return its result.
55 *
56 * @param reduct Pointer to the Reduct structure.
57 * @param future Pointer to the future.
58 * @return The result handle.
59 */
60static inline REDUCT_ALWAYS_INLINE reduct_handle_t reduct_future_join(struct reduct* reduct, reduct_future_t* future)
61{
62 if (!atomic_load_explicit(&future->done, memory_order_acquire))
63 {
64 reduct_task_join(reduct, future->taskId);
65 }
66
67 if (REDUCT_UNLIKELY(future->error != NULL))
68 {
69 REDUCT_ERROR_RETHROW(reduct, future->error);
70 }
71
72 return future->result;
73}
74
75/**
76 * @brief Check if the future is finished.
77 *
78 * @param reduct Pointer to the Reduct structure.
79 * @param future Pointer to the future.
80 * @return `true` if done, `false` otherwise.
81 */
82static inline REDUCT_ALWAYS_INLINE bool reduct_future_is_done(struct reduct* reduct, reduct_future_t* future)
83{
84 assert(reduct != NULL);
85 assert(future != NULL);
86
87 REDUCT_UNUSED(reduct);
88
89 return atomic_load_explicit(&future->done, memory_order_relaxed);
90}
91
92/** @} */
93
94#endif
#define REDUCT_UNLIKELY(_x)
Definition defs.h:59
#define REDUCT_ALWAYS_INLINE
Definition defs.h:61
#define REDUCT_API
Definition defs.h:24
#define REDUCT_UNUSED(_x)
Definition defs.h:67
#define REDUCT_ERROR_RETHROW(_reduct, _error)
Rethrow an existing error.
Definition error.h:215
static REDUCT_ALWAYS_INLINE reduct_handle_t reduct_future_join(struct reduct *reduct, reduct_future_t *future)
Wait for the future to complete and return its result.
Definition future.h:60
REDUCT_API reduct_future_t * reduct_future_new(struct reduct *reduct, reduct_handle_t callable, size_t argc, reduct_handle_t *argv)
Create a new future and start its execution.
#define REDUCT_FUTURE_SMALL_MAX
The maximum number of small arguments in a future.
Definition future.h:21
static REDUCT_ALWAYS_INLINE bool reduct_future_is_done(struct reduct *reduct, reduct_future_t *future)
Check if the future is finished.
Definition future.h:82
REDUCT_API void reduct_task_join(struct reduct *reduct, reduct_task_id_t id)
Wait for a task to finish.
Error structure.
Definition error.h:55
Future structure.
Definition future.h:28
reduct_handle_t callable
The callable to execute.
Definition future.h:31
reduct_handle_t * argv
The arguments for the callable.
Definition future.h:35
reduct_error_t * error
Error structure, will be NULL if no error occured.
Definition future.h:29
_Atomic(bool) done
Whether the future is finished.
reduct_task_id_t taskId
The task ID in the task system.
Definition future.h:38
uint32_t argc
The number of arguments.
Definition future.h:36
reduct_handle_t result
The result of the execution.
Definition future.h:32
Handle type.
Definition defs.h:119
Task ID structure.
Definition task.h:32
Task primitives.