pthread_cond_wait.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* Copyright (C) 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>, 2003.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <endian.h>
  16. #include <errno.h>
  17. #include <sysdep.h>
  18. #include <lowlevellock.h>
  19. #include <pthread.h>
  20. #include <pthreadP.h>
  21. struct _condvar_cleanup_buffer
  22. {
  23. int oldtype;
  24. pthread_cond_t *cond;
  25. pthread_mutex_t *mutex;
  26. unsigned int bc_seq;
  27. };
  28. void
  29. __attribute__ ((visibility ("hidden")))
  30. __condvar_cleanup (void *arg)
  31. {
  32. struct _condvar_cleanup_buffer *cbuffer =
  33. (struct _condvar_cleanup_buffer *) arg;
  34. unsigned int destroying;
  35. int pshared = (cbuffer->cond->__data.__mutex == (void *) ~0l)
  36. ? LLL_SHARED : LLL_PRIVATE;
  37. /* We are going to modify shared data. */
  38. lll_lock (cbuffer->cond->__data.__lock, pshared);
  39. if (cbuffer->bc_seq == cbuffer->cond->__data.__broadcast_seq)
  40. {
  41. /* This thread is not waiting anymore. Adjust the sequence counters
  42. appropriately. We do not increment WAKEUP_SEQ if this would
  43. bump it over the value of TOTAL_SEQ. This can happen if a thread
  44. was woken and then canceled. */
  45. if (cbuffer->cond->__data.__wakeup_seq
  46. < cbuffer->cond->__data.__total_seq)
  47. {
  48. ++cbuffer->cond->__data.__wakeup_seq;
  49. ++cbuffer->cond->__data.__futex;
  50. }
  51. ++cbuffer->cond->__data.__woken_seq;
  52. }
  53. cbuffer->cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
  54. /* If pthread_cond_destroy was called on this variable already,
  55. notify the pthread_cond_destroy caller all waiters have left
  56. and it can be successfully destroyed. */
  57. destroying = 0;
  58. if (cbuffer->cond->__data.__total_seq == -1ULL
  59. && cbuffer->cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT))
  60. {
  61. lll_futex_wake (&cbuffer->cond->__data.__nwaiters, 1, pshared);
  62. destroying = 1;
  63. }
  64. /* We are done. */
  65. lll_unlock (cbuffer->cond->__data.__lock, pshared);
  66. /* Wake everybody to make sure no condvar signal gets lost. */
  67. if (! destroying)
  68. lll_futex_wake (&cbuffer->cond->__data.__futex, INT_MAX, pshared);
  69. /* Get the mutex before returning unless asynchronous cancellation
  70. is in effect. */
  71. __pthread_mutex_cond_lock (cbuffer->mutex);
  72. }
  73. int
  74. attribute_protected
  75. __pthread_cond_wait (
  76. pthread_cond_t *cond,
  77. pthread_mutex_t *mutex)
  78. {
  79. struct _pthread_cleanup_buffer buffer;
  80. struct _condvar_cleanup_buffer cbuffer;
  81. int err;
  82. int pshared = (cond->__data.__mutex == (void *) ~0l)
  83. ? LLL_SHARED : LLL_PRIVATE;
  84. /* Make sure we are along. */
  85. lll_lock (cond->__data.__lock, pshared);
  86. /* Now we can release the mutex. */
  87. err = __pthread_mutex_unlock_usercnt (mutex, 0);
  88. if (__builtin_expect (err, 0))
  89. {
  90. lll_unlock (cond->__data.__lock, pshared);
  91. return err;
  92. }
  93. /* We have one new user of the condvar. */
  94. ++cond->__data.__total_seq;
  95. ++cond->__data.__futex;
  96. cond->__data.__nwaiters += 1 << COND_NWAITERS_SHIFT;
  97. /* Remember the mutex we are using here. If there is already a
  98. different address store this is a bad user bug. Do not store
  99. anything for pshared condvars. */
  100. if (cond->__data.__mutex != (void *) ~0l)
  101. cond->__data.__mutex = mutex;
  102. /* Prepare structure passed to cancellation handler. */
  103. cbuffer.cond = cond;
  104. cbuffer.mutex = mutex;
  105. /* Before we block we enable cancellation. Therefore we have to
  106. install a cancellation handler. */
  107. __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
  108. /* The current values of the wakeup counter. The "woken" counter
  109. must exceed this value. */
  110. unsigned long long int val;
  111. unsigned long long int seq;
  112. val = seq = cond->__data.__wakeup_seq;
  113. /* Remember the broadcast counter. */
  114. cbuffer.bc_seq = cond->__data.__broadcast_seq;
  115. do
  116. {
  117. unsigned int futex_val = cond->__data.__futex;
  118. /* Prepare to wait. Release the condvar futex. */
  119. lll_unlock (cond->__data.__lock, pshared);
  120. /* Enable asynchronous cancellation. Required by the standard. */
  121. cbuffer.oldtype = __pthread_enable_asynccancel ();
  122. /* Wait until woken by signal or broadcast. */
  123. lll_futex_wait (&cond->__data.__futex, futex_val, pshared);
  124. /* Disable asynchronous cancellation. */
  125. __pthread_disable_asynccancel (cbuffer.oldtype);
  126. /* We are going to look at shared data again, so get the lock. */
  127. lll_lock (cond->__data.__lock, pshared);
  128. /* If a broadcast happened, we are done. */
  129. if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
  130. goto bc_out;
  131. /* Check whether we are eligible for wakeup. */
  132. val = cond->__data.__wakeup_seq;
  133. }
  134. while (val == seq || cond->__data.__woken_seq == val);
  135. /* Another thread woken up. */
  136. ++cond->__data.__woken_seq;
  137. bc_out:
  138. cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
  139. /* If pthread_cond_destroy was called on this varaible already,
  140. notify the pthread_cond_destroy caller all waiters have left
  141. and it can be successfully destroyed. */
  142. if (cond->__data.__total_seq == -1ULL
  143. && cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT))
  144. lll_futex_wake (&cond->__data.__nwaiters, 1, pshared);
  145. /* We are done with the condvar. */
  146. lll_unlock (cond->__data.__lock, pshared);
  147. /* The cancellation handling is back to normal, remove the handler. */
  148. __pthread_cleanup_pop (&buffer, 0);
  149. /* Get the mutex before returning. */
  150. return __pthread_mutex_cond_lock (mutex);
  151. }
  152. weak_alias(__pthread_cond_wait, pthread_cond_wait)