1 /* Copyright (C) 2003, 2004, 2007, 2010 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #include <lowlevellock.h>
26 #include <kernel-features.h>
28 #include <shlib-compat.h>
30 #ifndef HAVE_CLOCK_GETTIME_VSYSCALL
31 # undef INTERNAL_VSYSCALL
32 # define INTERNAL_VSYSCALL INTERNAL_SYSCALL
33 # undef INLINE_VSYSCALL
34 # define INLINE_VSYSCALL INLINE_SYSCALL
36 # include <bits/libc-vdso.h>
39 /* Cleanup handler, defined in pthread_cond_wait.c. */
40 extern void __condvar_cleanup (void *arg
)
41 __attribute__ ((visibility ("hidden")));
43 struct _condvar_cleanup_buffer
47 pthread_mutex_t
*mutex
;
52 __pthread_cond_timedwait (cond
, mutex
, abstime
)
54 pthread_mutex_t
*mutex
;
55 const struct timespec
*abstime
;
57 struct _pthread_cleanup_buffer buffer
;
58 struct _condvar_cleanup_buffer cbuffer
;
61 /* Catch invalid parameters. */
62 if (abstime
->tv_nsec
< 0 || abstime
->tv_nsec
>= 1000000000)
65 int pshared
= (cond
->__data
.__mutex
== (void *) ~0l)
66 ? LLL_SHARED
: LLL_PRIVATE
;
68 /* Make sure we are along. */
69 lll_lock (cond
->__data
.__lock
, pshared
);
71 /* Now we can release the mutex. */
72 int err
= __pthread_mutex_unlock_usercnt (mutex
, 0);
75 lll_unlock (cond
->__data
.__lock
, pshared
);
79 /* We have one new user of the condvar. */
80 ++cond
->__data
.__total_seq
;
81 ++cond
->__data
.__futex
;
82 cond
->__data
.__nwaiters
+= 1 << COND_NWAITERS_SHIFT
;
84 /* Remember the mutex we are using here. If there is already a
85 different address store this is a bad user bug. Do not store
86 anything for pshared condvars. */
87 if (cond
->__data
.__mutex
!= (void *) ~0l)
88 cond
->__data
.__mutex
= mutex
;
90 /* Prepare structure passed to cancellation handler. */
92 cbuffer
.mutex
= mutex
;
94 /* Before we block we enable cancellation. Therefore we have to
95 install a cancellation handler. */
96 __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
;
110 #ifdef __NR_clock_gettime
111 INTERNAL_SYSCALL_DECL (err
);
113 ret
= INTERNAL_VSYSCALL (clock_gettime
, err
, 2,
114 (cond
->__data
.__nwaiters
115 & ((1 << COND_NWAITERS_SHIFT
) - 1)),
117 # ifndef __ASSUME_POSIX_TIMERS
118 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (ret
, err
), 0))
121 (void) gettimeofday (&tv
, NULL
);
123 /* Convert the absolute timeout value to a relative timeout. */
124 rt
.tv_sec
= abstime
->tv_sec
- tv
.tv_sec
;
125 rt
.tv_nsec
= abstime
->tv_nsec
- tv
.tv_usec
* 1000;
130 /* Convert the absolute timeout value to a relative timeout. */
131 rt
.tv_sec
= abstime
->tv_sec
- rt
.tv_sec
;
132 rt
.tv_nsec
= abstime
->tv_nsec
- rt
.tv_nsec
;
135 /* Get the current time. So far we support only one clock. */
137 (void) gettimeofday (&tv
, NULL
);
139 /* Convert the absolute timeout value to a relative timeout. */
140 rt
.tv_sec
= abstime
->tv_sec
- tv
.tv_sec
;
141 rt
.tv_nsec
= abstime
->tv_nsec
- tv
.tv_usec
* 1000;
146 rt
.tv_nsec
+= 1000000000;
149 /* Did we already time out? */
150 if (__builtin_expect (rt
.tv_sec
< 0, 0))
152 if (cbuffer
.bc_seq
!= cond
->__data
.__broadcast_seq
)
158 unsigned int futex_val
= cond
->__data
.__futex
;
160 /* Prepare to wait. Release the condvar futex. */
161 lll_unlock (cond
->__data
.__lock
, pshared
);
163 /* Enable asynchronous cancellation. Required by the standard. */
164 cbuffer
.oldtype
= __pthread_enable_asynccancel ();
166 /* Wait until woken by signal or broadcast. */
167 err
= lll_futex_timed_wait (&cond
->__data
.__futex
,
168 futex_val
, &rt
, pshared
);
170 /* Disable asynchronous cancellation. */
171 __pthread_disable_asynccancel (cbuffer
.oldtype
);
173 /* We are going to look at shared data again, so get the lock. */
174 lll_lock (cond
->__data
.__lock
, pshared
);
176 /* If a broadcast happened, we are done. */
177 if (cbuffer
.bc_seq
!= cond
->__data
.__broadcast_seq
)
180 /* Check whether we are eligible for wakeup. */
181 val
= cond
->__data
.__wakeup_seq
;
182 if (val
!= seq
&& cond
->__data
.__woken_seq
!= val
)
185 /* Not woken yet. Maybe the time expired? */
186 if (__builtin_expect (err
== -ETIMEDOUT
, 0))
189 /* Yep. Adjust the counters. */
190 ++cond
->__data
.__wakeup_seq
;
191 ++cond
->__data
.__futex
;
193 /* The error value. */
199 /* Another thread woken up. */
200 ++cond
->__data
.__woken_seq
;
204 cond
->__data
.__nwaiters
-= 1 << COND_NWAITERS_SHIFT
;
206 /* If pthread_cond_destroy was called on this variable already,
207 notify the pthread_cond_destroy caller all waiters have left
208 and it can be successfully destroyed. */
209 if (cond
->__data
.__total_seq
== -1ULL
210 && cond
->__data
.__nwaiters
< (1 << COND_NWAITERS_SHIFT
))
211 lll_futex_wake (&cond
->__data
.__nwaiters
, 1, pshared
);
213 /* We are done with the condvar. */
214 lll_unlock (cond
->__data
.__lock
, pshared
);
216 /* The cancellation handling is back to normal, remove the handler. */
217 __pthread_cleanup_pop (&buffer
, 0);
219 /* Get the mutex before returning. */
220 err
= __pthread_mutex_cond_lock (mutex
);
222 return err
?: result
;
225 versioned_symbol (libpthread
, __pthread_cond_timedwait
, pthread_cond_timedwait
,