Skip to content

Commit 79324c0

Browse files
GorrayLimysterywolf
authored andcommitted
[components/libc/posix]add comments for barrier APIs.
1 parent f797ecc commit 79324c0

File tree

1 file changed

+85
-1
lines changed

1 file changed

+85
-1
lines changed

components/libc/posix/pthreads/pthread_barrier.c

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -51,6 +51,31 @@ int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared)
5151
}
5252
RTM_EXPORT(pthread_barrierattr_setpshared);
5353

54+
/**
55+
* @brief Destroys a barrier object.
56+
*
57+
* The `pthread_barrier_destroy` function releases any resources associated
58+
* with the specified barrier object. After a barrier has been destroyed,
59+
* it cannot be used again unless it is reinitialized using `pthread_barrier_init`.
60+
*
61+
* @param[in] barrier
62+
* A pointer to an initialized `pthread_barrier_t` object to be destroyed.
63+
*
64+
* @return
65+
* - `0` on success.
66+
* - `EINVAL` if the `barrier` is invalid or uninitialized.
67+
* - `EBUSY` if there are threads currently blocked on the barrier.
68+
*
69+
* @note
70+
* - Ensure that no threads are blocked on the barrier before calling this function.
71+
* - Attempting to destroy a barrier that is still in use results in undefined behavior.
72+
*
73+
* @warning
74+
* Destroying a barrier without ensuring it is no longer in use can lead to
75+
* resource leaks or undefined program behavior.
76+
*
77+
* @see pthread_barrier_init, pthread_barrier_wait
78+
*/
5479
int pthread_barrier_destroy(pthread_barrier_t *barrier)
5580
{
5681
rt_err_t result;
@@ -64,6 +89,38 @@ int pthread_barrier_destroy(pthread_barrier_t *barrier)
6489
}
6590
RTM_EXPORT(pthread_barrier_destroy);
6691

92+
/**
93+
* @brief Initializes a barrier for synchronizing threads.
94+
*
95+
* The `pthread_barrier_init` function initializes a barrier object
96+
* that allows a specified number of threads to synchronize at a barrier point.
97+
* Each thread waits at the barrier until the required number of threads have called
98+
* `pthread_barrier_wait`.
99+
*
100+
* @param[out] barrier
101+
* A pointer to the `pthread_barrier_t` object to be initialized.
102+
* This object must not already be initialized.
103+
*
104+
* @param[in] attr
105+
* A pointer to a `pthread_barrierattr_t` object that specifies
106+
* attributes for the barrier (e.g., process-shared or process-private).
107+
* If NULL, the default attributes are used.
108+
*
109+
* @param[in] count
110+
* The number of threads that must call `pthread_barrier_wait`
111+
* before any of them successfully return from the barrier.
112+
*
113+
* @return
114+
* - `0` on success.
115+
* - `EINVAL` if the `count` is zero or `barrier` is invalid.
116+
*
117+
* @note The barrier must be destroyed using `pthread_barrier_destroy`
118+
* when it is no longer needed.
119+
*
120+
* @warning If `count` is set to zero, the behavior is undefined.
121+
*
122+
* @see pthread_barrier_wait, pthread_barrier_destroy
123+
*/
67124
int pthread_barrier_init(pthread_barrier_t *barrier,
68125
const pthread_barrierattr_t *attr,
69126
unsigned count)
@@ -83,6 +140,33 @@ int pthread_barrier_init(pthread_barrier_t *barrier,
83140
}
84141
RTM_EXPORT(pthread_barrier_init);
85142

143+
/**
144+
* @brief Synchronizes threads at a barrier.
145+
*
146+
* The `pthread_barrier_wait` function blocks the calling thread at the specified
147+
* barrier until the required number of threads have reached the barrier. Once
148+
* the required number of threads have called this function, all threads are
149+
* unblocked and can proceed.
150+
*
151+
* @param[in] barrier
152+
* A pointer to an initialized `pthread_barrier_t` object representing the barrier
153+
* at which threads will synchronize.
154+
*
155+
* @return
156+
* - `0` for all threads except one.
157+
* - `EINVAL` - The `barrier` is invalid or uninitialized.
158+
*
159+
* @note
160+
* - All threads participating in the barrier must call `pthread_barrier_wait`
161+
* before any of them are released.
162+
*
163+
* @warning
164+
* Ensure that the number of threads specified during the barrier's initialization
165+
* matches the number of threads calling this function, otherwise the program
166+
* may hang indefinitely.
167+
*
168+
* @see pthread_barrier_init, pthread_barrier_destroy
169+
*/
86170
int pthread_barrier_wait(pthread_barrier_t *barrier)
87171
{
88172
rt_err_t result;

0 commit comments

Comments
 (0)