[BZ #2510, BZ #2830, BZ #3137, BZ #3313, BZ #3426, BZ #3465, BZ #3480, BZ #3483,...
[jlayton/glibc.git] / nptl / pthread_cond_timedwait.c
1 /* Copyright (C) 2003, 2004 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   unsigned int bc_seq;
40 };
41
42 int
43 __pthread_cond_timedwait (cond, mutex, abstime)
44      pthread_cond_t *cond;
45      pthread_mutex_t *mutex;
46      const struct timespec *abstime;
47 {
48   struct _pthread_cleanup_buffer buffer;
49   struct _condvar_cleanup_buffer cbuffer;
50   int result = 0;
51
52   /* Catch invalid parameters.  */
53   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
54     return EINVAL;
55
56   /* Make sure we are along.  */
57   lll_mutex_lock (cond->__data.__lock);
58
59   /* Now we can release the mutex.  */
60   int err = __pthread_mutex_unlock_usercnt (mutex, 0);
61   if (err)
62     {
63       lll_mutex_unlock (cond->__data.__lock);
64       return err;
65     }
66
67   /* We have one new user of the condvar.  */
68   ++cond->__data.__total_seq;
69   ++cond->__data.__futex;
70   cond->__data.__nwaiters += 1 << COND_CLOCK_BITS;
71
72   /* Remember the mutex we are using here.  If there is already a
73      different address store this is a bad user bug.  Do not store
74      anything for pshared condvars.  */
75   if (cond->__data.__mutex != (void *) ~0l)
76     cond->__data.__mutex = mutex;
77
78   /* Prepare structure passed to cancellation handler.  */
79   cbuffer.cond = cond;
80   cbuffer.mutex = mutex;
81
82   /* Before we block we enable cancellation.  Therefore we have to
83      install a cancellation handler.  */
84   __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
85
86   /* The current values of the wakeup counter.  The "woken" counter
87      must exceed this value.  */
88   unsigned long long int val;
89   unsigned long long int seq;
90   val = seq = cond->__data.__wakeup_seq;
91   /* Remember the broadcast counter.  */
92   cbuffer.bc_seq = cond->__data.__broadcast_seq;
93
94   while (1)
95     {
96       struct timespec rt;
97       {
98 #ifdef __NR_clock_gettime
99         INTERNAL_SYSCALL_DECL (err);
100         int ret;
101         ret = INTERNAL_SYSCALL (clock_gettime, err, 2,
102                                 (cond->__data.__nwaiters
103                                  & ((1 << COND_CLOCK_BITS) - 1)),
104                                 &rt);
105 # ifndef __ASSUME_POSIX_TIMERS
106         if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (ret, err), 0))
107           {
108             struct timeval tv;
109             (void) gettimeofday (&tv, NULL);
110
111             /* Convert the absolute timeout value to a relative timeout.  */
112             rt.tv_sec = abstime->tv_sec - tv.tv_sec;
113             rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
114           }
115         else
116 # endif
117           {
118             /* Convert the absolute timeout value to a relative timeout.  */
119             rt.tv_sec = abstime->tv_sec - rt.tv_sec;
120             rt.tv_nsec = abstime->tv_nsec - rt.tv_nsec;
121           }
122 #else
123         /* Get the current time.  So far we support only one clock.  */
124         struct timeval tv;
125         (void) gettimeofday (&tv, NULL);
126
127         /* Convert the absolute timeout value to a relative timeout.  */
128         rt.tv_sec = abstime->tv_sec - tv.tv_sec;
129         rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
130 #endif
131       }
132       if (rt.tv_nsec < 0)
133         {
134           rt.tv_nsec += 1000000000;
135           --rt.tv_sec;
136         }
137       /* Did we already time out?  */
138       if (__builtin_expect (rt.tv_sec < 0, 0))
139         {
140           if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
141             goto bc_out;
142
143           goto timeout;
144         }
145
146       unsigned int futex_val = cond->__data.__futex;
147
148       /* Prepare to wait.  Release the condvar futex.  */
149       lll_mutex_unlock (cond->__data.__lock);
150
151       /* Enable asynchronous cancellation.  Required by the standard.  */
152       cbuffer.oldtype = __pthread_enable_asynccancel ();
153
154       /* Wait until woken by signal or broadcast.  */
155       err = lll_futex_timed_wait (&cond->__data.__futex,
156                                   futex_val, &rt);
157
158       /* Disable asynchronous cancellation.  */
159       __pthread_disable_asynccancel (cbuffer.oldtype);
160
161       /* We are going to look at shared data again, so get the lock.  */
162       lll_mutex_lock(cond->__data.__lock);
163
164       /* If a broadcast happened, we are done.  */
165       if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
166         goto bc_out;
167
168       /* Check whether we are eligible for wakeup.  */
169       val = cond->__data.__wakeup_seq;
170       if (val != seq && cond->__data.__woken_seq != val)
171         break;
172
173       /* Not woken yet.  Maybe the time expired?  */
174       if (__builtin_expect (err == -ETIMEDOUT, 0))
175         {
176         timeout:
177           /* Yep.  Adjust the counters.  */
178           ++cond->__data.__wakeup_seq;
179           ++cond->__data.__futex;
180
181           /* The error value.  */
182           result = ETIMEDOUT;
183           break;
184         }
185     }
186
187   /* Another thread woken up.  */
188   ++cond->__data.__woken_seq;
189
190  bc_out:
191
192   cond->__data.__nwaiters -= 1 << COND_CLOCK_BITS;
193
194   /* If pthread_cond_destroy was called on this variable already,
195      notify the pthread_cond_destroy caller all waiters have left
196      and it can be successfully destroyed.  */
197   if (cond->__data.__total_seq == -1ULL
198       && cond->__data.__nwaiters < (1 << COND_CLOCK_BITS))
199     lll_futex_wake (&cond->__data.__nwaiters, 1);
200
201   /* We are done with the condvar.  */
202   lll_mutex_unlock (cond->__data.__lock);
203
204   /* The cancellation handling is back to normal, remove the handler.  */
205   __pthread_cleanup_pop (&buffer, 0);
206
207   /* Get the mutex before returning.  */
208   err = __pthread_mutex_cond_lock (mutex);
209
210   return err ?: result;
211 }
212
213 versioned_symbol (libpthread, __pthread_cond_timedwait, pthread_cond_timedwait,
214                   GLIBC_2_3_2);