condvar.c 10 KB

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