pthread_cond_wait.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <endian.h>
  17. #include <errno.h>
  18. #include <sysdep.h>
  19. #include <lowlevellock.h>
  20. #include <pthread.h>
  21. #include <pthreadP.h>
  22. struct _condvar_cleanup_buffer
  23. {
  24. int oldtype;
  25. pthread_cond_t *cond;
  26. pthread_mutex_t *mutex;
  27. unsigned int bc_seq;
  28. };
  29. void
  30. __attribute__ ((visibility ("hidden")))
  31. __condvar_cleanup (void *arg)
  32. {
  33. struct _condvar_cleanup_buffer *cbuffer =
  34. (struct _condvar_cleanup_buffer *) arg;
  35. unsigned int destroying;
  36. int pshared = (cbuffer->cond->__data.__mutex == (void *) ~0l)
  37. ? LLL_SHARED : LLL_PRIVATE;
  38. /* We are going to modify shared data. */
  39. lll_lock (cbuffer->cond->__data.__lock, pshared);
  40. if (cbuffer->bc_seq == cbuffer->cond->__data.__broadcast_seq)
  41. {
  42. /* This thread is not waiting anymore. Adjust the sequence counters
  43. appropriately. We do not increment WAKEUP_SEQ if this would
  44. bump it over the value of TOTAL_SEQ. This can happen if a thread
  45. was woken and then canceled. */
  46. if (cbuffer->cond->__data.__wakeup_seq
  47. < cbuffer->cond->__data.__total_seq)
  48. {
  49. ++cbuffer->cond->__data.__wakeup_seq;
  50. ++cbuffer->cond->__data.__futex;
  51. }
  52. ++cbuffer->cond->__data.__woken_seq;
  53. }
  54. cbuffer->cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
  55. /* If pthread_cond_destroy was called on this variable already,
  56. notify the pthread_cond_destroy caller all waiters have left
  57. and it can be successfully destroyed. */
  58. destroying = 0;
  59. if (cbuffer->cond->__data.__total_seq == -1ULL
  60. && cbuffer->cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT))
  61. {
  62. lll_futex_wake (&cbuffer->cond->__data.__nwaiters, 1, pshared);
  63. destroying = 1;
  64. }
  65. /* We are done. */
  66. lll_unlock (cbuffer->cond->__data.__lock, pshared);
  67. /* Wake everybody to make sure no condvar signal gets lost. */
  68. if (! destroying)
  69. lll_futex_wake (&cbuffer->cond->__data.__futex, INT_MAX, pshared);
  70. /* Get the mutex before returning unless asynchronous cancellation
  71. is in effect. */
  72. __pthread_mutex_cond_lock (cbuffer->mutex);
  73. }
  74. int
  75. attribute_protected
  76. __pthread_cond_wait (
  77. pthread_cond_t *cond,
  78. pthread_mutex_t *mutex)
  79. {
  80. struct _pthread_cleanup_buffer buffer;
  81. struct _condvar_cleanup_buffer cbuffer;
  82. int err;
  83. int pshared = (cond->__data.__mutex == (void *) ~0l)
  84. ? LLL_SHARED : LLL_PRIVATE;
  85. /* Make sure we are along. */
  86. lll_lock (cond->__data.__lock, pshared);
  87. /* Now we can release the mutex. */
  88. err = __pthread_mutex_unlock_usercnt (mutex, 0);
  89. if (__builtin_expect (err, 0))
  90. {
  91. lll_unlock (cond->__data.__lock, pshared);
  92. return err;
  93. }
  94. /* We have one new user of the condvar. */
  95. ++cond->__data.__total_seq;
  96. ++cond->__data.__futex;
  97. cond->__data.__nwaiters += 1 << COND_NWAITERS_SHIFT;
  98. /* Remember the mutex we are using here. If there is already a
  99. different address store this is a bad user bug. Do not store
  100. anything for pshared condvars. */
  101. if (cond->__data.__mutex != (void *) ~0l)
  102. cond->__data.__mutex = mutex;
  103. /* Prepare structure passed to cancellation handler. */
  104. cbuffer.cond = cond;
  105. cbuffer.mutex = mutex;
  106. /* Before we block we enable cancellation. Therefore we have to
  107. install a cancellation handler. */
  108. __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
  109. /* The current values of the wakeup counter. The "woken" counter
  110. must exceed this value. */
  111. unsigned long long int val;
  112. unsigned long long int seq;
  113. val = seq = cond->__data.__wakeup_seq;
  114. /* Remember the broadcast counter. */
  115. cbuffer.bc_seq = cond->__data.__broadcast_seq;
  116. do
  117. {
  118. unsigned int futex_val = cond->__data.__futex;
  119. /* Prepare to wait. Release the condvar futex. */
  120. lll_unlock (cond->__data.__lock, pshared);
  121. /* Enable asynchronous cancellation. Required by the standard. */
  122. cbuffer.oldtype = __pthread_enable_asynccancel ();
  123. /* Wait until woken by signal or broadcast. */
  124. lll_futex_wait (&cond->__data.__futex, futex_val, pshared);
  125. /* Disable asynchronous cancellation. */
  126. __pthread_disable_asynccancel (cbuffer.oldtype);
  127. /* We are going to look at shared data again, so get the lock. */
  128. lll_lock (cond->__data.__lock, pshared);
  129. /* If a broadcast happened, we are done. */
  130. if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
  131. goto bc_out;
  132. /* Check whether we are eligible for wakeup. */
  133. val = cond->__data.__wakeup_seq;
  134. }
  135. while (val == seq || cond->__data.__woken_seq == val);
  136. /* Another thread woken up. */
  137. ++cond->__data.__woken_seq;
  138. bc_out:
  139. cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
  140. /* If pthread_cond_destroy was called on this varaible already,
  141. notify the pthread_cond_destroy caller all waiters have left
  142. and it can be successfully destroyed. */
  143. if (cond->__data.__total_seq == -1ULL
  144. && cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT))
  145. lll_futex_wake (&cond->__data.__nwaiters, 1, pshared);
  146. /* We are done with the condvar. */
  147. lll_unlock (cond->__data.__lock, pshared);
  148. /* The cancellation handling is back to normal, remove the handler. */
  149. __pthread_cleanup_pop (&buffer, 0);
  150. /* Get the mutex before returning. */
  151. return __pthread_mutex_cond_lock (mutex);
  152. }
  153. weak_alias(__pthread_cond_wait, pthread_cond_wait)