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 *
@@ -72,6 +72,30 @@ int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
7272}
7373RTM_EXPORT (pthread_condattr_setpshared );
7474
75+ /**
76+ * @brief Initializes a condition variable.
77+ *
78+ * This function initializes the condition variable pointed to by `cond` with the attributes
79+ * specified by `attr`. If `attr` is NULL, the condition variable is initialized with the
80+ * default attributes.
81+ *
82+ * @param cond A pointer to the condition variable to be initialized.
83+ * Must point to valid memory.
84+ * @param attr A pointer to the condition variable attributes object.
85+ * If NULL, default attributes are used.
86+ *
87+ * @return
88+ * - `0` on success.
89+ * - A non-zero error code on failure, including:
90+ * - `EINVAL`: Invalid attributes, invalid condition variable pointer, or semaphore init failed.
91+ *
92+ * @note
93+ * - The condition variable must not be used until it has been initialized.
94+ * - Each condition variable must be destroyed using `pthread_cond_destroy()`
95+ * once it is no longer needed.
96+ *
97+ * @see pthread_cond_destroy, pthread_cond_wait, pthread_cond_signal, pthread_cond_broadcast
98+ */
7599int pthread_cond_init (pthread_cond_t * cond , const pthread_condattr_t * attr )
76100{
77101 rt_err_t result ;
@@ -110,6 +134,30 @@ int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
110134}
111135RTM_EXPORT (pthread_cond_init );
112136
137+ /**
138+ * @brief Destroys a condition variable.
139+ *
140+ * This function destroys the condition variable pointed to by `cond`. After a condition
141+ * variable is destroyed, it must not be used until it is reinitialized with
142+ * `pthread_cond_init`.
143+ *
144+ * @param cond A pointer to the condition variable to be destroyed.
145+ * Must point to a valid, previously initialized condition variable.
146+ *
147+ * @return
148+ * - `0` on success.
149+ * - A non-zero error code on failure, including:
150+ * - `EBUSY`: The condition variable is currently in use by other threads.
151+ * - `EINVAL`: The condition variable is invalid or uninitialized.
152+ *
153+ * @note
154+ * - The condition variable must not be destroyed while it is being used by other threads
155+ * (e.g., in `pthread_cond_wait` or `pthread_cond_timedwait`).
156+ * - Attempting to destroy a condition variable that has not been initialized results in
157+ * undefined behavior.
158+ *
159+ * @see pthread_cond_init, pthread_cond_wait, pthread_cond_signal, pthread_cond_broadcast
160+ */
113161int pthread_cond_destroy (pthread_cond_t * cond )
114162{
115163 rt_err_t result ;
@@ -143,6 +191,30 @@ int pthread_cond_destroy(pthread_cond_t *cond)
143191}
144192RTM_EXPORT (pthread_cond_destroy );
145193
194+ /**
195+ * @brief Unblocks all threads waiting on the specified condition variable.
196+ *
197+ * This function wakes up all threads that are currently blocked on the condition variable
198+ * pointed to by `cond`. The condition variable must be associated with a mutex, and
199+ * threads waiting on the condition variable should recheck the condition after being
200+ * unblocked.
201+ *
202+ * @param cond A pointer to the condition variable.
203+ * Must point to a valid, initialized condition variable.
204+ *
205+ * @return
206+ * - `0` on success.
207+ * - A non-zero error code on failure, including:
208+ * - `EINVAL`: The condition variable is invalid or uninitialized.
209+ *
210+ * @note
211+ * - Calling this function does not release the associated mutex.
212+ * - Waking up threads does not guarantee that any specific thread will acquire the
213+ * mutex immediately, as thread scheduling depends on the system.
214+ * - Typically used when the condition might allow multiple waiting threads to proceed.
215+ *
216+ * @see pthread_cond_signal, pthread_cond_wait, pthread_cond_init, pthread_cond_destroy
217+ */
146218int pthread_cond_broadcast (pthread_cond_t * cond )
147219{
148220 rt_err_t result ;
@@ -177,6 +249,30 @@ int pthread_cond_broadcast(pthread_cond_t *cond)
177249}
178250RTM_EXPORT (pthread_cond_broadcast );
179251
252+ /**
253+ * @brief Wakes up one thread waiting on the specified condition variable.
254+ *
255+ * This function unblocks one thread that is currently waiting on the
256+ * condition variable `cond`. If multiple threads are waiting, the thread to wake
257+ * up is determined by the system's scheduling policies.
258+ *
259+ * @param cond A pointer to the condition variable to signal.
260+ * Must point to a valid and initialized condition variable.
261+ *
262+ * @return
263+ * - `0` on success.
264+ * - A non-zero error code on failure, including:
265+ * - `EINVAL`: The condition variable is invalid or uninitialized.
266+ *
267+ * @note
268+ * - This function does not release the associated mutex.
269+ * - If no threads are currently waiting on the condition variable, the call has no effect.
270+ * - The awakened thread will not run until it can reacquire the associated mutex and
271+ * re-evaluate the waiting condition.
272+ * - It is typically used when only one waiting thread should be allowed to proceed.
273+ *
274+ * @see pthread_cond_broadcast, pthread_cond_wait, pthread_cond_init, pthread_cond_destroy
275+ */
180276int pthread_cond_signal (pthread_cond_t * cond )
181277{
182278 rt_base_t temp ;
@@ -210,6 +306,35 @@ int pthread_cond_signal(pthread_cond_t *cond)
210306}
211307RTM_EXPORT (pthread_cond_signal );
212308
309+ /**
310+ * @brief Waits on a condition variable with a timeout.
311+ *
312+ * This function causes the calling thread to block on the condition variable `cond`,
313+ * releasing the associated mutex `mutex`. The thread will remain blocked until
314+ * one of the following occurs:
315+ * - It is signaled or broadcast using `pthread_cond_signal` or `pthread_cond_broadcast`.
316+ * - The specified timeout expires.
317+ *
318+ * @param cond A pointer to the condition variable to wait on.
319+ * Must point to a valid, initialized condition variable.
320+ * @param mutex A pointer to the mutex associated with the condition variable.
321+ * Must be locked by the calling thread before invoking this function.
322+ * @param timeout The timeout duration in milliseconds. A value of `RT_WAITING_FOREVER`
323+ * indicates the thread will wait indefinitely.
324+ *
325+ * @return
326+ * - `RT_EOK` on successful wakeup (signaled or broadcast).
327+ * - `-RT_ETIMEOUT` if the timeout expires before the condition variable is signaled.
328+ * - `-RT_ERROR` if an error occurs (e.g., invalid parameters).
329+ *
330+ * @note
331+ * - The mutex is automatically released while the thread waits and re-acquired before
332+ * the function returns.
333+ * - If `timeout` is 0, the function behaves as a non-blocking check.
334+ * - Ensure the condition variable and mutex are properly initialized before use.
335+ *
336+ * @see pthread_cond_signal, pthread_cond_broadcast, pthread_mutex_lock, pthread_mutex_unlock
337+ */
213338rt_err_t _pthread_cond_timedwait (pthread_cond_t * cond ,
214339 pthread_mutex_t * mutex ,
215340 rt_int32_t timeout )
@@ -327,6 +452,33 @@ rt_err_t _pthread_cond_timedwait(pthread_cond_t *cond,
327452}
328453RTM_EXPORT (_pthread_cond_timedwait );
329454
455+ /**
456+ * @brief Waits on a condition variable.
457+ *
458+ * This function blocks the calling thread on the condition variable `cond` and releases
459+ * the associated mutex `mutex`. The thread remains blocked until it is signaled or
460+ * broadcast using `pthread_cond_signal` or `pthread_cond_broadcast`. When the thread
461+ * is awakened, it re-acquires the mutex and resumes execution.
462+ *
463+ * @param cond A pointer to the condition variable to wait on.
464+ * Must point to a valid, initialized condition variable.
465+ * @param mutex A pointer to the mutex associated with the condition variable.
466+ * Must be locked by the calling thread before invoking this function.
467+ *
468+ * @return
469+ * - `0` on success.
470+ * - A non-zero error code on failure, including:
471+ * - `EINVAL`: The condition variable or mutex is invalid or uninitialized.
472+ *
473+ * @note
474+ * - The mutex must be locked before calling this function.
475+ * - Upon returning, the mutex is locked again by the calling thread.
476+ * - Spurious wakeups may occur, so the thread should always recheck the waiting
477+ * condition upon wakeup.
478+ * - This function may block indefinitely unless the condition is signaled or broadcast.
479+ *
480+ * @see pthread_cond_signal, pthread_cond_broadcast, pthread_cond_timedwait, pthread_mutex_lock
481+ */
330482int pthread_cond_wait (pthread_cond_t * cond , pthread_mutex_t * mutex )
331483{
332484 rt_err_t result ;
@@ -349,6 +501,37 @@ int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
349501}
350502RTM_EXPORT (pthread_cond_wait );
351503
504+ /**
505+ * @brief Waits on a condition variable with a timeout.
506+ *
507+ * This function blocks the calling thread on the condition variable `cond`, releasing
508+ * the associated mutex `mutex`. The thread remains blocked until one of the following occurs:
509+ * - The condition variable is signaled or broadcast using `pthread_cond_signal` or `pthread_cond_broadcast`.
510+ * - The specified absolute timeout `abstime` is reached.
511+ * - A spurious wakeup occurs (requiring the thread to recheck the condition).
512+ *
513+ * @param cond A pointer to the condition variable to wait on.
514+ * Must point to a valid, initialized condition variable.
515+ * @param mutex A pointer to the mutex associated with the condition variable.
516+ * Must be locked by the calling thread before invoking this function.
517+ * @param abstime A pointer to a `struct timespec` specifying the absolute timeout (in seconds and nanoseconds
518+ * since the Epoch). If the time specified is already reached, the function immediately returns.
519+ *
520+ * @return
521+ * - `0` on successful wakeup (signaled or broadcast).
522+ * - `ETIMEDOUT` if the timeout expires before the condition variable is signaled.
523+ * - A non-zero error code on failure, including:
524+ * - `EINVAL`: The condition variable, mutex, or `abstime` is invalid.
525+ * - `EPERM`: The mutex is not owned by the calling thread.
526+ *
527+ * @note
528+ * - The mutex is released while the thread is waiting and re-acquired before the function returns.
529+ * - Spurious wakeups may occur, so the thread must always recheck the waiting condition upon wakeup.
530+ * - The timeout is specified in absolute time, not relative duration.
531+ * - Ensure the condition variable and mutex are properly initialized before use.
532+ *
533+ * @see pthread_cond_wait, pthread_cond_signal, pthread_cond_broadcast, pthread_mutex_lock
534+ */
352535int pthread_cond_timedwait (pthread_cond_t * cond ,
353536 pthread_mutex_t * mutex ,
354537 const struct timespec * abstime )
0 commit comments