pthread_cond_timedwait.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "pthread_impl.h"
  2. void __pthread_testcancel(void);
  3. int __pthread_mutex_lock(pthread_mutex_t *);
  4. int __pthread_mutex_unlock(pthread_mutex_t *);
  5. int __pthread_setcancelstate(int, int *);
  6. /*
  7. * struct waiter
  8. *
  9. * Waiter objects have automatic storage on the waiting thread, and
  10. * are used in building a linked list representing waiters currently
  11. * waiting on the condition variable or a group of waiters woken
  12. * together by a broadcast or signal; in the case of signal, this is a
  13. * degenerate list of one member.
  14. *
  15. * Waiter lists attached to the condition variable itself are
  16. * protected by the lock on the cv. Detached waiter lists are never
  17. * modified again, but can only be traversed in reverse order, and are
  18. * protected by the "barrier" locks in each node, which are unlocked
  19. * in turn to control wake order.
  20. *
  21. * Since process-shared cond var semantics do not necessarily allow
  22. * one thread to see another's automatic storage (they may be in
  23. * different processes), the waiter list is not used for the
  24. * process-shared case, but the structure is still used to store data
  25. * needed by the cancellation cleanup handler.
  26. */
  27. struct waiter {
  28. struct waiter *prev, *next;
  29. volatile int state, barrier;
  30. volatile int *notify;
  31. };
  32. /* Self-synchronized-destruction-safe lock functions */
  33. static inline void lock(volatile int *l)
  34. {
  35. if (a_cas(l, 0, 1)) {
  36. a_cas(l, 1, 2);
  37. do __wait(l, 0, 2, 1);
  38. while (a_cas(l, 0, 2));
  39. }
  40. }
  41. static inline void unlock(volatile int *l)
  42. {
  43. if (a_swap(l, 0)==2)
  44. __wake(l, 1, 1);
  45. }
  46. static inline void unlock_requeue(volatile int *l, volatile int *r, int w)
  47. {
  48. a_store(l, 0);
  49. if (w) __wake(l, 1, 1);
  50. else __syscall(SYS_futex, l, FUTEX_REQUEUE|FUTEX_PRIVATE, 0, 1, r) != -ENOSYS
  51. || __syscall(SYS_futex, l, FUTEX_REQUEUE, 0, 1, r);
  52. }
  53. enum {
  54. WAITING,
  55. SIGNALED,
  56. LEAVING,
  57. };
  58. int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, const struct timespec *restrict ts)
  59. {
  60. struct waiter node = { 0 };
  61. int e, seq, clock = c->_c_clock, cs, shared=0, oldstate, tmp;
  62. volatile int *fut;
  63. if ((m->_m_type&15) && (m->_m_lock&INT_MAX) != __pthread_self()->tid)
  64. return EPERM;
  65. if (ts && ts->tv_nsec >= 1000000000UL)
  66. return EINVAL;
  67. __pthread_testcancel();
  68. if (c->_c_shared) {
  69. shared = 1;
  70. fut = &c->_c_seq;
  71. seq = c->_c_seq;
  72. a_inc(&c->_c_waiters);
  73. } else {
  74. lock(&c->_c_lock);
  75. seq = node.barrier = 2;
  76. fut = &node.barrier;
  77. node.state = WAITING;
  78. node.next = c->_c_head;
  79. c->_c_head = &node;
  80. if (!c->_c_tail) c->_c_tail = &node;
  81. else node.next->prev = &node;
  82. unlock(&c->_c_lock);
  83. }
  84. __pthread_mutex_unlock(m);
  85. __pthread_setcancelstate(PTHREAD_CANCEL_MASKED, &cs);
  86. if (cs == PTHREAD_CANCEL_DISABLE) __pthread_setcancelstate(cs, 0);
  87. do e = __timedwait_cp(fut, seq, clock, ts, !shared);
  88. while (*fut==seq && (!e || e==EINTR));
  89. if (e == EINTR) e = 0;
  90. if (shared) {
  91. /* Suppress cancellation if a signal was potentially
  92. * consumed; this is a legitimate form of spurious
  93. * wake even if not. */
  94. if (e == ECANCELED && c->_c_seq != seq) e = 0;
  95. if (a_fetch_add(&c->_c_waiters, -1) == -0x7fffffff)
  96. __wake(&c->_c_waiters, 1, 0);
  97. oldstate = WAITING;
  98. goto relock;
  99. }
  100. oldstate = a_cas(&node.state, WAITING, LEAVING);
  101. if (oldstate == WAITING) {
  102. /* Access to cv object is valid because this waiter was not
  103. * yet signaled and a new signal/broadcast cannot return
  104. * after seeing a LEAVING waiter without getting notified
  105. * via the futex notify below. */
  106. lock(&c->_c_lock);
  107. if (c->_c_head == &node) c->_c_head = node.next;
  108. else if (node.prev) node.prev->next = node.next;
  109. if (c->_c_tail == &node) c->_c_tail = node.prev;
  110. else if (node.next) node.next->prev = node.prev;
  111. unlock(&c->_c_lock);
  112. if (node.notify) {
  113. if (a_fetch_add(node.notify, -1)==1)
  114. __wake(node.notify, 1, 1);
  115. }
  116. } else {
  117. /* Lock barrier first to control wake order. */
  118. lock(&node.barrier);
  119. }
  120. relock:
  121. /* Errors locking the mutex override any existing error or
  122. * cancellation, since the caller must see them to know the
  123. * state of the mutex. */
  124. if ((tmp = pthread_mutex_lock(m))) e = tmp;
  125. if (oldstate == WAITING) goto done;
  126. if (!node.next) a_inc(&m->_m_waiters);
  127. /* Unlock the barrier that's holding back the next waiter, and
  128. * either wake it or requeue it to the mutex. */
  129. if (node.prev)
  130. unlock_requeue(&node.prev->barrier, &m->_m_lock, m->_m_type & 128);
  131. else
  132. a_dec(&m->_m_waiters);
  133. /* Since a signal was consumed, cancellation is not permitted. */
  134. if (e == ECANCELED) e = 0;
  135. done:
  136. __pthread_setcancelstate(cs, 0);
  137. if (e == ECANCELED) {
  138. __pthread_testcancel();
  139. __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
  140. }
  141. return e;
  142. }
  143. int __private_cond_signal(pthread_cond_t *c, int n)
  144. {
  145. struct waiter *p, *first=0;
  146. volatile int ref = 0;
  147. int cur;
  148. lock(&c->_c_lock);
  149. for (p=c->_c_tail; n && p; p=p->prev) {
  150. if (a_cas(&p->state, WAITING, SIGNALED) != WAITING) {
  151. ref++;
  152. p->notify = &ref;
  153. } else {
  154. n--;
  155. if (!first) first=p;
  156. }
  157. }
  158. /* Split the list, leaving any remainder on the cv. */
  159. if (p) {
  160. if (p->next) p->next->prev = 0;
  161. p->next = 0;
  162. } else {
  163. c->_c_head = 0;
  164. }
  165. c->_c_tail = p;
  166. unlock(&c->_c_lock);
  167. /* Wait for any waiters in the LEAVING state to remove
  168. * themselves from the list before returning or allowing
  169. * signaled threads to proceed. */
  170. while ((cur = ref)) __wait(&ref, 0, cur, 1);
  171. /* Allow first signaled waiter, if any, to proceed. */
  172. if (first) unlock(&first->barrier);
  173. return 0;
  174. }
  175. weak_alias(__pthread_cond_timedwait, pthread_cond_timedwait);