condvar.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. libpthread_hidden_proto(pthread_cond_broadcast)
  26. libpthread_hidden_proto(pthread_cond_destroy)
  27. libpthread_hidden_proto(pthread_cond_init)
  28. libpthread_hidden_proto(pthread_cond_signal)
  29. libpthread_hidden_proto(pthread_cond_wait)
  30. libpthread_hidden_proto(pthread_cond_timedwait)
  31. libpthread_hidden_proto(pthread_condattr_destroy)
  32. libpthread_hidden_proto(pthread_condattr_init)
  33. int pthread_cond_init(pthread_cond_t *cond,
  34. const pthread_condattr_t *cond_attr attribute_unused)
  35. {
  36. __pthread_init_lock(&cond->__c_lock);
  37. cond->__c_waiting = NULL;
  38. return 0;
  39. }
  40. libpthread_hidden_def(pthread_cond_init)
  41. int pthread_cond_destroy(pthread_cond_t *cond)
  42. {
  43. if (cond->__c_waiting != NULL) return EBUSY;
  44. return 0;
  45. }
  46. libpthread_hidden_def(pthread_cond_destroy)
  47. /* Function called by pthread_cancel to remove the thread from
  48. waiting on a condition variable queue. */
  49. static int cond_extricate_func(void *obj, pthread_descr th)
  50. {
  51. volatile pthread_descr self = thread_self();
  52. pthread_cond_t *cond = obj;
  53. int did_remove = 0;
  54. __pthread_lock(&cond->__c_lock, self);
  55. did_remove = remove_from_queue(&cond->__c_waiting, th);
  56. __pthread_unlock(&cond->__c_lock);
  57. return did_remove;
  58. }
  59. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  60. {
  61. volatile pthread_descr self = thread_self();
  62. pthread_extricate_if extr;
  63. int already_canceled = 0;
  64. int spurious_wakeup_count;
  65. /* Check whether the mutex is locked and owned by this thread. */
  66. if (mutex->__m_kind != PTHREAD_MUTEX_TIMED_NP
  67. && mutex->__m_kind != PTHREAD_MUTEX_ADAPTIVE_NP
  68. && mutex->__m_owner != self)
  69. return EINVAL;
  70. /* Set up extrication interface */
  71. extr.pu_object = cond;
  72. extr.pu_extricate_func = cond_extricate_func;
  73. /* Register extrication interface */
  74. THREAD_SETMEM(self, p_condvar_avail, 0);
  75. __pthread_set_own_extricate_if(self, &extr);
  76. /* Atomically enqueue thread for waiting, but only if it is not
  77. canceled. If the thread is canceled, then it will fall through the
  78. suspend call below, and then call pthread_exit without
  79. having to worry about whether it is still on the condition variable queue.
  80. This depends on pthread_cancel setting p_canceled before calling the
  81. extricate function. */
  82. __pthread_lock(&cond->__c_lock, self);
  83. if (!(THREAD_GETMEM(self, p_canceled)
  84. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
  85. enqueue(&cond->__c_waiting, self);
  86. else
  87. already_canceled = 1;
  88. __pthread_unlock(&cond->__c_lock);
  89. if (already_canceled) {
  90. __pthread_set_own_extricate_if(self, 0);
  91. __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
  92. }
  93. __pthread_mutex_unlock(mutex);
  94. spurious_wakeup_count = 0;
  95. while (1)
  96. {
  97. suspend(self);
  98. if (THREAD_GETMEM(self, p_condvar_avail) == 0
  99. && (THREAD_GETMEM(self, p_woken_by_cancel) == 0
  100. || THREAD_GETMEM(self, p_cancelstate) != PTHREAD_CANCEL_ENABLE))
  101. {
  102. /* Count resumes that don't belong to us. */
  103. spurious_wakeup_count++;
  104. continue;
  105. }
  106. break;
  107. }
  108. __pthread_set_own_extricate_if(self, 0);
  109. /* Check for cancellation again, to provide correct cancellation
  110. point behavior */
  111. if (THREAD_GETMEM(self, p_woken_by_cancel)
  112. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
  113. THREAD_SETMEM(self, p_woken_by_cancel, 0);
  114. __pthread_mutex_lock(mutex);
  115. __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
  116. }
  117. /* Put back any resumes we caught that don't belong to us. */
  118. while (spurious_wakeup_count--)
  119. restart(self);
  120. __pthread_mutex_lock(mutex);
  121. return 0;
  122. }
  123. libpthread_hidden_def(pthread_cond_wait)
  124. static int
  125. pthread_cond_timedwait_relative(pthread_cond_t *cond,
  126. pthread_mutex_t *mutex,
  127. const struct timespec * abstime)
  128. {
  129. volatile pthread_descr self = thread_self();
  130. int already_canceled = 0;
  131. pthread_extricate_if extr;
  132. int spurious_wakeup_count;
  133. /* Check whether the mutex is locked and owned by this thread. */
  134. if (mutex->__m_kind != PTHREAD_MUTEX_TIMED_NP
  135. && mutex->__m_kind != PTHREAD_MUTEX_ADAPTIVE_NP
  136. && mutex->__m_owner != self)
  137. return EINVAL;
  138. /* Set up extrication interface */
  139. extr.pu_object = cond;
  140. extr.pu_extricate_func = cond_extricate_func;
  141. /* Register extrication interface */
  142. THREAD_SETMEM(self, p_condvar_avail, 0);
  143. __pthread_set_own_extricate_if(self, &extr);
  144. /* Enqueue to wait on the condition and check for cancellation. */
  145. __pthread_lock(&cond->__c_lock, self);
  146. if (!(THREAD_GETMEM(self, p_canceled)
  147. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
  148. enqueue(&cond->__c_waiting, self);
  149. else
  150. already_canceled = 1;
  151. __pthread_unlock(&cond->__c_lock);
  152. if (already_canceled) {
  153. __pthread_set_own_extricate_if(self, 0);
  154. __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
  155. }
  156. __pthread_mutex_unlock(mutex);
  157. spurious_wakeup_count = 0;
  158. while (1)
  159. {
  160. if (!timedsuspend(self, abstime)) {
  161. int was_on_queue;
  162. /* __pthread_lock will queue back any spurious restarts that
  163. may happen to it. */
  164. __pthread_lock(&cond->__c_lock, self);
  165. was_on_queue = remove_from_queue(&cond->__c_waiting, self);
  166. __pthread_unlock(&cond->__c_lock);
  167. if (was_on_queue) {
  168. __pthread_set_own_extricate_if(self, 0);
  169. __pthread_mutex_lock(mutex);
  170. return ETIMEDOUT;
  171. }
  172. /* Eat the outstanding restart() from the signaller */
  173. suspend(self);
  174. }
  175. if (THREAD_GETMEM(self, p_condvar_avail) == 0
  176. && (THREAD_GETMEM(self, p_woken_by_cancel) == 0
  177. || THREAD_GETMEM(self, p_cancelstate) != PTHREAD_CANCEL_ENABLE))
  178. {
  179. /* Count resumes that don't belong to us. */
  180. spurious_wakeup_count++;
  181. continue;
  182. }
  183. break;
  184. }
  185. __pthread_set_own_extricate_if(self, 0);
  186. /* The remaining logic is the same as in other cancellable waits,
  187. such as pthread_join sem_wait or pthread_cond wait. */
  188. if (THREAD_GETMEM(self, p_woken_by_cancel)
  189. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
  190. THREAD_SETMEM(self, p_woken_by_cancel, 0);
  191. __pthread_mutex_lock(mutex);
  192. __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
  193. }
  194. /* Put back any resumes we caught that don't belong to us. */
  195. while (spurious_wakeup_count--)
  196. restart(self);
  197. __pthread_mutex_lock(mutex);
  198. return 0;
  199. }
  200. int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
  201. const struct timespec * abstime)
  202. {
  203. /* Indirect call through pointer! */
  204. return pthread_cond_timedwait_relative(cond, mutex, abstime);
  205. }
  206. libpthread_hidden_def(pthread_cond_timedwait)
  207. int pthread_cond_signal(pthread_cond_t *cond)
  208. {
  209. pthread_descr th;
  210. __pthread_lock(&cond->__c_lock, NULL);
  211. th = dequeue(&cond->__c_waiting);
  212. __pthread_unlock(&cond->__c_lock);
  213. if (th != NULL) {
  214. th->p_condvar_avail = 1;
  215. WRITE_MEMORY_BARRIER();
  216. restart(th);
  217. }
  218. return 0;
  219. }
  220. libpthread_hidden_def(pthread_cond_signal)
  221. int pthread_cond_broadcast(pthread_cond_t *cond)
  222. {
  223. pthread_descr tosignal, th;
  224. __pthread_lock(&cond->__c_lock, NULL);
  225. /* Copy the current state of the waiting queue and empty it */
  226. tosignal = cond->__c_waiting;
  227. cond->__c_waiting = NULL;
  228. __pthread_unlock(&cond->__c_lock);
  229. /* Now signal each process in the queue */
  230. while ((th = dequeue(&tosignal)) != NULL) {
  231. th->p_condvar_avail = 1;
  232. WRITE_MEMORY_BARRIER();
  233. restart(th);
  234. }
  235. return 0;
  236. }
  237. libpthread_hidden_def(pthread_cond_broadcast)
  238. int pthread_condattr_init(pthread_condattr_t *attr attribute_unused)
  239. {
  240. return 0;
  241. }
  242. libpthread_hidden_def(pthread_condattr_init)
  243. int pthread_condattr_destroy(pthread_condattr_t *attr attribute_unused)
  244. {
  245. return 0;
  246. }
  247. libpthread_hidden_def(pthread_condattr_destroy)
  248. int pthread_condattr_getpshared (const pthread_condattr_t *attr attribute_unused, int *pshared)
  249. {
  250. *pshared = PTHREAD_PROCESS_PRIVATE;
  251. return 0;
  252. }
  253. int pthread_condattr_setpshared (pthread_condattr_t *attr attribute_unused, int pshared)
  254. {
  255. if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
  256. return EINVAL;
  257. /* For now it is not possible to shared a conditional variable. */
  258. if (pshared != PTHREAD_PROCESS_PRIVATE)
  259. return ENOSYS;
  260. return 0;
  261. }