KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
barrier.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 kos/barrier.h
4 Copyright (C) 2023 Lawrence Sebald
5*/
6
7#ifndef __KOS_BARRIER_H
8#define __KOS_BARRIER_H
9
10#include <sys/cdefs.h>
11__BEGIN_DECLS
12
13/** \file kos/barrier.h
14 \brief Thread barriers.
15 \ingroup barriers
16
17 Thread barriers are used to synchronize the progress of multiple threads. A
18 barrier causes threads to wait until a specified number of threads have
19 reached a certain execution point, ensuring a consistent state across
20 different execution paths.
21
22 This synchronization primitive is essential for scenarios in parallel
23 programming where tasks executed by multiple threads must reach a certain
24 point before any can proceed, ensuring data consistency and coordination
25 among threads.
26
27 \author Lawrence Sebald
28*/
29
30/** \defgroup barriers Barriers
31 \brief KOS barrier API for kernel threads
32 \ingroup kthreads
33
34 Barriers are a type of synchronization method which halt execution
35 for group of threads until a certain number of them have reached
36 the barrier.
37
38 @{
39*/
40
41/** \brief Constant returned to one thread from pthread_barrier_wait().
42
43 A single (unspecified) thread will be returned this value after successfully
44 waiting on a barrier, with all other threads being returned a 0. This is
45 useful for selecting one thread to perform any cleanup work associated with
46 the barrier (or other serial work that must be performed).
47*/
48#define THD_BARRIER_SERIAL_THREAD 0x7fffffff
49
50/** \cond */
51#ifndef __KTHREAD_HAVE_BARRIER_TYPE
52
53#define __KTHREAD_HAVE_BARRIER_TYPE 1
54/** \endcond */
55
56/** \brief Size of a thread barrier, in bytes. */
57#define THD_BARRIER_SIZE 64
58
59/** \brief Thread barrier type.
60
61 Type used for implementing thread barriers. All members of this structure
62 are private. Do not attempt to manipulate any data within any instances of
63 this structure.
64
65 \headerfile kos/barrier.h
66*/
67typedef union kos_thd_barrier {
68 /** \cond Opaque structure */
69 unsigned char __opaque[THD_BARRIER_SIZE];
70 long int __align;
71 /** \endcond */
73
74/** \cond */
75#endif /* !__KTHREAD_HAVE_BARRIER_TYPE */
76/** \endcond */
77
78/** \brief Initialize a thread barrier.
79
80 This function initializes a thread barrier for use among the specified
81 number of threads.
82
83 \param barrier The barrier to initialize.
84 \param attr Reserved for POSIX compatibility. Always pass NULL.
85 \param count The number of threads the barrier will expect.
86
87 \return 0 on success, non-zero error code on failure
88
89 \par Error Conditions:
90 \em EINVAL - NULL was passed for barrier \n
91 \em EINVAL - Non-NULL was passed for attr \n
92 \em EINVAL - count == 0 or count > UINT32_MAX
93*/
95 const void *__RESTRICT attr, unsigned count);
96
97/** \brief Destroy a thread barrier.
98
99 This function destroys a thread barrier, releasing any resources associated
100 with the barrier. Subsequent use of the barrier is results in undefined
101 behavior unless it is later re-initialized with thd_barrier_init().
102
103 \param barrier The barrier to destroy.
104
105 \return 0 on success, non-zero error code on failure
106
107 \par Error Conditions:
108 \em EBUSY - A destroy operation is already in progress on barrier \n
109 \em EINVAL - NULL was passed for barrier \n
110 \em EINVAL - An already destroyed barrier was passed in \n
111 \em EPERM - Function was called in an interrupt context
112
113 \note This function may block if a wait operation is currently in progress
114 on the specified barrier.
115*/
117
118/** \brief Wait on a thread barrier.
119
120 This function synchronizes the participating threads at the barrier
121 specified. The calling thread will block until the required number of
122 threads (specified by the barrier's count) have called this function to
123 wait on the barrier.
124
125 \param barrier The barrier to wait on.
126
127 \return 0 or THD_BARRIER_SERIAL_THREAD on success, non-zero error
128 code on failure
129
130 \par Error Conditions:
131 \em EINVAL - NULL was passed for barrier \n
132 \em EINVAL - barrier was destroyed via thd_barrier_destroy() \n
133 \em EINVAL - A call to thd_barrier_destroy() is in progress for
134 the specified barrier \n
135 \em EPERM - Function was called in an interrupt context
136*/
138
139/** @} */
140
141__END_DECLS
142
143#endif /* !__KOS_BARRIER_H */
int thd_barrier_destroy(thd_barrier_t *barrier)
Destroy a thread barrier.
int thd_barrier_wait(thd_barrier_t *barrier)
Wait on a thread barrier.
int thd_barrier_init(thd_barrier_t *__RESTRICT barrier, const void *__RESTRICT attr, unsigned count)
Initialize a thread barrier.
#define THD_BARRIER_SIZE
Size of a thread barrier, in bytes.
Definition barrier.h:57
#define __RESTRICT
Definition cdefs.h:195
Thread barrier type.
Definition barrier.h:67