pthread_cond.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. * 2022-06-27 xiangxistu use atomic operation to protect pthread conditional variable
  10. */
  11. #include <rthw.h>
  12. #include <pthread.h>
  13. #include "pthread_internal.h"
  14. int pthread_condattr_destroy(pthread_condattr_t *attr)
  15. {
  16. if (!attr)
  17. return EINVAL;
  18. return 0;
  19. }
  20. RTM_EXPORT(pthread_condattr_destroy);
  21. int pthread_condattr_init(pthread_condattr_t *attr)
  22. {
  23. if (!attr)
  24. return EINVAL;
  25. *attr = PTHREAD_PROCESS_PRIVATE;
  26. return 0;
  27. }
  28. RTM_EXPORT(pthread_condattr_init);
  29. int pthread_condattr_getclock(const pthread_condattr_t *attr,
  30. clockid_t *clock_id)
  31. {
  32. return 0;
  33. }
  34. RTM_EXPORT(pthread_condattr_getclock);
  35. int pthread_condattr_setclock(pthread_condattr_t *attr,
  36. clockid_t clock_id)
  37. {
  38. return 0;
  39. }
  40. RTM_EXPORT(pthread_condattr_setclock);
  41. int pthread_condattr_getpshared(const pthread_condattr_t *attr, int *pshared)
  42. {
  43. if (!attr || !pshared)
  44. return EINVAL;
  45. *pshared = PTHREAD_PROCESS_PRIVATE;
  46. return 0;
  47. }
  48. RTM_EXPORT(pthread_condattr_getpshared);
  49. int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
  50. {
  51. if ((pshared != PTHREAD_PROCESS_PRIVATE) &&
  52. (pshared != PTHREAD_PROCESS_SHARED))
  53. {
  54. return EINVAL;
  55. }
  56. if (pshared != PTHREAD_PROCESS_PRIVATE)
  57. return ENOSYS;
  58. return 0;
  59. }
  60. RTM_EXPORT(pthread_condattr_setpshared);
  61. int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
  62. {
  63. rt_err_t result;
  64. char cond_name[RT_NAME_MAX];
  65. static rt_uint16_t cond_num = 0;
  66. /* parameter check */
  67. if (cond == RT_NULL)
  68. return EINVAL;
  69. if ((attr != RT_NULL) && (*attr != PTHREAD_PROCESS_PRIVATE))
  70. return EINVAL;
  71. rt_snprintf(cond_name, sizeof(cond_name), "cond%02d", cond_num++);
  72. /* use default value */
  73. if (attr == RT_NULL)
  74. {
  75. cond->attr = PTHREAD_PROCESS_PRIVATE;
  76. }
  77. else
  78. {
  79. cond->attr = *attr;
  80. }
  81. result = rt_sem_init(&cond->sem, cond_name, 0, RT_IPC_FLAG_FIFO);
  82. if (result != RT_EOK)
  83. {
  84. return EINVAL;
  85. }
  86. /* detach the object from system object container */
  87. rt_object_detach(&(cond->sem.parent.parent));
  88. cond->sem.parent.parent.type = RT_Object_Class_Semaphore;
  89. return 0;
  90. }
  91. RTM_EXPORT(pthread_cond_init);
  92. int pthread_cond_destroy(pthread_cond_t *cond)
  93. {
  94. rt_err_t result;
  95. if (cond == RT_NULL)
  96. {
  97. return EINVAL;
  98. }
  99. /* which is not initialized */
  100. if (cond->attr == -1)
  101. {
  102. return 0;
  103. }
  104. if (!rt_list_isempty(&cond->sem.parent.suspend_thread))
  105. {
  106. return EBUSY;
  107. }
  108. __retry:
  109. result = rt_sem_trytake(&(cond->sem));
  110. if (result == EBUSY)
  111. {
  112. pthread_cond_broadcast(cond);
  113. goto __retry;
  114. }
  115. /* clean condition */
  116. rt_memset(cond, 0, sizeof(pthread_cond_t));
  117. cond->attr = -1;
  118. return 0;
  119. }
  120. RTM_EXPORT(pthread_cond_destroy);
  121. int pthread_cond_broadcast(pthread_cond_t *cond)
  122. {
  123. rt_err_t result;
  124. if (cond == RT_NULL)
  125. return EINVAL;
  126. if (cond->attr == -1)
  127. pthread_cond_init(cond, RT_NULL);
  128. while (1)
  129. {
  130. /* try to take condition semaphore */
  131. result = rt_sem_trytake(&(cond->sem));
  132. if (result == -RT_ETIMEOUT)
  133. {
  134. /* it's timeout, release this semaphore */
  135. rt_sem_release(&(cond->sem));
  136. }
  137. else if (result == RT_EOK)
  138. {
  139. /* has taken this semaphore, release it */
  140. rt_sem_release(&(cond->sem));
  141. break;
  142. }
  143. else
  144. {
  145. return EINVAL;
  146. }
  147. }
  148. return 0;
  149. }
  150. RTM_EXPORT(pthread_cond_broadcast);
  151. int pthread_cond_signal(pthread_cond_t *cond)
  152. {
  153. rt_base_t temp;
  154. rt_err_t result;
  155. if (cond == RT_NULL)
  156. return EINVAL;
  157. if (cond->attr == -1)
  158. pthread_cond_init(cond, RT_NULL);
  159. /* disable interrupt */
  160. temp = rt_hw_interrupt_disable();
  161. if (rt_list_isempty(&cond->sem.parent.suspend_thread))
  162. {
  163. /* enable interrupt */
  164. rt_hw_interrupt_enable(temp);
  165. return 0;
  166. }
  167. else
  168. {
  169. /* enable interrupt */
  170. rt_hw_interrupt_enable(temp);
  171. result = rt_sem_release(&(cond->sem));
  172. if (result == RT_EOK)
  173. {
  174. return 0;
  175. }
  176. return 0;
  177. }
  178. }
  179. RTM_EXPORT(pthread_cond_signal);
  180. rt_err_t _pthread_cond_timedwait(pthread_cond_t *cond,
  181. pthread_mutex_t *mutex,
  182. rt_int32_t timeout)
  183. {
  184. rt_err_t result = RT_EOK;
  185. rt_sem_t sem;
  186. rt_int32_t time;
  187. sem = &(cond->sem);
  188. if (sem == RT_NULL)
  189. {
  190. return -RT_ERROR;
  191. }
  192. time = timeout;
  193. if (!cond || !mutex)
  194. {
  195. return -RT_ERROR;
  196. }
  197. /* check whether initialized */
  198. if (cond->attr == -1)
  199. {
  200. pthread_cond_init(cond, RT_NULL);
  201. }
  202. /* The mutex was not owned by the current thread at the time of the call. */
  203. if (mutex->lock.owner != rt_thread_self())
  204. {
  205. return -RT_ERROR;
  206. }
  207. {
  208. register rt_base_t temp;
  209. struct rt_thread *thread;
  210. /* parameter check */
  211. RT_ASSERT(sem != RT_NULL);
  212. RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
  213. /* disable interrupt */
  214. temp = rt_hw_interrupt_disable();
  215. if (sem->value > 0)
  216. {
  217. /* semaphore is available */
  218. sem->value--;
  219. /* enable interrupt */
  220. rt_hw_interrupt_enable(temp);
  221. }
  222. else
  223. {
  224. /* no waiting, return with timeout */
  225. if (time == 0)
  226. {
  227. rt_hw_interrupt_enable(temp);
  228. return -RT_ETIMEOUT;
  229. }
  230. else
  231. {
  232. /* current context checking */
  233. RT_DEBUG_IN_THREAD_CONTEXT;
  234. /* semaphore is unavailable, push to suspend list */
  235. /* get current thread */
  236. thread = rt_thread_self();
  237. /* reset thread error number */
  238. thread->error = RT_EOK;
  239. /* suspend thread */
  240. rt_thread_suspend(thread);
  241. /* Only support FIFO */
  242. rt_list_insert_before(&(sem->parent.suspend_thread), &RT_THREAD_LIST_NODE(thread));
  243. /**
  244. rt_ipc_list_suspend(&(sem->parent.suspend_thread),
  245. thread,
  246. sem->parent.parent.flag);
  247. */
  248. /* has waiting time, start thread timer */
  249. if (time > 0)
  250. {
  251. /* reset the timeout of thread timer and start it */
  252. rt_timer_control(&(thread->thread_timer),
  253. RT_TIMER_CTRL_SET_TIME,
  254. &time);
  255. rt_timer_start(&(thread->thread_timer));
  256. }
  257. /* to avoid the lost of singal< cond->sem > */
  258. if (pthread_mutex_unlock(mutex) != 0)
  259. {
  260. return -RT_ERROR;
  261. }
  262. /* enable interrupt */
  263. rt_hw_interrupt_enable(temp);
  264. /* do schedule */
  265. rt_schedule();
  266. result = thread->error;
  267. /* lock mutex again */
  268. pthread_mutex_lock(mutex);
  269. }
  270. }
  271. }
  272. return result;
  273. }
  274. RTM_EXPORT(_pthread_cond_timedwait);
  275. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  276. {
  277. rt_err_t result;
  278. __retry:
  279. result = _pthread_cond_timedwait(cond, mutex, RT_WAITING_FOREVER);
  280. if (result == RT_EOK)
  281. {
  282. return 0;
  283. }
  284. else if (result == -RT_EINTR)
  285. {
  286. /* https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_wait.html
  287. * These functions shall not return an error code of [EINTR].
  288. */
  289. goto __retry;
  290. }
  291. return EINVAL;
  292. }
  293. RTM_EXPORT(pthread_cond_wait);
  294. int pthread_cond_timedwait(pthread_cond_t *cond,
  295. pthread_mutex_t *mutex,
  296. const struct timespec *abstime)
  297. {
  298. int timeout;
  299. rt_err_t result;
  300. timeout = rt_timespec_to_tick(abstime);
  301. result = _pthread_cond_timedwait(cond, mutex, timeout);
  302. if (result == RT_EOK)
  303. {
  304. return 0;
  305. }
  306. if (result == -RT_ETIMEOUT)
  307. {
  308. return ETIMEDOUT;
  309. }
  310. return EINVAL;
  311. }
  312. RTM_EXPORT(pthread_cond_timedwait);