condvar.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. static int pthread_cond_timedwait_relative_old(pthread_cond_t *,
  26. pthread_mutex_t *, const struct timespec *);
  27. static int pthread_cond_timedwait_relative_new(pthread_cond_t *,
  28. pthread_mutex_t *, const struct timespec *);
  29. static int (*pthread_cond_tw_rel)(pthread_cond_t *, pthread_mutex_t *,
  30. const struct timespec *) = pthread_cond_timedwait_relative_old;
  31. /* initialize this module */
  32. void __pthread_init_condvar(int rt_sig_available)
  33. {
  34. if (rt_sig_available)
  35. pthread_cond_tw_rel = pthread_cond_timedwait_relative_new;
  36. }
  37. int pthread_cond_init(pthread_cond_t *cond,
  38. const pthread_condattr_t *cond_attr)
  39. {
  40. __pthread_init_lock(&cond->__c_lock);
  41. cond->__c_waiting = NULL;
  42. return 0;
  43. }
  44. int pthread_cond_destroy(pthread_cond_t *cond)
  45. {
  46. if (cond->__c_waiting != NULL) return EBUSY;
  47. return 0;
  48. }
  49. /* Function called by pthread_cancel to remove the thread from
  50. waiting on a condition variable queue. */
  51. static int cond_extricate_func(void *obj, pthread_descr th)
  52. {
  53. volatile pthread_descr self = thread_self();
  54. pthread_cond_t *cond = obj;
  55. int did_remove = 0;
  56. __pthread_lock(&cond->__c_lock, self);
  57. did_remove = remove_from_queue(&cond->__c_waiting, th);
  58. __pthread_unlock(&cond->__c_lock);
  59. return did_remove;
  60. }
  61. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  62. {
  63. volatile pthread_descr self = thread_self();
  64. pthread_extricate_if extr;
  65. int already_canceled = 0;
  66. /* Set up extrication interface */
  67. extr.pu_object = cond;
  68. extr.pu_extricate_func = cond_extricate_func;
  69. /* Register extrication interface */
  70. __pthread_set_own_extricate_if(self, &extr);
  71. /* Atomically enqueue thread for waiting, but only if it is not
  72. canceled. If the thread is canceled, then it will fall through the
  73. suspend call below, and then call pthread_exit without
  74. having to worry about whether it is still on the condition variable queue.
  75. This depends on pthread_cancel setting p_canceled before calling the
  76. extricate function. */
  77. __pthread_lock(&cond->__c_lock, self);
  78. if (!(THREAD_GETMEM(self, p_canceled)
  79. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
  80. enqueue(&cond->__c_waiting, self);
  81. else
  82. already_canceled = 1;
  83. __pthread_unlock(&cond->__c_lock);
  84. if (already_canceled) {
  85. __pthread_set_own_extricate_if(self, 0);
  86. pthread_exit(PTHREAD_CANCELED);
  87. }
  88. pthread_mutex_unlock(mutex);
  89. suspend(self);
  90. __pthread_set_own_extricate_if(self, 0);
  91. /* Check for cancellation again, to provide correct cancellation
  92. point behavior */
  93. if (THREAD_GETMEM(self, p_woken_by_cancel)
  94. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
  95. THREAD_SETMEM(self, p_woken_by_cancel, 0);
  96. pthread_mutex_lock(mutex);
  97. pthread_exit(PTHREAD_CANCELED);
  98. }
  99. pthread_mutex_lock(mutex);
  100. return 0;
  101. }
  102. /* The following function is used on kernels that don't have rt signals.
  103. SIGUSR1 is used as the restart signal. The different code is needed
  104. because that ordinary signal does not queue. */
  105. static int
  106. pthread_cond_timedwait_relative_old(pthread_cond_t *cond,
  107. pthread_mutex_t *mutex,
  108. const struct timespec * abstime)
  109. {
  110. volatile pthread_descr self = thread_self();
  111. sigset_t unblock, initial_mask;
  112. int already_canceled = 0;
  113. int was_signalled = 0;
  114. sigjmp_buf jmpbuf;
  115. pthread_extricate_if extr;
  116. /* Set up extrication interface */
  117. extr.pu_object = cond;
  118. extr.pu_extricate_func = cond_extricate_func;
  119. /* Register extrication interface */
  120. __pthread_set_own_extricate_if(self, &extr);
  121. /* Enqueue to wait on the condition and check for cancellation. */
  122. __pthread_lock(&cond->__c_lock, self);
  123. if (!(THREAD_GETMEM(self, p_canceled)
  124. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
  125. enqueue(&cond->__c_waiting, self);
  126. else
  127. already_canceled = 1;
  128. __pthread_unlock(&cond->__c_lock);
  129. if (already_canceled) {
  130. __pthread_set_own_extricate_if(self, 0);
  131. pthread_exit(PTHREAD_CANCELED);
  132. }
  133. pthread_mutex_unlock(mutex);
  134. if (atomic_decrement(&self->p_resume_count) == 0) {
  135. /* Set up a longjmp handler for the restart signal, unblock
  136. the signal and sleep. */
  137. if (sigsetjmp(jmpbuf, 1) == 0) {
  138. THREAD_SETMEM(self, p_signal_jmp, &jmpbuf);
  139. THREAD_SETMEM(self, p_signal, 0);
  140. /* Unblock the restart signal */
  141. sigemptyset(&unblock);
  142. sigaddset(&unblock, __pthread_sig_restart);
  143. sigprocmask(SIG_UNBLOCK, &unblock, &initial_mask);
  144. while (1) {
  145. struct timeval now;
  146. struct timespec reltime;
  147. /* Compute a time offset relative to now. */
  148. gettimeofday (&now, NULL);
  149. reltime.tv_nsec = abstime->tv_nsec - now.tv_usec * 1000;
  150. reltime.tv_sec = abstime->tv_sec - now.tv_sec;
  151. if (reltime.tv_nsec < 0) {
  152. reltime.tv_nsec += 1000000000;
  153. reltime.tv_sec -= 1;
  154. }
  155. /* Sleep for the required duration. If woken by a signal, resume waiting
  156. as required by Single Unix Specification. */
  157. if (reltime.tv_sec < 0 || __libc_nanosleep(&reltime, NULL) == 0)
  158. break;
  159. }
  160. /* Block the restart signal again */
  161. sigprocmask(SIG_SETMASK, &initial_mask, NULL);
  162. was_signalled = 0;
  163. } else {
  164. was_signalled = 1;
  165. }
  166. THREAD_SETMEM(self, p_signal_jmp, NULL);
  167. }
  168. /* Now was_signalled is true if we exited the above code
  169. due to the delivery of a restart signal. In that case,
  170. we know we have been dequeued and resumed and that the
  171. resume count is balanced. Otherwise, there are some
  172. cases to consider. First, try to bump up the resume count
  173. back to zero. If it goes to 1, it means restart() was
  174. invoked on this thread. The signal must be consumed
  175. and the count bumped down and everything is cool.
  176. Otherwise, no restart was delivered yet, so we remove
  177. the thread from the queue. If this succeeds, it's a clear
  178. case of timeout. If we fail to remove from the queue, then we
  179. must wait for a restart. */
  180. if (!was_signalled) {
  181. if (atomic_increment(&self->p_resume_count) != -1) {
  182. __pthread_wait_for_restart_signal(self);
  183. atomic_decrement(&self->p_resume_count); /* should be zero now! */
  184. } else {
  185. int was_on_queue;
  186. __pthread_lock(&cond->__c_lock, self);
  187. was_on_queue = remove_from_queue(&cond->__c_waiting, self);
  188. __pthread_unlock(&cond->__c_lock);
  189. if (was_on_queue) {
  190. __pthread_set_own_extricate_if(self, 0);
  191. pthread_mutex_lock(mutex);
  192. return ETIMEDOUT;
  193. }
  194. suspend(self);
  195. }
  196. }
  197. __pthread_set_own_extricate_if(self, 0);
  198. /* The remaining logic is the same as in other cancellable waits,
  199. such as pthread_join sem_wait or pthread_cond wait. */
  200. if (THREAD_GETMEM(self, p_woken_by_cancel)
  201. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
  202. THREAD_SETMEM(self, p_woken_by_cancel, 0);
  203. pthread_mutex_lock(mutex);
  204. pthread_exit(PTHREAD_CANCELED);
  205. }
  206. pthread_mutex_lock(mutex);
  207. return 0;
  208. }
  209. /* The following function is used on new (late 2.1 and 2.2 and higher) kernels
  210. that have rt signals which queue. */
  211. static int
  212. pthread_cond_timedwait_relative_new(pthread_cond_t *cond,
  213. pthread_mutex_t *mutex,
  214. const struct timespec * abstime)
  215. {
  216. volatile pthread_descr self = thread_self();
  217. sigset_t unblock, initial_mask;
  218. int already_canceled = 0;
  219. int was_signalled = 0;
  220. sigjmp_buf jmpbuf;
  221. pthread_extricate_if extr;
  222. /* Set up extrication interface */
  223. extr.pu_object = cond;
  224. extr.pu_extricate_func = cond_extricate_func;
  225. /* Register extrication interface */
  226. __pthread_set_own_extricate_if(self, &extr);
  227. /* Enqueue to wait on the condition and check for cancellation. */
  228. __pthread_lock(&cond->__c_lock, self);
  229. if (!(THREAD_GETMEM(self, p_canceled)
  230. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
  231. enqueue(&cond->__c_waiting, self);
  232. else
  233. already_canceled = 1;
  234. __pthread_unlock(&cond->__c_lock);
  235. if (already_canceled) {
  236. __pthread_set_own_extricate_if(self, 0);
  237. pthread_exit(PTHREAD_CANCELED);
  238. }
  239. pthread_mutex_unlock(mutex);
  240. /* Set up a longjmp handler for the restart signal, unblock
  241. the signal and sleep. */
  242. if (sigsetjmp(jmpbuf, 1) == 0) {
  243. THREAD_SETMEM(self, p_signal_jmp, &jmpbuf);
  244. THREAD_SETMEM(self, p_signal, 0);
  245. /* Unblock the restart signal */
  246. sigemptyset(&unblock);
  247. sigaddset(&unblock, __pthread_sig_restart);
  248. sigprocmask(SIG_UNBLOCK, &unblock, &initial_mask);
  249. while (1) {
  250. struct timeval now;
  251. struct timespec reltime;
  252. /* Compute a time offset relative to now. */
  253. gettimeofday (&now, NULL);
  254. reltime.tv_nsec = abstime->tv_nsec - now.tv_usec * 1000;
  255. reltime.tv_sec = abstime->tv_sec - now.tv_sec;
  256. if (reltime.tv_nsec < 0) {
  257. reltime.tv_nsec += 1000000000;
  258. reltime.tv_sec -= 1;
  259. }
  260. /* Sleep for the required duration. If woken by a signal,
  261. resume waiting as required by Single Unix Specification. */
  262. if (reltime.tv_sec < 0 || __libc_nanosleep(&reltime, NULL) == 0)
  263. break;
  264. }
  265. /* Block the restart signal again */
  266. sigprocmask(SIG_SETMASK, &initial_mask, NULL);
  267. was_signalled = 0;
  268. } else {
  269. was_signalled = 1;
  270. }
  271. THREAD_SETMEM(self, p_signal_jmp, NULL);
  272. /* Now was_signalled is true if we exited the above code
  273. due to the delivery of a restart signal. In that case,
  274. everything is cool. We have been removed from the queue
  275. by the other thread, and consumed its signal.
  276. Otherwise we this thread woke up spontaneously, or due to a signal other
  277. than restart. The next thing to do is to try to remove the thread
  278. from the queue. This may fail due to a race against another thread
  279. trying to do the same. In the failed case, we know we were signalled,
  280. and we may also have to consume a restart signal. */
  281. if (!was_signalled) {
  282. int was_on_queue;
  283. /* __pthread_lock will queue back any spurious restarts that
  284. may happen to it. */
  285. __pthread_lock(&cond->__c_lock, self);
  286. was_on_queue = remove_from_queue(&cond->__c_waiting, self);
  287. __pthread_unlock(&cond->__c_lock);
  288. if (was_on_queue) {
  289. __pthread_set_own_extricate_if(self, 0);
  290. pthread_mutex_lock(mutex);
  291. return ETIMEDOUT;
  292. }
  293. /* Eat the outstanding restart() from the signaller */
  294. suspend(self);
  295. }
  296. __pthread_set_own_extricate_if(self, 0);
  297. /* The remaining logic is the same as in other cancellable waits,
  298. such as pthread_join sem_wait or pthread_cond wait. */
  299. if (THREAD_GETMEM(self, p_woken_by_cancel)
  300. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
  301. THREAD_SETMEM(self, p_woken_by_cancel, 0);
  302. pthread_mutex_lock(mutex);
  303. pthread_exit(PTHREAD_CANCELED);
  304. }
  305. pthread_mutex_lock(mutex);
  306. return 0;
  307. }
  308. int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
  309. const struct timespec * abstime)
  310. {
  311. /* Indirect call through pointer! */
  312. return pthread_cond_tw_rel(cond, mutex, abstime);
  313. }
  314. int pthread_cond_signal(pthread_cond_t *cond)
  315. {
  316. pthread_descr th;
  317. __pthread_lock(&cond->__c_lock, NULL);
  318. th = dequeue(&cond->__c_waiting);
  319. __pthread_unlock(&cond->__c_lock);
  320. if (th != NULL) restart(th);
  321. return 0;
  322. }
  323. int pthread_cond_broadcast(pthread_cond_t *cond)
  324. {
  325. pthread_descr tosignal, th;
  326. __pthread_lock(&cond->__c_lock, NULL);
  327. /* Copy the current state of the waiting queue and empty it */
  328. tosignal = cond->__c_waiting;
  329. cond->__c_waiting = NULL;
  330. __pthread_unlock(&cond->__c_lock);
  331. /* Now signal each process in the queue */
  332. while ((th = dequeue(&tosignal)) != NULL) restart(th);
  333. return 0;
  334. }
  335. int pthread_condattr_init(pthread_condattr_t *attr)
  336. {
  337. return 0;
  338. }
  339. int pthread_condattr_destroy(pthread_condattr_t *attr)
  340. {
  341. return 0;
  342. }