pthread_cond.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2006-2021, 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. #include "pthread_internal.h"
  12. int pthread_condattr_destroy(pthread_condattr_t *attr)
  13. {
  14. if (!attr)
  15. return EINVAL;
  16. return 0;
  17. }
  18. RTM_EXPORT(pthread_condattr_destroy);
  19. int pthread_condattr_init(pthread_condattr_t *attr)
  20. {
  21. if (!attr)
  22. return EINVAL;
  23. *attr = PTHREAD_PROCESS_PRIVATE;
  24. return 0;
  25. }
  26. RTM_EXPORT(pthread_condattr_init);
  27. int pthread_condattr_getclock(const pthread_condattr_t *attr,
  28. clockid_t *clock_id)
  29. {
  30. return 0;
  31. }
  32. RTM_EXPORT(pthread_condattr_getclock);
  33. int pthread_condattr_setclock(pthread_condattr_t *attr,
  34. clockid_t clock_id)
  35. {
  36. return 0;
  37. }
  38. RTM_EXPORT(pthread_condattr_setclock);
  39. int pthread_condattr_getpshared(const pthread_condattr_t *attr, int *pshared)
  40. {
  41. if (!attr || !pshared)
  42. return EINVAL;
  43. *pshared = PTHREAD_PROCESS_PRIVATE;
  44. return 0;
  45. }
  46. RTM_EXPORT(pthread_condattr_getpshared);
  47. int pthread_condattr_setpshared(pthread_condattr_t*attr, int pshared)
  48. {
  49. if ((pshared != PTHREAD_PROCESS_PRIVATE) &&
  50. (pshared != PTHREAD_PROCESS_SHARED))
  51. {
  52. return EINVAL;
  53. }
  54. if (pshared != PTHREAD_PROCESS_PRIVATE)
  55. return ENOSYS;
  56. return 0;
  57. }
  58. RTM_EXPORT(pthread_condattr_setpshared);
  59. int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
  60. {
  61. rt_err_t result;
  62. char cond_name[RT_NAME_MAX];
  63. static rt_uint16_t cond_num = 0;
  64. /* parameter check */
  65. if (cond == RT_NULL)
  66. return EINVAL;
  67. if ((attr != RT_NULL) && (*attr != PTHREAD_PROCESS_PRIVATE))
  68. return EINVAL;
  69. rt_snprintf(cond_name, sizeof(cond_name), "cond%02d", cond_num++);
  70. if (attr == RT_NULL) /* use default value */
  71. cond->attr = PTHREAD_PROCESS_PRIVATE;
  72. else
  73. cond->attr = *attr;
  74. result = rt_sem_init(&cond->sem, cond_name, 0, RT_IPC_FLAG_FIFO);
  75. if (result != RT_EOK)
  76. return EINVAL;
  77. /* detach the object from system object container */
  78. rt_object_detach(&(cond->sem.parent.parent));
  79. cond->sem.parent.parent.type = RT_Object_Class_Semaphore;
  80. return 0;
  81. }
  82. RTM_EXPORT(pthread_cond_init);
  83. int pthread_cond_destroy(pthread_cond_t *cond)
  84. {
  85. rt_err_t result;
  86. if (cond == RT_NULL)
  87. return EINVAL;
  88. if (cond->attr == -1)
  89. return 0; /* which is not initialized */
  90. result = rt_sem_trytake(&(cond->sem));
  91. if (result != RT_EOK)
  92. return EBUSY;
  93. /* clean condition */
  94. rt_memset(cond, 0, sizeof(pthread_cond_t));
  95. cond->attr = -1;
  96. return 0;
  97. }
  98. RTM_EXPORT(pthread_cond_destroy);
  99. int pthread_cond_broadcast(pthread_cond_t *cond)
  100. {
  101. rt_err_t result;
  102. if (cond == RT_NULL)
  103. return EINVAL;
  104. if (cond->attr == -1)
  105. pthread_cond_init(cond, RT_NULL);
  106. rt_enter_critical();
  107. while (1)
  108. {
  109. /* try to take condition semaphore */
  110. result = rt_sem_trytake(&(cond->sem));
  111. if (result == -RT_ETIMEOUT)
  112. {
  113. /* it's timeout, release this semaphore */
  114. rt_sem_release(&(cond->sem));
  115. }
  116. else if (result == RT_EOK)
  117. {
  118. /* has taken this semaphore, release it */
  119. rt_sem_release(&(cond->sem));
  120. break;
  121. }
  122. else
  123. {
  124. rt_exit_critical();
  125. return EINVAL;
  126. }
  127. }
  128. rt_exit_critical();
  129. return 0;
  130. }
  131. RTM_EXPORT(pthread_cond_broadcast);
  132. int pthread_cond_signal(pthread_cond_t *cond)
  133. {
  134. rt_err_t result;
  135. if (cond == RT_NULL)
  136. return EINVAL;
  137. if (cond->attr == -1)
  138. pthread_cond_init(cond, RT_NULL);
  139. result = rt_sem_release(&(cond->sem));
  140. if (result == RT_EOK)
  141. return 0;
  142. return 0;
  143. }
  144. RTM_EXPORT(pthread_cond_signal);
  145. rt_err_t _pthread_cond_timedwait(pthread_cond_t *cond,
  146. pthread_mutex_t *mutex,
  147. rt_int32_t timeout)
  148. {
  149. rt_err_t result;
  150. if (!cond || !mutex)
  151. return -RT_ERROR;
  152. /* check whether initialized */
  153. if (cond->attr == -1)
  154. pthread_cond_init(cond, RT_NULL);
  155. /* The mutex was not owned by the current thread at the time of the call. */
  156. if (mutex->lock.owner != rt_thread_self())
  157. return -RT_ERROR;
  158. /* unlock a mutex failed */
  159. if (pthread_mutex_unlock(mutex) != 0)
  160. return -RT_ERROR;
  161. result = rt_sem_take(&(cond->sem), timeout);
  162. /* lock mutex again */
  163. pthread_mutex_lock(mutex);
  164. return result;
  165. }
  166. RTM_EXPORT(_pthread_cond_timedwait);
  167. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  168. {
  169. rt_err_t result;
  170. result = _pthread_cond_timedwait(cond, mutex, RT_WAITING_FOREVER);
  171. if (result == RT_EOK)
  172. return 0;
  173. return EINVAL;
  174. }
  175. RTM_EXPORT(pthread_cond_wait);
  176. int pthread_cond_timedwait(pthread_cond_t *cond,
  177. pthread_mutex_t *mutex,
  178. const struct timespec *abstime)
  179. {
  180. int timeout;
  181. rt_err_t result;
  182. timeout = rt_timespec_to_tick(abstime);
  183. result = _pthread_cond_timedwait(cond, mutex, timeout);
  184. if (result == RT_EOK)
  185. return 0;
  186. if (result == -RT_ETIMEOUT)
  187. return ETIMEDOUT;
  188. return EINVAL;
  189. }
  190. RTM_EXPORT(pthread_cond_timedwait);