]> sourceware.org Git - glibc.git/blob - nptl/sysdeps/pthread/pthread_cond_timedwait.c
Update.
[glibc.git] / nptl / sysdeps / pthread / pthread_cond_timedwait.c
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.
4
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.
9
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.
14
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
18 02111-1307 USA. */
19
20 #include <endian.h>
21 #include <errno.h>
22 #include <sysdep.h>
23 #include <lowlevellock.h>
24 #include <pthread.h>
25 #include <pthreadP.h>
26
27 #include <shlib-compat.h>
28
29
30 /* Cleanup handler, defined in pthread_cond_wait.c. */
31 extern void __condvar_cleanup (void *arg)
32 __attribute__ ((visibility ("hidden")));
33
34 struct _condvar_cleanup_buffer
35 {
36 int oldtype;
37 pthread_cond_t *cond;
38 pthread_mutex_t *mutex;
39 };
40
41 int
42 __pthread_cond_timedwait (cond, mutex, abstime)
43 pthread_cond_t *cond;
44 pthread_mutex_t *mutex;
45 const struct timespec *abstime;
46 {
47 struct _pthread_cleanup_buffer buffer;
48 struct _condvar_cleanup_buffer cbuffer;
49 int result = 0;
50
51 /* Catch invalid parameters. */
52 if (abstime->tv_nsec >= 1000000000)
53 return EINVAL;
54
55 /* Make sure we are along. */
56 lll_mutex_lock (cond->__data.__lock);
57
58 /* Now we can release the mutex. */
59 int err = __pthread_mutex_unlock_internal (mutex);
60 if (err)
61 {
62 lll_mutex_unlock (cond->__data.__lock);
63 return err;
64 }
65
66 /* We have one new user of the condvar. */
67 ++cond->__data.__total_seq;
68
69 /* Prepare structure passed to cancellation handler. */
70 cbuffer.cond = cond;
71 cbuffer.mutex = mutex;
72
73 /* Before we block we enable cancellation. Therefore we have to
74 install a cancellation handler. */
75 __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
76
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;
82
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;
89 #else
90 # error "No valid byte order"
91 #endif
92
93 while (1)
94 {
95 /* Get the current time. So far we support only one clock. */
96 struct timeval tv;
97 (void) gettimeofday (&tv, NULL);
98
99 /* Convert the absolute timeout value to a relative timeout. */
100 struct timespec rt;
101 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
102 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
103 if (rt.tv_nsec < 0)
104 {
105 rt.tv_nsec += 1000000000;
106 --rt.tv_sec;
107 }
108 /* Did we already time out? */
109 if (rt.tv_sec < 0)
110 {
111 /* Yep. Adjust the sequence counter. */
112 ++cond->__data.__wakeup_seq;
113
114 /* The error value. */
115 result = ETIMEDOUT;
116 break;
117 }
118
119 /* Prepare to wait. Release the condvar futex. */
120 lll_mutex_unlock (cond->__data.__lock);
121
122 /* Enable asynchronous cancellation. Required by the standard. */
123 cbuffer.oldtype = __pthread_enable_asynccancel ();
124
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);
128
129 /* Disable asynchronous cancellation. */
130 __pthread_disable_asynccancel (cbuffer.oldtype);
131
132 /* We are going to look at shared data again, so get the lock. */
133 lll_mutex_lock(cond->__data.__lock);
134
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)
139 break;
140
141 /* Not woken yet. Maybe the time expired? */
142 if (err == -ETIMEDOUT)
143 {
144 /* Yep. Adjust the counters. */
145 ++cond->__data.__wakeup_seq;
146
147 /* The error value. */
148 result = ETIMEDOUT;
149 break;
150 }
151 }
152
153 /* Another thread woken up. */
154 ++cond->__data.__woken_seq;
155
156 /* We are done with the condvar. */
157 lll_mutex_unlock (cond->__data.__lock);
158
159 /* The cancellation handling is back to normal, remove the handler. */
160 __pthread_cleanup_pop (&buffer, 0);
161
162 /* Get the mutex before returning. */
163 err = __pthread_mutex_lock_internal (mutex);
164
165 return err ?: result;
166 }
167
168 versioned_symbol (libpthread, __pthread_cond_timedwait, pthread_cond_timedwait,
169 GLIBC_2_3_2);
This page took 0.046784 seconds and 5 git commands to generate.