pthread_cond_timedwait.c 6.7 KB

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