(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nptl / sysdeps / pthread / pthread_cond_wait.c
blob86669458a0705de7565414dad169c03df2c9d02a
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.
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
18 02111-1307 USA. */
20 #include <endian.h>
21 #include <errno.h>
22 #include <sysdep.h>
23 #include <lowlevellock.h>
24 #include <pthread.h>
25 #include <pthreadP.h>
27 #include <shlib-compat.h>
30 struct _condvar_cleanup_buffer
32 int oldtype;
33 pthread_cond_t *cond;
34 pthread_mutex_t *mutex;
35 unsigned int bc_seq;
39 void
40 __attribute__ ((visibility ("hidden")))
41 __condvar_cleanup (void *arg)
43 struct _condvar_cleanup_buffer *cbuffer =
44 (struct _condvar_cleanup_buffer *) arg;
45 unsigned int destroying;
47 /* We are going to modify shared data. */
48 lll_mutex_lock (cbuffer->cond->__data.__lock);
50 if (cbuffer->bc_seq == cbuffer->cond->__data.__broadcast_seq)
52 /* This thread is not waiting anymore. Adjust the sequence counters
53 appropriately. */
54 ++cbuffer->cond->__data.__wakeup_seq;
55 ++cbuffer->cond->__data.__woken_seq;
56 ++cbuffer->cond->__data.__futex;
59 cbuffer->cond->__data.__nwaiters -= 1 << COND_CLOCK_BITS;
61 /* If pthread_cond_destroy was called on this variable already,
62 notify the pthread_cond_destroy caller all waiters have left
63 and it can be successfully destroyed. */
64 destroying = 0;
65 if (cbuffer->cond->__data.__total_seq == -1ULL
66 && cbuffer->cond->__data.__nwaiters < (1 << COND_CLOCK_BITS))
68 lll_futex_wake (&cbuffer->cond->__data.__nwaiters, 1);
69 destroying = 1;
72 /* We are done. */
73 lll_mutex_unlock (cbuffer->cond->__data.__lock);
75 /* Wake everybody to make sure no condvar signal gets lost. */
76 if (! destroying)
77 lll_futex_wake (&cbuffer->cond->__data.__futex, INT_MAX);
79 /* Get the mutex before returning unless asynchronous cancellation
80 is in effect. */
81 __pthread_mutex_cond_lock (cbuffer->mutex);
85 int
86 __pthread_cond_wait (cond, mutex)
87 pthread_cond_t *cond;
88 pthread_mutex_t *mutex;
90 struct _pthread_cleanup_buffer buffer;
91 struct _condvar_cleanup_buffer cbuffer;
92 int err;
94 /* Make sure we are along. */
95 lll_mutex_lock (cond->__data.__lock);
97 /* Now we can release the mutex. */
98 err = __pthread_mutex_unlock_usercnt (mutex, 0);
99 if (__builtin_expect (err, 0))
101 lll_mutex_unlock (cond->__data.__lock);
102 return err;
105 /* We have one new user of the condvar. */
106 ++cond->__data.__total_seq;
107 ++cond->__data.__futex;
108 cond->__data.__nwaiters += 1 << COND_CLOCK_BITS;
110 /* Remember the mutex we are using here. If there is already a
111 different address store this is a bad user bug. Do not store
112 anything for pshared condvars. */
113 if (cond->__data.__mutex != (void *) ~0l)
114 cond->__data.__mutex = mutex;
116 /* Prepare structure passed to cancellation handler. */
117 cbuffer.cond = cond;
118 cbuffer.mutex = mutex;
120 /* Before we block we enable cancellation. Therefore we have to
121 install a cancellation handler. */
122 __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
124 /* The current values of the wakeup counter. The "woken" counter
125 must exceed this value. */
126 unsigned long long int val;
127 unsigned long long int seq;
128 val = seq = cond->__data.__wakeup_seq;
129 /* Remember the broadcast counter. */
130 cbuffer.bc_seq = cond->__data.__broadcast_seq;
134 unsigned int futex_val = cond->__data.__futex;
136 /* Prepare to wait. Release the condvar futex. */
137 lll_mutex_unlock (cond->__data.__lock);
139 /* Enable asynchronous cancellation. Required by the standard. */
140 cbuffer.oldtype = __pthread_enable_asynccancel ();
142 /* Wait until woken by signal or broadcast. */
143 lll_futex_wait (&cond->__data.__futex, futex_val);
145 /* Disable asynchronous cancellation. */
146 __pthread_disable_asynccancel (cbuffer.oldtype);
148 /* We are going to look at shared data again, so get the lock. */
149 lll_mutex_lock (cond->__data.__lock);
151 /* If a broadcast happened, we are done. */
152 if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
153 goto bc_out;
155 /* Check whether we are eligible for wakeup. */
156 val = cond->__data.__wakeup_seq;
158 while (val == seq || cond->__data.__woken_seq == val);
160 /* Another thread woken up. */
161 ++cond->__data.__woken_seq;
163 bc_out:
165 cond->__data.__nwaiters -= 1 << COND_CLOCK_BITS;
167 /* If pthread_cond_destroy was called on this varaible already,
168 notify the pthread_cond_destroy caller all waiters have left
169 and it can be successfully destroyed. */
170 if (cond->__data.__total_seq == -1ULL
171 && cond->__data.__nwaiters < (1 << COND_CLOCK_BITS))
172 lll_futex_wake (&cond->__data.__nwaiters, 1);
174 /* We are done with the condvar. */
175 lll_mutex_unlock (cond->__data.__lock);
177 /* The cancellation handling is back to normal, remove the handler. */
178 __pthread_cleanup_pop (&buffer, 0);
180 /* Get the mutex before returning. */
181 return __pthread_mutex_cond_lock (mutex);
184 versioned_symbol (libpthread, __pthread_cond_wait, pthread_cond_wait,
185 GLIBC_2_3_2);