1 /* Copyright (C) 2003-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>, 2003.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
22 #include <lowlevellock.h>
25 #include <kernel-features.h>
27 #include <shlib-compat.h>
29 #ifndef HAVE_CLOCK_GETTIME_VSYSCALL
30 # undef INTERNAL_VSYSCALL
31 # define INTERNAL_VSYSCALL INTERNAL_SYSCALL
32 # undef INLINE_VSYSCALL
33 # define INLINE_VSYSCALL INLINE_SYSCALL
35 # include <bits/libc-vdso.h>
38 /* Cleanup handler, defined in pthread_cond_wait.c. */
39 extern void __condvar_cleanup (void *arg
)
40 __attribute__ ((visibility ("hidden")));
42 struct _condvar_cleanup_buffer
46 pthread_mutex_t
*mutex
;
51 __pthread_cond_timedwait (cond
, mutex
, abstime
)
53 pthread_mutex_t
*mutex
;
54 const struct timespec
*abstime
;
56 struct _pthread_cleanup_buffer buffer
;
57 struct _condvar_cleanup_buffer cbuffer
;
60 /* Catch invalid parameters. */
61 if (abstime
->tv_nsec
< 0 || abstime
->tv_nsec
>= 1000000000)
64 int pshared
= (cond
->__data
.__mutex
== (void *) ~0l)
65 ? LLL_SHARED
: LLL_PRIVATE
;
67 #if (defined lll_futex_timed_wait_requeue_pi \
68 && defined __ASSUME_REQUEUE_PI)
72 /* Make sure we are alone. */
73 lll_lock (cond
->__data
.__lock
, pshared
);
75 /* Now we can release the mutex. */
76 int err
= __pthread_mutex_unlock_usercnt (mutex
, 0);
79 lll_unlock (cond
->__data
.__lock
, pshared
);
83 /* We have one new user of the condvar. */
84 ++cond
->__data
.__total_seq
;
85 ++cond
->__data
.__futex
;
86 cond
->__data
.__nwaiters
+= 1 << COND_NWAITERS_SHIFT
;
88 /* Work around the fact that the kernel rejects negative timeout values
89 despite them being valid. */
90 if (__glibc_unlikely (abstime
->tv_sec
< 0))
93 /* Remember the mutex we are using here. If there is already a
94 different address store this is a bad user bug. Do not store
95 anything for pshared condvars. */
96 if (cond
->__data
.__mutex
!= (void *) ~0l)
97 cond
->__data
.__mutex
= mutex
;
99 /* Prepare structure passed to cancellation handler. */
101 cbuffer
.mutex
= mutex
;
103 /* Before we block we enable cancellation. Therefore we have to
104 install a cancellation handler. */
105 __pthread_cleanup_push (&buffer
, __condvar_cleanup
, &cbuffer
);
107 /* The current values of the wakeup counter. The "woken" counter
108 must exceed this value. */
109 unsigned long long int val
;
110 unsigned long long int seq
;
111 val
= seq
= cond
->__data
.__wakeup_seq
;
112 /* Remember the broadcast counter. */
113 cbuffer
.bc_seq
= cond
->__data
.__broadcast_seq
;
117 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
118 || !defined lll_futex_timed_wait_bitset)
121 # ifdef __NR_clock_gettime
122 INTERNAL_SYSCALL_DECL (err
);
123 (void) INTERNAL_VSYSCALL (clock_gettime
, err
, 2,
124 (cond
->__data
.__nwaiters
125 & ((1 << COND_NWAITERS_SHIFT
) - 1)),
127 /* Convert the absolute timeout value to a relative timeout. */
128 rt
.tv_sec
= abstime
->tv_sec
- rt
.tv_sec
;
129 rt
.tv_nsec
= abstime
->tv_nsec
- rt
.tv_nsec
;
131 /* Get the current time. So far we support only one clock. */
133 (void) gettimeofday (&tv
, NULL
);
135 /* Convert the absolute timeout value to a relative timeout. */
136 rt
.tv_sec
= abstime
->tv_sec
- tv
.tv_sec
;
137 rt
.tv_nsec
= abstime
->tv_nsec
- tv
.tv_usec
* 1000;
142 rt
.tv_nsec
+= 1000000000;
145 /* Did we already time out? */
146 if (__glibc_unlikely (rt
.tv_sec
< 0))
148 if (cbuffer
.bc_seq
!= cond
->__data
.__broadcast_seq
)
155 unsigned int futex_val
= cond
->__data
.__futex
;
157 /* Prepare to wait. Release the condvar futex. */
158 lll_unlock (cond
->__data
.__lock
, pshared
);
160 /* Enable asynchronous cancellation. Required by the standard. */
161 cbuffer
.oldtype
= __pthread_enable_asynccancel ();
163 /* REQUEUE_PI was implemented after FUTEX_CLOCK_REALTIME, so it is sufficient
164 to check just the former. */
165 #if (defined lll_futex_timed_wait_requeue_pi \
166 && defined __ASSUME_REQUEUE_PI)
167 /* If pi_flag remained 1 then it means that we had the lock and the mutex
168 but a spurious waker raced ahead of us. Give back the mutex before
169 going into wait again. */
172 __pthread_mutex_cond_lock_adjust (mutex
);
173 __pthread_mutex_unlock_usercnt (mutex
, 0);
175 pi_flag
= USE_REQUEUE_PI (mutex
);
179 unsigned int clockbit
= (cond
->__data
.__nwaiters
& 1
180 ? 0 : FUTEX_CLOCK_REALTIME
);
181 err
= lll_futex_timed_wait_requeue_pi (&cond
->__data
.__futex
,
182 futex_val
, abstime
, clockbit
,
183 &mutex
->__data
.__lock
,
185 pi_flag
= (err
== 0);
191 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
192 || !defined lll_futex_timed_wait_bitset)
193 /* Wait until woken by signal or broadcast. */
194 err
= lll_futex_timed_wait (&cond
->__data
.__futex
,
195 futex_val
, &rt
, pshared
);
197 unsigned int clockbit
= (cond
->__data
.__nwaiters
& 1
198 ? 0 : FUTEX_CLOCK_REALTIME
);
199 err
= lll_futex_timed_wait_bitset (&cond
->__data
.__futex
, futex_val
,
200 abstime
, clockbit
, pshared
);
204 /* Disable asynchronous cancellation. */
205 __pthread_disable_asynccancel (cbuffer
.oldtype
);
207 /* We are going to look at shared data again, so get the lock. */
208 lll_lock (cond
->__data
.__lock
, pshared
);
210 /* If a broadcast happened, we are done. */
211 if (cbuffer
.bc_seq
!= cond
->__data
.__broadcast_seq
)
214 /* Check whether we are eligible for wakeup. */
215 val
= cond
->__data
.__wakeup_seq
;
216 if (val
!= seq
&& cond
->__data
.__woken_seq
!= val
)
219 /* Not woken yet. Maybe the time expired? */
220 if (__glibc_unlikely (err
== -ETIMEDOUT
))
223 /* Yep. Adjust the counters. */
224 ++cond
->__data
.__wakeup_seq
;
225 ++cond
->__data
.__futex
;
227 /* The error value. */
233 /* Another thread woken up. */
234 ++cond
->__data
.__woken_seq
;
238 cond
->__data
.__nwaiters
-= 1 << COND_NWAITERS_SHIFT
;
240 /* If pthread_cond_destroy was called on this variable already,
241 notify the pthread_cond_destroy caller all waiters have left
242 and it can be successfully destroyed. */
243 if (cond
->__data
.__total_seq
== -1ULL
244 && cond
->__data
.__nwaiters
< (1 << COND_NWAITERS_SHIFT
))
245 lll_futex_wake (&cond
->__data
.__nwaiters
, 1, pshared
);
247 /* We are done with the condvar. */
248 lll_unlock (cond
->__data
.__lock
, pshared
);
250 /* The cancellation handling is back to normal, remove the handler. */
251 __pthread_cleanup_pop (&buffer
, 0);
253 /* Get the mutex before returning. */
254 #if (defined lll_futex_timed_wait_requeue_pi \
255 && defined __ASSUME_REQUEUE_PI)
258 __pthread_mutex_cond_lock_adjust (mutex
);
263 err
= __pthread_mutex_cond_lock (mutex
);
265 return err
?: result
;
268 versioned_symbol (libpthread
, __pthread_cond_timedwait
, pthread_cond_timedwait
,