pthread_barrier.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-10-26 Bernard the first version
  9. */
  10. #include <pthread.h>
  11. int pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
  12. {
  13. if (!attr)
  14. return EINVAL;
  15. return 0;
  16. }
  17. RTM_EXPORT(pthread_barrierattr_destroy);
  18. int pthread_barrierattr_init(pthread_barrierattr_t *attr)
  19. {
  20. if (!attr)
  21. return EINVAL;
  22. *attr = PTHREAD_PROCESS_PRIVATE;
  23. return 0;
  24. }
  25. RTM_EXPORT(pthread_barrierattr_init);
  26. int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr,
  27. int *pshared)
  28. {
  29. if (!attr)
  30. return EINVAL;
  31. *pshared = (int)*attr;
  32. return 0;
  33. }
  34. RTM_EXPORT(pthread_barrierattr_getpshared);
  35. int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared)
  36. {
  37. if (!attr)
  38. return EINVAL;
  39. if (pshared == PTHREAD_PROCESS_PRIVATE)
  40. {
  41. *attr = PTHREAD_PROCESS_PRIVATE;
  42. return 0;
  43. }
  44. return EINVAL;
  45. }
  46. RTM_EXPORT(pthread_barrierattr_setpshared);
  47. /**
  48. * @brief Destroys a barrier object.
  49. *
  50. * The `pthread_barrier_destroy` function releases any resources associated
  51. * with the specified barrier object. After a barrier has been destroyed,
  52. * it cannot be used again unless it is reinitialized using `pthread_barrier_init`.
  53. *
  54. * @param[in] barrier
  55. * A pointer to an initialized `pthread_barrier_t` object to be destroyed.
  56. *
  57. * @return
  58. * - `0` on success.
  59. * - `EINVAL` if the `barrier` is invalid or uninitialized.
  60. * - `EBUSY` if there are threads currently blocked on the barrier.
  61. *
  62. * @note
  63. * - Ensure that no threads are blocked on the barrier before calling this function.
  64. * - Attempting to destroy a barrier that is still in use results in undefined behavior.
  65. *
  66. * @warning
  67. * Destroying a barrier without ensuring it is no longer in use can lead to
  68. * resource leaks or undefined program behavior.
  69. *
  70. * @see pthread_barrier_init, pthread_barrier_wait
  71. */
  72. int pthread_barrier_destroy(pthread_barrier_t *barrier)
  73. {
  74. rt_err_t result;
  75. if (!barrier)
  76. return EINVAL;
  77. /* Lock the internal mutex to safely check the barrier's state*/
  78. result = pthread_mutex_lock(&(barrier->mutex));
  79. if (result != 0)
  80. return result;
  81. /* Check if any threads are currently waiting on the barrier*/
  82. if (barrier->count != 0)
  83. {
  84. pthread_mutex_unlock(&(barrier->mutex));
  85. return EBUSY; /* Threads are still waiting*/
  86. }
  87. /* Free resources associated with the barrier*/
  88. result = pthread_mutex_unlock(&(barrier->mutex));
  89. if (result != 0)
  90. {
  91. return result; /* Return mutex unlock error*/
  92. }
  93. result = pthread_mutex_destroy(&(barrier->mutex));
  94. if (result != 0)
  95. {
  96. return result; /* Return mutex destroy error*/
  97. }
  98. result = pthread_cond_destroy(&(barrier->cond));
  99. return result;
  100. }
  101. RTM_EXPORT(pthread_barrier_destroy);
  102. /**
  103. * @brief Initializes a barrier for synchronizing threads.
  104. *
  105. * The `pthread_barrier_init` function initializes a barrier object
  106. * that allows a specified number of threads to synchronize at a barrier point.
  107. * Each thread waits at the barrier until the required number of threads have called
  108. * `pthread_barrier_wait`.
  109. *
  110. * @param[out] barrier
  111. * A pointer to the `pthread_barrier_t` object to be initialized.
  112. * This object must not already be initialized.
  113. *
  114. * @param[in] attr
  115. * A pointer to a `pthread_barrierattr_t` object that specifies
  116. * attributes for the barrier (e.g., process-shared or process-private).
  117. * If NULL, the default attributes are used.
  118. *
  119. * @param[in] count
  120. * The number of threads that must call `pthread_barrier_wait`
  121. * before any of them successfully return from the barrier.
  122. *
  123. * @return
  124. * - `0` on success.
  125. * - `EINVAL` if the `count` is zero or `barrier` is invalid.
  126. *
  127. * @note The barrier must be destroyed using `pthread_barrier_destroy`
  128. * when it is no longer needed.
  129. *
  130. * @warning If `count` is set to zero, the behavior is undefined.
  131. *
  132. * @see pthread_barrier_wait, pthread_barrier_destroy
  133. */
  134. int pthread_barrier_init(pthread_barrier_t *barrier,
  135. const pthread_barrierattr_t *attr,
  136. unsigned count)
  137. {
  138. if (!barrier)
  139. return EINVAL;
  140. if (attr && (*attr != PTHREAD_PROCESS_PRIVATE))
  141. return EINVAL;
  142. if (count == 0)
  143. return EINVAL;
  144. barrier->count = count;
  145. pthread_cond_init(&(barrier->cond), NULL);
  146. pthread_mutex_init(&(barrier->mutex), NULL);
  147. return 0;
  148. }
  149. RTM_EXPORT(pthread_barrier_init);
  150. /**
  151. * @brief Synchronizes threads at a barrier.
  152. *
  153. * The `pthread_barrier_wait` function blocks the calling thread at the specified
  154. * barrier until the required number of threads have reached the barrier. Once
  155. * the required number of threads have called this function, all threads are
  156. * unblocked and can proceed.
  157. *
  158. * @param[in] barrier
  159. * A pointer to an initialized `pthread_barrier_t` object representing the barrier
  160. * at which threads will synchronize.
  161. *
  162. * @return
  163. * - `0` for all threads except one.
  164. * - `EINVAL` - The `barrier` is invalid or uninitialized.
  165. *
  166. * @note
  167. * - All threads participating in the barrier must call `pthread_barrier_wait`
  168. * before any of them are released.
  169. *
  170. * @warning
  171. * Ensure that the number of threads specified during the barrier's initialization
  172. * matches the number of threads calling this function, otherwise the program
  173. * may hang indefinitely.
  174. *
  175. * @see pthread_barrier_init, pthread_barrier_destroy
  176. */
  177. int pthread_barrier_wait(pthread_barrier_t *barrier)
  178. {
  179. rt_err_t result;
  180. if (!barrier)
  181. return EINVAL;
  182. result = pthread_mutex_lock(&(barrier->mutex));
  183. if (result != 0)
  184. return EINVAL;
  185. if (barrier->count == 0)
  186. result = EINVAL;
  187. else
  188. {
  189. barrier->count -= 1;
  190. if (barrier->count == 0) /* broadcast condition */
  191. pthread_cond_broadcast(&(barrier->cond));
  192. else
  193. pthread_cond_wait(&(barrier->cond), &(barrier->mutex));
  194. }
  195. pthread_mutex_unlock(&(barrier->mutex));
  196. return result;
  197. }
  198. RTM_EXPORT(pthread_barrier_wait);