Reduct  v4.0.5-1-g4851deb
A functional and immutable language.
Loading...
Searching...
No Matches
task.h
Go to the documentation of this file.
1#ifndef REDUCT_TASK_H
2#define REDUCT_TASK_H 1
3
4#include <reduct/defs.h>
5#include <reduct/error.h>
6#include <reduct/sync.h>
7
8#include <stdatomic.h>
9#include <stdbool.h>
10#include <stdint.h>
11#include <threads.h>
12
13struct reduct_task;
14
15/**
16 * @file task.h
17 * @brief Task primitives.
18 * @defgroup task Task
19 *
20 * @{
21 */
22
23#define REDUCT_TASK_QUEUE_MAX 256 ///< The maximum number of tasks.
24
25#define REDUCT_TASK_SPIN_MAX 1000 ///< The maximum number of times to spin while waiting for work to be ready.
26
27/**
28 * @brief Task ID structure.
29 * @struct reduct_task_id_t
30 */
31typedef struct
32{
33 uint16_t index; ///< The index within the queue.
34 uint16_t
35 generation; ///< Incrementally increasing counter to avoid ABA problems when reusing task slots in the queue.
37
38#define REDUCT_TASK_ID_INVALID ((reduct_task_id_t){ .index = UINT16_MAX, .generation = UINT16_MAX }) ///< Invalid task ID.
39
40/**
41 * @brief Task structure.
42 * @struct reduct_task_t
43 */
44typedef struct reduct_task
45{
46 _Atomic(uint16_t) generation;
47 void (*func)(struct reduct* reduct, void* arg);
48 void* arg;
50
51/**
52 * @brief Global thread-related state structure.
53 * @struct reduct_task_global_t
54 */
55typedef struct
56{
57 mtx_t mutex;
58 cnd_t cond;
59 _Atomic(bool) shutdown;
61 _Atomic(size_t) queueHead;
62 _Atomic(size_t) queueTail;
63 _Atomic(uint32_t) idleCount;
64 _Atomic(uint32_t) barrierCount;
65 _Atomic(uint32_t) barrierGen;
67
68/**
69 * @brief Worker thread main loop..
70 *
71 * @param arg Pointer to the `reduct_t` structure of the thread.
72 */
74
75/**
76 * @brief Initialize a global task state.
77 *
78 * @param global Pointer to the global task state to initialize.
79 */
81
82/**
83 * @brief Deinitialize a global task state.
84 *
85 * @param global Pointer to the global task state to deinitialize.
86 */
88
89/**
90 * @brief Try to create a new task.
91 *
92 * @param reduct Pointer to the Reduct structure.
93 * @param func The function to execute in the task.
94 * @param arg The argument to pass to the function.
95 * @param outId Pointer to store the ID of the created task.
96 * @return `true` if the task was created, `false` if the queue is full.
97 */
98REDUCT_API bool reduct_task_create(struct reduct* reduct, void (*func)(struct reduct* reduct, void* arg), void* arg,
99 reduct_task_id_t* outId);
100
101/**
102 * @brief Wait for a task to finish.
103 *
104 * @param reduct Pointer to the Reduct structure.
105 * @param id The id of the task to wait for.
106 */
107REDUCT_API void reduct_task_join(struct reduct* reduct, reduct_task_id_t id);
108
109/**
110 * @brief Retrieve the number of pending tasks.
111 *
112 * @param global Pointer to the global task state.
113 * @return The number of pending tasks in the queue.
114 */
116{
117 size_t head = atomic_load_explicit(&global->queueHead, memory_order_acquire);
118 size_t tail = atomic_load_explicit(&global->queueTail, memory_order_acquire);
119 return head - tail;
120}
121
122/**
123 * @brief Barrier function to ensure all threads have either reached the barrier.
124 *
125 * Once the function returns all threads that have reached the barrier will be unblocked.
126 *
127 * @param reduct Pointer to the Reduct structure.
128 */
129REDUCT_API void reduct_task_barrier(struct reduct* reduct);
130
131/** @} */
132
133#endif
#define REDUCT_ALWAYS_INLINE
Definition defs.h:61
#define REDUCT_API
Definition defs.h:24
Error handling and reporting.
REDUCT_API void reduct_task_global_init(reduct_task_global_t *global)
Initialize a global task state.
static REDUCT_ALWAYS_INLINE uint64_t reduct_task_queue_size(reduct_task_global_t *global)
Retrieve the number of pending tasks.
Definition task.h:115
REDUCT_API bool reduct_task_create(struct reduct *reduct, void(*func)(struct reduct *reduct, void *arg), void *arg, reduct_task_id_t *outId)
Try to create a new task.
#define REDUCT_TASK_QUEUE_MAX
The maximum number of tasks.
Definition task.h:23
REDUCT_API int reduct_task_worker(void *arg)
Worker thread main loop..
REDUCT_API void reduct_task_global_deinit(reduct_task_global_t *global)
Deinitialize a global task state.
REDUCT_API void reduct_task_join(struct reduct *reduct, reduct_task_id_t id)
Wait for a task to finish.
REDUCT_API void reduct_task_barrier(struct reduct *reduct)
Barrier function to ensure all threads have either reached the barrier.
Global thread-related state structure.
Definition task.h:56
_Atomic(size_t) queueHead
_Atomic(size_t) queueTail
_Atomic(bool) shutdown
_Atomic(uint32_t) idleCount
_Atomic(uint32_t) barrierCount
_Atomic(uint32_t) barrierGen
Task ID structure.
Definition task.h:32
uint16_t generation
Incrementally increasing counter to avoid ABA problems when reusing task slots in the queue.
Definition task.h:35
uint16_t index
The index within the queue.
Definition task.h:33
Task structure.
Definition task.h:45
_Atomic(uint16_t) generation
void * arg
Definition task.h:48
Syncronization primitives.