condvar.c 8.5 KB

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