pthread_cond_wait.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Copyright (C) 2003, 2004 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. /* We are going to modify shared data. */
  37. lll_mutex_lock (cbuffer->cond->__data.__lock);
  38. if (cbuffer->bc_seq == cbuffer->cond->__data.__broadcast_seq)
  39. {
  40. /* This thread is not waiting anymore. Adjust the sequence counters
  41. appropriately. */
  42. ++cbuffer->cond->__data.__wakeup_seq;
  43. ++cbuffer->cond->__data.__woken_seq;
  44. ++cbuffer->cond->__data.__futex;
  45. }
  46. cbuffer->cond->__data.__nwaiters -= 1 << COND_CLOCK_BITS;
  47. /* If pthread_cond_destroy was called on this variable already,
  48. notify the pthread_cond_destroy caller all waiters have left
  49. and it can be successfully destroyed. */
  50. destroying = 0;
  51. if (cbuffer->cond->__data.__total_seq == -1ULL
  52. && cbuffer->cond->__data.__nwaiters < (1 << COND_CLOCK_BITS))
  53. {
  54. lll_futex_wake (&cbuffer->cond->__data.__nwaiters, 1);
  55. destroying = 1;
  56. }
  57. /* We are done. */
  58. lll_mutex_unlock (cbuffer->cond->__data.__lock);
  59. /* Wake everybody to make sure no condvar signal gets lost. */
  60. if (! destroying)
  61. lll_futex_wake (&cbuffer->cond->__data.__futex, INT_MAX);
  62. /* Get the mutex before returning unless asynchronous cancellation
  63. is in effect. */
  64. __pthread_mutex_cond_lock (cbuffer->mutex);
  65. }
  66. int
  67. __pthread_cond_wait (cond, mutex)
  68. pthread_cond_t *cond;
  69. pthread_mutex_t *mutex;
  70. {
  71. struct _pthread_cleanup_buffer buffer;
  72. struct _condvar_cleanup_buffer cbuffer;
  73. int err;
  74. /* Make sure we are along. */
  75. lll_mutex_lock (cond->__data.__lock);
  76. /* Now we can release the mutex. */
  77. err = __pthread_mutex_unlock_usercnt (mutex, 0);
  78. if (__builtin_expect (err, 0))
  79. {
  80. lll_mutex_unlock (cond->__data.__lock);
  81. return err;
  82. }
  83. /* We have one new user of the condvar. */
  84. ++cond->__data.__total_seq;
  85. ++cond->__data.__futex;
  86. cond->__data.__nwaiters += 1 << COND_CLOCK_BITS;
  87. /* Remember the mutex we are using here. If there is already a
  88. different address store this is a bad user bug. Do not store
  89. anything for pshared condvars. */
  90. if (cond->__data.__mutex != (void *) ~0l)
  91. cond->__data.__mutex = mutex;
  92. /* Prepare structure passed to cancellation handler. */
  93. cbuffer.cond = cond;
  94. cbuffer.mutex = mutex;
  95. /* Before we block we enable cancellation. Therefore we have to
  96. install a cancellation handler. */
  97. __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
  98. /* The current values of the wakeup counter. The "woken" counter
  99. must exceed this value. */
  100. unsigned long long int val;
  101. unsigned long long int seq;
  102. val = seq = cond->__data.__wakeup_seq;
  103. /* Remember the broadcast counter. */
  104. cbuffer.bc_seq = cond->__data.__broadcast_seq;
  105. do
  106. {
  107. unsigned int futex_val = cond->__data.__futex;
  108. /* Prepare to wait. Release the condvar futex. */
  109. lll_mutex_unlock (cond->__data.__lock);
  110. /* Enable asynchronous cancellation. Required by the standard. */
  111. cbuffer.oldtype = __pthread_enable_asynccancel ();
  112. /* Wait until woken by signal or broadcast. */
  113. lll_futex_wait (&cond->__data.__futex, futex_val);
  114. /* Disable asynchronous cancellation. */
  115. __pthread_disable_asynccancel (cbuffer.oldtype);
  116. /* We are going to look at shared data again, so get the lock. */
  117. lll_mutex_lock (cond->__data.__lock);
  118. /* If a broadcast happened, we are done. */
  119. if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
  120. goto bc_out;
  121. /* Check whether we are eligible for wakeup. */
  122. val = cond->__data.__wakeup_seq;
  123. }
  124. while (val == seq || cond->__data.__woken_seq == val);
  125. /* Another thread woken up. */
  126. ++cond->__data.__woken_seq;
  127. bc_out:
  128. cond->__data.__nwaiters -= 1 << COND_CLOCK_BITS;
  129. /* If pthread_cond_destroy was called on this varaible already,
  130. notify the pthread_cond_destroy caller all waiters have left
  131. and it can be successfully destroyed. */
  132. if (cond->__data.__total_seq == -1ULL
  133. && cond->__data.__nwaiters < (1 << COND_CLOCK_BITS))
  134. lll_futex_wake (&cond->__data.__nwaiters, 1);
  135. /* We are done with the condvar. */
  136. lll_mutex_unlock (cond->__data.__lock);
  137. /* The cancellation handling is back to normal, remove the handler. */
  138. __pthread_cleanup_pop (&buffer, 0);
  139. /* Get the mutex before returning. */
  140. return __pthread_mutex_cond_lock (mutex);
  141. }
  142. weak_alias(__pthread_cond_wait, pthread_cond_wait)