condvar.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* Linuxthreads - a simple clone()-based implementation of Posix */
  2. /* threads for Linux. */
  3. /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
  4. /* and Pavel Krauz (krauz@fsid.cvut.cz). */
  5. /* */
  6. /* This program is free software; you can redistribute it and/or */
  7. /* modify it under the terms of the GNU Library General Public License */
  8. /* as published by the Free Software Foundation; either version 2 */
  9. /* of the License, or (at your option) any later version. */
  10. /* */
  11. /* This program is distributed in the hope that it will be useful, */
  12. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  13. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  14. /* GNU Library General Public License for more details. */
  15. /* Condition variables */
  16. #include <errno.h>
  17. #include <sched.h>
  18. #include <stddef.h>
  19. #include <sys/time.h>
  20. #include "pthread.h"
  21. #include "internals.h"
  22. #include "spinlock.h"
  23. #include "queue.h"
  24. #include "restart.h"
  25. /* glibc uses strong aliases, we wont bother */
  26. #undef strong_alias
  27. #define strong_alias(sym, alias)
  28. #define __pthread_cond_init pthread_cond_init
  29. #define __pthread_cond_destroy pthread_cond_destroy
  30. #define __pthread_cond_wait pthread_cond_wait
  31. #define __pthread_cond_timedwait pthread_cond_timedwait
  32. #define __pthread_cond_signal pthread_cond_signal
  33. #define __pthread_cond_broadcast pthread_cond_broadcast
  34. #define __pthread_condattr_init pthread_condattr_init
  35. #define __pthread_condattr_destroy pthread_condattr_destroy
  36. int __pthread_cond_init(pthread_cond_t *cond,
  37. const pthread_condattr_t *cond_attr)
  38. {
  39. __pthread_init_lock(&cond->__c_lock);
  40. cond->__c_waiting = NULL;
  41. return 0;
  42. }
  43. /* Don't bother with this version stuff in uClibc */
  44. #if 0
  45. versioned_symbol (libpthread, __pthread_cond_init, pthread_cond_init,
  46. GLIBC_2_3_2);
  47. #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2)
  48. strong_alias (__pthread_cond_init, __old_pthread_cond_init)
  49. compat_symbol (libpthread, __old_pthread_cond_init, pthread_cond_init,
  50. GLIBC_2_0);
  51. #endif
  52. #endif
  53. int __pthread_cond_destroy(pthread_cond_t *cond)
  54. {
  55. if (cond->__c_waiting != NULL) return EBUSY;
  56. return 0;
  57. }
  58. /* Don't bother with this version stuff in uClibc */
  59. #if 0
  60. versioned_symbol (libpthread, __pthread_cond_destroy, pthread_cond_destroy,
  61. GLIBC_2_3_2);
  62. #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2)
  63. strong_alias (__pthread_cond_destroy, __old_pthread_cond_destroy)
  64. compat_symbol (libpthread, __old_pthread_cond_destroy, pthread_cond_destroy,
  65. GLIBC_2_0);
  66. #endif
  67. #endif
  68. /* Function called by pthread_cancel to remove the thread from
  69. waiting on a condition variable queue. */
  70. static int cond_extricate_func(void *obj, pthread_descr th)
  71. {
  72. volatile pthread_descr self = thread_self();
  73. pthread_cond_t *cond = obj;
  74. int did_remove = 0;
  75. __pthread_lock(&cond->__c_lock, self);
  76. did_remove = remove_from_queue(&cond->__c_waiting, th);
  77. __pthread_unlock(&cond->__c_lock);
  78. return did_remove;
  79. }
  80. int __pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  81. {
  82. volatile pthread_descr self = thread_self();
  83. pthread_extricate_if extr;
  84. int already_canceled = 0;
  85. int spurious_wakeup_count;
  86. /* Check whether the mutex is locked and owned by this thread. */
  87. if (mutex->__m_kind != PTHREAD_MUTEX_TIMED_NP
  88. && mutex->__m_kind != PTHREAD_MUTEX_ADAPTIVE_NP
  89. && mutex->__m_owner != self)
  90. return EINVAL;
  91. /* Set up extrication interface */
  92. extr.pu_object = cond;
  93. extr.pu_extricate_func = cond_extricate_func;
  94. /* Register extrication interface */
  95. THREAD_SETMEM(self, p_condvar_avail, 0);
  96. __pthread_set_own_extricate_if(self, &extr);
  97. /* Atomically enqueue thread for waiting, but only if it is not
  98. canceled. If the thread is canceled, then it will fall through the
  99. suspend call below, and then call pthread_exit without
  100. having to worry about whether it is still on the condition variable queue.
  101. This depends on pthread_cancel setting p_canceled before calling the
  102. extricate function. */
  103. __pthread_lock(&cond->__c_lock, self);
  104. if (!(THREAD_GETMEM(self, p_canceled)
  105. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
  106. enqueue(&cond->__c_waiting, self);
  107. else
  108. already_canceled = 1;
  109. __pthread_unlock(&cond->__c_lock);
  110. if (already_canceled) {
  111. __pthread_set_own_extricate_if(self, 0);
  112. pthread_exit(PTHREAD_CANCELED);
  113. }
  114. pthread_mutex_unlock(mutex);
  115. spurious_wakeup_count = 0;
  116. while (1)
  117. {
  118. suspend(self);
  119. if (THREAD_GETMEM(self, p_condvar_avail) == 0
  120. && (THREAD_GETMEM(self, p_woken_by_cancel) == 0
  121. || THREAD_GETMEM(self, p_cancelstate) != PTHREAD_CANCEL_ENABLE))
  122. {
  123. /* Count resumes that don't belong to us. */
  124. spurious_wakeup_count++;
  125. continue;
  126. }
  127. break;
  128. }
  129. __pthread_set_own_extricate_if(self, 0);
  130. /* Check for cancellation again, to provide correct cancellation
  131. point behavior */
  132. if (THREAD_GETMEM(self, p_woken_by_cancel)
  133. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
  134. THREAD_SETMEM(self, p_woken_by_cancel, 0);
  135. pthread_mutex_lock(mutex);
  136. pthread_exit(PTHREAD_CANCELED);
  137. }
  138. /* Put back any resumes we caught that don't belong to us. */
  139. while (spurious_wakeup_count--)
  140. restart(self);
  141. pthread_mutex_lock(mutex);
  142. return 0;
  143. }
  144. /* Don't bother with this version stuff in uClibc */
  145. #if 0
  146. versioned_symbol (libpthread, __pthread_cond_wait, pthread_cond_wait,
  147. GLIBC_2_3_2);
  148. #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2)
  149. strong_alias (__pthread_cond_wait, __old_pthread_cond_wait)
  150. compat_symbol (libpthread, __old_pthread_cond_wait, pthread_cond_wait,
  151. GLIBC_2_0);
  152. #endif
  153. #endif
  154. static int
  155. pthread_cond_timedwait_relative(pthread_cond_t *cond,
  156. pthread_mutex_t *mutex,
  157. const struct timespec * abstime)
  158. {
  159. volatile pthread_descr self = thread_self();
  160. int already_canceled = 0;
  161. pthread_extricate_if extr;
  162. int spurious_wakeup_count;
  163. /* Check whether the mutex is locked and owned by this thread. */
  164. if (mutex->__m_kind != PTHREAD_MUTEX_TIMED_NP
  165. && mutex->__m_kind != PTHREAD_MUTEX_ADAPTIVE_NP
  166. && mutex->__m_owner != self)
  167. return EINVAL;
  168. /* Set up extrication interface */
  169. extr.pu_object = cond;
  170. extr.pu_extricate_func = cond_extricate_func;
  171. /* Register extrication interface */
  172. THREAD_SETMEM(self, p_condvar_avail, 0);
  173. __pthread_set_own_extricate_if(self, &extr);
  174. /* Enqueue to wait on the condition and check for cancellation. */
  175. __pthread_lock(&cond->__c_lock, self);
  176. if (!(THREAD_GETMEM(self, p_canceled)
  177. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
  178. enqueue(&cond->__c_waiting, self);
  179. else
  180. already_canceled = 1;
  181. __pthread_unlock(&cond->__c_lock);
  182. if (already_canceled) {
  183. __pthread_set_own_extricate_if(self, 0);
  184. pthread_exit(PTHREAD_CANCELED);
  185. }
  186. pthread_mutex_unlock(mutex);
  187. spurious_wakeup_count = 0;
  188. while (1)
  189. {
  190. if (!timedsuspend(self, abstime)) {
  191. int was_on_queue;
  192. /* __pthread_lock will queue back any spurious restarts that
  193. may happen to it. */
  194. __pthread_lock(&cond->__c_lock, self);
  195. was_on_queue = remove_from_queue(&cond->__c_waiting, self);
  196. __pthread_unlock(&cond->__c_lock);
  197. if (was_on_queue) {
  198. __pthread_set_own_extricate_if(self, 0);
  199. pthread_mutex_lock(mutex);
  200. return ETIMEDOUT;
  201. }
  202. /* Eat the outstanding restart() from the signaller */
  203. suspend(self);
  204. }
  205. if (THREAD_GETMEM(self, p_condvar_avail) == 0
  206. && (THREAD_GETMEM(self, p_woken_by_cancel) == 0
  207. || THREAD_GETMEM(self, p_cancelstate) != PTHREAD_CANCEL_ENABLE))
  208. {
  209. /* Count resumes that don't belong to us. */
  210. spurious_wakeup_count++;
  211. continue;
  212. }
  213. break;
  214. }
  215. __pthread_set_own_extricate_if(self, 0);
  216. /* The remaining logic is the same as in other cancellable waits,
  217. such as pthread_join sem_wait or pthread_cond wait. */
  218. if (THREAD_GETMEM(self, p_woken_by_cancel)
  219. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
  220. THREAD_SETMEM(self, p_woken_by_cancel, 0);
  221. pthread_mutex_lock(mutex);
  222. pthread_exit(PTHREAD_CANCELED);
  223. }
  224. /* Put back any resumes we caught that don't belong to us. */
  225. while (spurious_wakeup_count--)
  226. restart(self);
  227. pthread_mutex_lock(mutex);
  228. return 0;
  229. }
  230. int __pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
  231. const struct timespec * abstime)
  232. {
  233. /* Indirect call through pointer! */
  234. return pthread_cond_timedwait_relative(cond, mutex, abstime);
  235. }
  236. /* Don't bother with this version stuff in uClibc */
  237. #if 0
  238. versioned_symbol (libpthread, __pthread_cond_timedwait, pthread_cond_timedwait,
  239. GLIBC_2_3_2);
  240. #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2)
  241. strong_alias (__pthread_cond_timedwait, __old_pthread_cond_timedwait)
  242. compat_symbol (libpthread, __old_pthread_cond_timedwait,
  243. pthread_cond_timedwait, GLIBC_2_0);
  244. #endif
  245. #endif
  246. int __pthread_cond_signal(pthread_cond_t *cond)
  247. {
  248. pthread_descr th;
  249. __pthread_lock(&cond->__c_lock, NULL);
  250. th = dequeue(&cond->__c_waiting);
  251. __pthread_unlock(&cond->__c_lock);
  252. if (th != NULL) {
  253. th->p_condvar_avail = 1;
  254. WRITE_MEMORY_BARRIER();
  255. restart(th);
  256. }
  257. return 0;
  258. }
  259. /* Don't bother with this version stuff in uClibc */
  260. #if 0
  261. versioned_symbol (libpthread, __pthread_cond_signal, pthread_cond_signal,
  262. GLIBC_2_3_2);
  263. #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2)
  264. strong_alias (__pthread_cond_signal, __old_pthread_cond_signal)
  265. compat_symbol (libpthread, __old_pthread_cond_signal, pthread_cond_signal,
  266. GLIBC_2_0);
  267. #endif
  268. #endif
  269. int __pthread_cond_broadcast(pthread_cond_t *cond)
  270. {
  271. pthread_descr tosignal, th;
  272. __pthread_lock(&cond->__c_lock, NULL);
  273. /* Copy the current state of the waiting queue and empty it */
  274. tosignal = cond->__c_waiting;
  275. cond->__c_waiting = NULL;
  276. __pthread_unlock(&cond->__c_lock);
  277. /* Now signal each process in the queue */
  278. while ((th = dequeue(&tosignal)) != NULL) {
  279. th->p_condvar_avail = 1;
  280. WRITE_MEMORY_BARRIER();
  281. restart(th);
  282. }
  283. return 0;
  284. }
  285. /* Don't bother with this version stuff in uClibc */
  286. #if 0
  287. versioned_symbol (libpthread, __pthread_cond_broadcast, pthread_cond_broadcast,
  288. GLIBC_2_3_2);
  289. #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2)
  290. strong_alias (__pthread_cond_broadcast, __old_pthread_cond_broadcast)
  291. compat_symbol (libpthread, __old_pthread_cond_broadcast,
  292. pthread_cond_broadcast, GLIBC_2_0);
  293. #endif
  294. #endif
  295. int __pthread_condattr_init(pthread_condattr_t *attr)
  296. {
  297. return 0;
  298. }
  299. strong_alias (__pthread_condattr_init, pthread_condattr_init)
  300. int __pthread_condattr_destroy(pthread_condattr_t *attr)
  301. {
  302. return 0;
  303. }
  304. strong_alias (__pthread_condattr_destroy, pthread_condattr_destroy)
  305. int pthread_condattr_getpshared (const pthread_condattr_t *attr, int *pshared)
  306. {
  307. *pshared = PTHREAD_PROCESS_PRIVATE;
  308. return 0;
  309. }
  310. int pthread_condattr_setpshared (pthread_condattr_t *attr, int pshared)
  311. {
  312. if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
  313. return EINVAL;
  314. /* For now it is not possible to shared a conditional variable. */
  315. if (pshared != PTHREAD_PROCESS_PRIVATE)
  316. return ENOSYS;
  317. return 0;
  318. }