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
23 #include <lowlevellock.h>
27 #include <shlib-compat.h>
30 struct _condvar_cleanup_buffer
34 pthread_mutex_t *mutex;
40 __attribute__ ((visibility ("hidden")))
41 __condvar_cleanup (void *arg)
43 struct _condvar_cleanup_buffer *cbuffer =
44 (struct _condvar_cleanup_buffer *) arg;
46 /* We are going to modify shared data. */
47 lll_mutex_lock (cbuffer->cond->__data.__lock);
49 if (cbuffer->bc_seq == cbuffer->cond->__data.__broadcast_seq)
51 /* This thread is not waiting anymore. Adjust the sequence counters
53 ++cbuffer->cond->__data.__wakeup_seq;
54 ++cbuffer->cond->__data.__woken_seq;
58 lll_mutex_unlock (cbuffer->cond->__data.__lock);
60 /* Wake everybody to make sure no condvar signal gets lost. */
61 #if BYTE_ORDER == LITTLE_ENDIAN
62 int *futex = ((int *) (&cbuffer->cond->__data.__wakeup_seq));
63 #elif BYTE_ORDER == BIG_ENDIAN
64 int *futex = ((int *) (&cbuffer->cond->__data.__wakeup_seq)) + 1;
66 # error "No valid byte order"
68 lll_futex_wake (futex, INT_MAX);
70 /* Get the mutex before returning unless asynchronous cancellation
72 __pthread_mutex_cond_lock (cbuffer->mutex);
77 __pthread_cond_wait (cond, mutex)
79 pthread_mutex_t *mutex;
81 struct _pthread_cleanup_buffer buffer;
82 struct _condvar_cleanup_buffer cbuffer;
85 /* Make sure we are along. */
86 lll_mutex_lock (cond->__data.__lock);
88 /* Now we can release the mutex. */
89 err = __pthread_mutex_unlock_usercnt (mutex, 0);
90 if (__builtin_expect (err, 0))
92 lll_mutex_unlock (cond->__data.__lock);
96 /* We have one new user of the condvar. */
97 ++cond->__data.__total_seq;
99 /* Remember the mutex we are using here. If there is already a
100 different address store this is a bad user bug. Do not store
101 anything for pshared condvars. */
102 if (cond->__data.__mutex != (void *) ~0l)
103 cond->__data.__mutex = mutex;
105 /* Prepare structure passed to cancellation handler. */
107 cbuffer.mutex = mutex;
109 /* Before we block we enable cancellation. Therefore we have to
110 install a cancellation handler. */
111 __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
113 /* The current values of the wakeup counter. The "woken" counter
114 must exceed this value. */
115 unsigned long long int val;
116 unsigned long long int seq;
117 val = seq = cond->__data.__wakeup_seq;
118 /* Remember the broadcast counter. */
119 cbuffer.bc_seq = cond->__data.__broadcast_seq;
121 /* The futex syscall operates on a 32-bit word. That is fine, we
122 just use the low 32 bits of the sequence counter. */
123 #if BYTE_ORDER == LITTLE_ENDIAN
124 int *futex = ((int *) (&cond->__data.__wakeup_seq));
125 #elif BYTE_ORDER == BIG_ENDIAN
126 int *futex = ((int *) (&cond->__data.__wakeup_seq)) + 1;
128 # error "No valid byte order"
133 /* Prepare to wait. Release the condvar futex. */
134 lll_mutex_unlock (cond->__data.__lock);
136 /* Enable asynchronous cancellation. Required by the standard. */
137 cbuffer.oldtype = __pthread_enable_asynccancel ();
139 /* Wait until woken by signal or broadcast. Note that we
140 truncate the 'val' value to 32 bits. */
141 lll_futex_wait (futex, (unsigned int) val);
143 /* Disable asynchronous cancellation. */
144 __pthread_disable_asynccancel (cbuffer.oldtype);
146 /* If a broadcast happened, we are done. */
147 if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
150 /* We are going to look at shared data again, so get the lock. */
151 lll_mutex_lock (cond->__data.__lock);
153 /* Check whether we are eligible for wakeup. */
154 val = cond->__data.__wakeup_seq;
156 while (val == seq || cond->__data.__woken_seq == val);
158 /* Another thread woken up. */
159 ++cond->__data.__woken_seq;
162 /* We are done with the condvar. */
163 lll_mutex_unlock (cond->__data.__lock);
165 /* The cancellation handling is back to normal, remove the handler. */
166 __pthread_cleanup_pop (&buffer, 0);
168 /* Get the mutex before returning. */
169 return __pthread_mutex_cond_lock (mutex);
172 versioned_symbol (libpthread, __pthread_cond_wait, pthread_cond_wait,