pthread_cond_timedwait.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* Copyright (C) 2003, 2004, 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. #include <bits/kernel-features.h>
  23. /* Cleanup handler, defined in pthread_cond_wait.c. */
  24. extern void __condvar_cleanup (void *arg)
  25. __attribute__ ((visibility ("hidden")));
  26. struct _condvar_cleanup_buffer
  27. {
  28. int oldtype;
  29. pthread_cond_t *cond;
  30. pthread_mutex_t *mutex;
  31. unsigned int bc_seq;
  32. };
  33. int
  34. attribute_protected
  35. __pthread_cond_timedwait (
  36. pthread_cond_t *cond,
  37. pthread_mutex_t *mutex,
  38. const struct timespec *abstime)
  39. {
  40. struct _pthread_cleanup_buffer buffer;
  41. struct _condvar_cleanup_buffer cbuffer;
  42. int result = 0;
  43. /* Catch invalid parameters. */
  44. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
  45. return EINVAL;
  46. int pshared = (cond->__data.__mutex == (void *) ~0l)
  47. ? LLL_SHARED : LLL_PRIVATE;
  48. /* Make sure we are along. */
  49. lll_lock (cond->__data.__lock, pshared);
  50. /* Now we can release the mutex. */
  51. int err = __pthread_mutex_unlock_usercnt (mutex, 0);
  52. if (err)
  53. {
  54. lll_unlock (cond->__data.__lock, pshared);
  55. return err;
  56. }
  57. /* We have one new user of the condvar. */
  58. ++cond->__data.__total_seq;
  59. ++cond->__data.__futex;
  60. cond->__data.__nwaiters += 1 << COND_NWAITERS_SHIFT;
  61. /* Remember the mutex we are using here. If there is already a
  62. different address store this is a bad user bug. Do not store
  63. anything for pshared condvars. */
  64. if (cond->__data.__mutex != (void *) ~0l)
  65. cond->__data.__mutex = mutex;
  66. /* Prepare structure passed to cancellation handler. */
  67. cbuffer.cond = cond;
  68. cbuffer.mutex = mutex;
  69. /* Before we block we enable cancellation. Therefore we have to
  70. install a cancellation handler. */
  71. __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
  72. /* The current values of the wakeup counter. The "woken" counter
  73. must exceed this value. */
  74. unsigned long long int val;
  75. unsigned long long int seq;
  76. val = seq = cond->__data.__wakeup_seq;
  77. /* Remember the broadcast counter. */
  78. cbuffer.bc_seq = cond->__data.__broadcast_seq;
  79. while (1)
  80. {
  81. struct timespec rt;
  82. {
  83. #ifdef __NR_clock_gettime
  84. INTERNAL_SYSCALL_DECL (err);
  85. # ifndef __ASSUME_POSIX_TIMERS
  86. int ret =
  87. # endif
  88. INTERNAL_SYSCALL (clock_gettime, err, 2,
  89. (cond->__data.__nwaiters
  90. & ((1 << COND_NWAITERS_SHIFT) - 1)),
  91. &rt);
  92. # ifndef __ASSUME_POSIX_TIMERS
  93. if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (ret, err), 0))
  94. {
  95. struct timeval tv;
  96. (void) gettimeofday (&tv, NULL);
  97. /* Convert the absolute timeout value to a relative timeout. */
  98. rt.tv_sec = abstime->tv_sec - tv.tv_sec;
  99. rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
  100. }
  101. else
  102. # endif
  103. {
  104. /* Convert the absolute timeout value to a relative timeout. */
  105. rt.tv_sec = abstime->tv_sec - rt.tv_sec;
  106. rt.tv_nsec = abstime->tv_nsec - rt.tv_nsec;
  107. }
  108. #else
  109. /* Get the current time. So far we support only one clock. */
  110. struct timeval tv;
  111. (void) gettimeofday (&tv, NULL);
  112. /* Convert the absolute timeout value to a relative timeout. */
  113. rt.tv_sec = abstime->tv_sec - tv.tv_sec;
  114. rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
  115. #endif
  116. }
  117. if (rt.tv_nsec < 0)
  118. {
  119. rt.tv_nsec += 1000000000;
  120. --rt.tv_sec;
  121. }
  122. /* Did we already time out? */
  123. if (__builtin_expect (rt.tv_sec < 0, 0))
  124. {
  125. if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
  126. goto bc_out;
  127. goto timeout;
  128. }
  129. unsigned int futex_val = cond->__data.__futex;
  130. /* Prepare to wait. Release the condvar futex. */
  131. lll_unlock (cond->__data.__lock, pshared);
  132. /* Enable asynchronous cancellation. Required by the standard. */
  133. cbuffer.oldtype = __pthread_enable_asynccancel ();
  134. /* Wait until woken by signal or broadcast. */
  135. err = lll_futex_timed_wait (&cond->__data.__futex,
  136. futex_val, &rt, pshared);
  137. /* Disable asynchronous cancellation. */
  138. __pthread_disable_asynccancel (cbuffer.oldtype);
  139. /* We are going to look at shared data again, so get the lock. */
  140. lll_lock (cond->__data.__lock, pshared);
  141. /* If a broadcast happened, we are done. */
  142. if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
  143. goto bc_out;
  144. /* Check whether we are eligible for wakeup. */
  145. val = cond->__data.__wakeup_seq;
  146. if (val != seq && cond->__data.__woken_seq != val)
  147. break;
  148. /* Not woken yet. Maybe the time expired? */
  149. if (__builtin_expect (err == -ETIMEDOUT, 0))
  150. {
  151. timeout:
  152. /* Yep. Adjust the counters. */
  153. ++cond->__data.__wakeup_seq;
  154. ++cond->__data.__futex;
  155. /* The error value. */
  156. result = ETIMEDOUT;
  157. break;
  158. }
  159. }
  160. /* Another thread woken up. */
  161. ++cond->__data.__woken_seq;
  162. bc_out:
  163. cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
  164. /* If pthread_cond_destroy was called on this variable already,
  165. notify the pthread_cond_destroy caller all waiters have left
  166. and it can be successfully destroyed. */
  167. if (cond->__data.__total_seq == -1ULL
  168. && cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT))
  169. lll_futex_wake (&cond->__data.__nwaiters, 1, pshared);
  170. /* We are done with the condvar. */
  171. lll_unlock (cond->__data.__lock, pshared);
  172. /* The cancellation handling is back to normal, remove the handler. */
  173. __pthread_cleanup_pop (&buffer, 0);
  174. /* Get the mutex before returning. */
  175. err = __pthread_mutex_cond_lock (mutex);
  176. return err ?: result;
  177. }
  178. weak_alias(__pthread_cond_timedwait, pthread_cond_timedwait)