]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/sysdeps/pthread/pthread_cond_wait.c
[BZ #40]
[thirdparty/glibc.git] / nptl / sysdeps / pthread / pthread_cond_wait.c
CommitLineData
b078c591 1/* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
a88c9263
UD
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
7ce5c164
UD
30struct _condvar_cleanup_buffer
31{
32 int oldtype;
33 pthread_cond_t *cond;
34 pthread_mutex_t *mutex;
35};
36
3e976b96 37
a88c9263
UD
38void
39__attribute__ ((visibility ("hidden")))
40__condvar_cleanup (void *arg)
41{
7ce5c164
UD
42 struct _condvar_cleanup_buffer *cbuffer =
43 (struct _condvar_cleanup_buffer *) arg;
a88c9263
UD
44
45 /* We are going to modify shared data. */
7ce5c164 46 lll_mutex_lock (cbuffer->cond->__data.__lock);
a88c9263
UD
47
48 /* This thread is not waiting anymore. Adjust the sequence counters
49 appropriately. */
7ce5c164
UD
50 ++cbuffer->cond->__data.__wakeup_seq;
51 ++cbuffer->cond->__data.__woken_seq;
a88c9263 52
24a49f38
UD
53 /* We are done. */
54 lll_mutex_unlock (cbuffer->cond->__data.__lock);
55
3e976b96
UD
56 /* Wake everybody to make sure no condvar signal gets lost. */
57#if BYTE_ORDER == LITTLE_ENDIAN
58 int *futex = ((int *) (&cbuffer->cond->__data.__wakeup_seq));
59#elif BYTE_ORDER == BIG_ENDIAN
60 int *futex = ((int *) (&cbuffer->cond->__data.__wakeup_seq)) + 1;
61#else
62# error "No valid byte order"
63#endif
64 lll_futex_wake (futex, INT_MAX);
65
7ce5c164
UD
66 /* Get the mutex before returning unless asynchronous cancellation
67 is in effect. */
69431c9a 68 __pthread_mutex_cond_lock (cbuffer->mutex);
a88c9263
UD
69}
70
71
72int
73__pthread_cond_wait (cond, mutex)
74 pthread_cond_t *cond;
75 pthread_mutex_t *mutex;
76{
77 struct _pthread_cleanup_buffer buffer;
7ce5c164
UD
78 struct _condvar_cleanup_buffer cbuffer;
79 int err;
a88c9263
UD
80
81 /* Make sure we are along. */
82 lll_mutex_lock (cond->__data.__lock);
83
84 /* Now we can release the mutex. */
61623643 85 err = __pthread_mutex_unlock_usercnt (mutex, 0);
b078c591 86 if (__builtin_expect (err, 0))
7ce5c164
UD
87 {
88 lll_mutex_unlock (cond->__data.__lock);
89 return err;
90 }
a88c9263
UD
91
92 /* We have one new user of the condvar. */
93 ++cond->__data.__total_seq;
94
69431c9a 95 /* Remember the mutex we are using here. If there is already a
e42a990e
UD
96 different address store this is a bad user bug. Do not store
97 anything for pshared condvars. */
98 if (cond->__data.__mutex != (void *) ~0l)
99 cond->__data.__mutex = mutex;
69431c9a 100
7ce5c164
UD
101 /* Prepare structure passed to cancellation handler. */
102 cbuffer.cond = cond;
103 cbuffer.mutex = mutex;
104
a88c9263
UD
105 /* Before we block we enable cancellation. Therefore we have to
106 install a cancellation handler. */
7ce5c164 107 __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
a88c9263
UD
108
109 /* The current values of the wakeup counter. The "woken" counter
110 must exceed this value. */
111 unsigned long long int val;
112 unsigned long long int seq;
113 val = seq = cond->__data.__wakeup_seq;
114
115 /* The futex syscall operates on a 32-bit word. That is fine, we
116 just use the low 32 bits of the sequence counter. */
117#if BYTE_ORDER == LITTLE_ENDIAN
118 int *futex = ((int *) (&cond->__data.__wakeup_seq));
119#elif BYTE_ORDER == BIG_ENDIAN
120 int *futex = ((int *) (&cond->__data.__wakeup_seq)) + 1;
121#else
122# error "No valid byte order"
123#endif
124
46a32546 125 do
a88c9263
UD
126 {
127 /* Prepare to wait. Release the condvar futex. */
128 lll_mutex_unlock (cond->__data.__lock);
129
130 /* Enable asynchronous cancellation. Required by the standard. */
69431c9a 131 cbuffer.oldtype = __pthread_enable_asynccancel ();
a88c9263
UD
132
133 /* Wait until woken by signal or broadcast. Note that we
134 truncate the 'val' value to 32 bits. */
135 lll_futex_wait (futex, (unsigned int) val);
136
137 /* Disable asynchronous cancellation. */
7ce5c164 138 __pthread_disable_asynccancel (cbuffer.oldtype);
a88c9263
UD
139
140 /* We are going to look at shared data again, so get the lock. */
32a589b1 141 lll_mutex_lock (cond->__data.__lock);
a88c9263
UD
142
143 /* Check whether we are eligible for wakeup. */
144 val = cond->__data.__wakeup_seq;
a88c9263 145 }
cff08c81 146 while (val == seq || cond->__data.__woken_seq == val);
a88c9263
UD
147
148 /* Another thread woken up. */
149 ++cond->__data.__woken_seq;
150
151 /* We are done with the condvar. */
152 lll_mutex_unlock (cond->__data.__lock);
153
154 /* The cancellation handling is back to normal, remove the handler. */
155 __pthread_cleanup_pop (&buffer, 0);
156
157 /* Get the mutex before returning. */
69431c9a 158 return __pthread_mutex_cond_lock (mutex);
a88c9263
UD
159}
160
161versioned_symbol (libpthread, __pthread_cond_wait, pthread_cond_wait,
162 GLIBC_2_3_2);