1 /* Copyright (C) 2003 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>
27 #include <shlib-compat.h>
30 /* Cleanup handler, defined in pthread_cond_wait.c. */
31 extern void __condvar_cleanup (void *arg
)
32 __attribute__ ((visibility ("hidden")));
34 struct _condvar_cleanup_buffer
38 pthread_mutex_t
*mutex
;
42 __pthread_cond_timedwait (cond
, mutex
, abstime
)
44 pthread_mutex_t
*mutex
;
45 const struct timespec
*abstime
;
47 struct _pthread_cleanup_buffer buffer
;
48 struct _condvar_cleanup_buffer cbuffer
;
51 /* Catch invalid parameters. */
52 if (abstime
->tv_nsec
>= 1000000000)
55 /* Make sure we are along. */
56 lll_mutex_lock (cond
->__data
.__lock
);
58 /* Now we can release the mutex. */
59 int err
= __pthread_mutex_unlock_internal (mutex
);
62 lll_mutex_unlock (cond
->__data
.__lock
);
66 /* We have one new user of the condvar. */
67 ++cond
->__data
.__total_seq
;
69 /* Prepare structure passed to cancellation handler. */
71 cbuffer
.mutex
= mutex
;
73 /* Before we block we enable cancellation. Therefore we have to
74 install a cancellation handler. */
75 __pthread_cleanup_push (&buffer
, __condvar_cleanup
, &cbuffer
);
77 /* The current values of the wakeup counter. The "woken" counter
78 must exceed this value. */
79 unsigned long long int val
;
80 unsigned long long int seq
;
81 val
= seq
= cond
->__data
.__wakeup_seq
;
83 /* The futex syscall operates on a 32-bit word. That is fine, we
84 just use the low 32 bits of the sequence counter. */
85 #if BYTE_ORDER == LITTLE_ENDIAN
86 int *futex
= ((int *) (&cond
->__data
.__wakeup_seq
));
87 #elif BYTE_ORDER == BIG_ENDIAN
88 int *futex
= ((int *) (&cond
->__data
.__wakeup_seq
)) + 1;
90 # error "No valid byte order"
95 /* Get the current time. So far we support only one clock. */
97 (void) gettimeofday (&tv
, NULL
);
99 /* Convert the absolute timeout value to a relative timeout. */
101 rt
.tv_sec
= abstime
->tv_sec
- tv
.tv_sec
;
102 rt
.tv_nsec
= abstime
->tv_nsec
- tv
.tv_usec
* 1000;
105 rt
.tv_nsec
+= 1000000000;
108 /* Did we already time out? */
111 /* Yep. Adjust the sequence counter. */
112 ++cond
->__data
.__wakeup_seq
;
114 /* The error value. */
119 /* Prepare to wait. Release the condvar futex. */
120 lll_mutex_unlock (cond
->__data
.__lock
);
122 /* Enable asynchronous cancellation. Required by the standard. */
123 cbuffer
.oldtype
= __pthread_enable_asynccancel ();
125 /* Wait until woken by signal or broadcast. Note that we
126 truncate the 'val' value to 32 bits. */
127 err
= lll_futex_timed_wait (futex
, (unsigned int) val
, &rt
);
129 /* Disable asynchronous cancellation. */
130 __pthread_disable_asynccancel (cbuffer
.oldtype
);
132 /* We are going to look at shared data again, so get the lock. */
133 lll_mutex_lock(cond
->__data
.__lock
);
135 /* Check whether we are eligible for wakeup. */
136 val
= cond
->__data
.__wakeup_seq
;
137 if (cond
->__data
.__woken_seq
>= seq
138 && cond
->__data
.__woken_seq
< val
)
141 /* Not woken yet. Maybe the time expired? */
142 if (err
== -ETIMEDOUT
)
144 /* Yep. Adjust the counters. */
145 ++cond
->__data
.__wakeup_seq
;
147 /* The error value. */
153 /* Another thread woken up. */
154 ++cond
->__data
.__woken_seq
;
156 /* We are done with the condvar. */
157 lll_mutex_unlock (cond
->__data
.__lock
);
159 /* The cancellation handling is back to normal, remove the handler. */
160 __pthread_cleanup_pop (&buffer
, 0);
162 /* Get the mutex before returning. */
163 err
= __pthread_mutex_lock_internal (mutex
);
165 return err
?: result
;
168 versioned_symbol (libpthread
, __pthread_cond_timedwait
, pthread_cond_timedwait
,