Check supported DF_1_XXX bits
[glibc.git] / nptl / pthread_cond_timedwait.c
blob2fcbc57c2adcedf60a7174be60072d15d0558509
1 /* Copyright (C) 2003-2012 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/>. */
19 #include <endian.h>
20 #include <errno.h>
21 #include <sysdep.h>
22 #include <lowlevellock.h>
23 #include <pthread.h>
24 #include <pthreadP.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
34 #else
35 # include <bits/libc-vdso.h>
36 #endif
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
44 int oldtype;
45 pthread_cond_t *cond;
46 pthread_mutex_t *mutex;
47 unsigned int bc_seq;
50 int
51 __pthread_cond_timedwait (cond, mutex, abstime)
52 pthread_cond_t *cond;
53 pthread_mutex_t *mutex;
54 const struct timespec *abstime;
56 struct _pthread_cleanup_buffer buffer;
57 struct _condvar_cleanup_buffer cbuffer;
58 int result = 0;
60 /* Catch invalid parameters. */
61 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
62 return EINVAL;
64 int pshared = (cond->__data.__mutex == (void *) ~0l)
65 ? LLL_SHARED : LLL_PRIVATE;
67 /* Make sure we are alone. */
68 lll_lock (cond->__data.__lock, pshared);
70 /* Now we can release the mutex. */
71 int err = __pthread_mutex_unlock_usercnt (mutex, 0);
72 if (err)
74 lll_unlock (cond->__data.__lock, pshared);
75 return err;
78 /* We have one new user of the condvar. */
79 ++cond->__data.__total_seq;
80 ++cond->__data.__futex;
81 cond->__data.__nwaiters += 1 << COND_NWAITERS_SHIFT;
83 /* Work around the fact that the kernel rejects negative timeout values
84 despite them being valid. */
85 if (__builtin_expect (abstime->tv_sec < 0, 0))
86 goto timeout;
88 /* Remember the mutex we are using here. If there is already a
89 different address store this is a bad user bug. Do not store
90 anything for pshared condvars. */
91 if (cond->__data.__mutex != (void *) ~0l)
92 cond->__data.__mutex = mutex;
94 /* Prepare structure passed to cancellation handler. */
95 cbuffer.cond = cond;
96 cbuffer.mutex = mutex;
98 /* Before we block we enable cancellation. Therefore we have to
99 install a cancellation handler. */
100 __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
102 /* The current values of the wakeup counter. The "woken" counter
103 must exceed this value. */
104 unsigned long long int val;
105 unsigned long long int seq;
106 val = seq = cond->__data.__wakeup_seq;
107 /* Remember the broadcast counter. */
108 cbuffer.bc_seq = cond->__data.__broadcast_seq;
110 while (1)
112 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
113 || !defined lll_futex_timed_wait_bitset)
114 struct timespec rt;
116 # ifdef __NR_clock_gettime
117 INTERNAL_SYSCALL_DECL (err);
118 int ret;
119 ret = INTERNAL_VSYSCALL (clock_gettime, err, 2,
120 (cond->__data.__nwaiters
121 & ((1 << COND_NWAITERS_SHIFT) - 1)),
122 &rt);
123 /* Convert the absolute timeout value to a relative timeout. */
124 rt.tv_sec = abstime->tv_sec - rt.tv_sec;
125 rt.tv_nsec = abstime->tv_nsec - rt.tv_nsec;
126 # else
127 /* Get the current time. So far we support only one clock. */
128 struct timeval tv;
129 (void) gettimeofday (&tv, NULL);
131 /* Convert the absolute timeout value to a relative timeout. */
132 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
133 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
134 # endif
136 if (rt.tv_nsec < 0)
138 rt.tv_nsec += 1000000000;
139 --rt.tv_sec;
141 /* Did we already time out? */
142 if (__builtin_expect (rt.tv_sec < 0, 0))
144 if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
145 goto bc_out;
147 goto timeout;
149 #endif
151 unsigned int futex_val = cond->__data.__futex;
153 /* Prepare to wait. Release the condvar futex. */
154 lll_unlock (cond->__data.__lock, pshared);
156 /* Enable asynchronous cancellation. Required by the standard. */
157 cbuffer.oldtype = __pthread_enable_asynccancel ();
159 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
160 || !defined lll_futex_timed_wait_bitset)
161 /* Wait until woken by signal or broadcast. */
162 err = lll_futex_timed_wait (&cond->__data.__futex,
163 futex_val, &rt, pshared);
164 #else
165 unsigned int clockbit = (cond->__data.__nwaiters & 1
166 ? 0 : FUTEX_CLOCK_REALTIME);
167 err = lll_futex_timed_wait_bitset (&cond->__data.__futex, futex_val,
168 abstime, clockbit, pshared);
169 #endif
171 /* Disable asynchronous cancellation. */
172 __pthread_disable_asynccancel (cbuffer.oldtype);
174 /* We are going to look at shared data again, so get the lock. */
175 lll_lock (cond->__data.__lock, pshared);
177 /* If a broadcast happened, we are done. */
178 if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
179 goto bc_out;
181 /* Check whether we are eligible for wakeup. */
182 val = cond->__data.__wakeup_seq;
183 if (val != seq && cond->__data.__woken_seq != val)
184 break;
186 /* Not woken yet. Maybe the time expired? */
187 if (__builtin_expect (err == -ETIMEDOUT, 0))
189 timeout:
190 /* Yep. Adjust the counters. */
191 ++cond->__data.__wakeup_seq;
192 ++cond->__data.__futex;
194 /* The error value. */
195 result = ETIMEDOUT;
196 break;
200 /* Another thread woken up. */
201 ++cond->__data.__woken_seq;
203 bc_out:
205 cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
207 /* If pthread_cond_destroy was called on this variable already,
208 notify the pthread_cond_destroy caller all waiters have left
209 and it can be successfully destroyed. */
210 if (cond->__data.__total_seq == -1ULL
211 && cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT))
212 lll_futex_wake (&cond->__data.__nwaiters, 1, pshared);
214 /* We are done with the condvar. */
215 lll_unlock (cond->__data.__lock, pshared);
217 /* The cancellation handling is back to normal, remove the handler. */
218 __pthread_cleanup_pop (&buffer, 0);
220 /* Get the mutex before returning. */
221 err = __pthread_mutex_cond_lock (mutex);
223 return err ?: result;
226 versioned_symbol (libpthread, __pthread_cond_timedwait, pthread_cond_timedwait,
227 GLIBC_2_3_2);