1 /* Copyright (C) 2003, 2004, 2006, 2007 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, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <endian.h>
20 #include <errno.h>
21 #include <sysdep.h>
22 #include <lowlevellock.h>
23 #include <pthread.h>
24 #include <pthreadP.h>
25
26
27 struct _condvar_cleanup_buffer
28 {
29 int oldtype;
30 pthread_cond_t *cond;
31 pthread_mutex_t *mutex;
32 unsigned int bc_seq;
33 };
34
35
36 void
37 __attribute__ ((visibility ("hidden")))
__condvar_cleanup(void * arg)38 __condvar_cleanup (void *arg)
39 {
40 struct _condvar_cleanup_buffer *cbuffer =
41 (struct _condvar_cleanup_buffer *) arg;
42 unsigned int destroying;
43 int pshared = (cbuffer->cond->__data.__mutex == (void *) ~0l)
44 ? LLL_SHARED : LLL_PRIVATE;
45
46 /* We are going to modify shared data. */
47 lll_lock (cbuffer->cond->__data.__lock, pshared);
48
49 if (cbuffer->bc_seq == cbuffer->cond->__data.__broadcast_seq)
50 {
51 /* This thread is not waiting anymore. Adjust the sequence counters
52 appropriately. We do not increment WAKEUP_SEQ if this would
53 bump it over the value of TOTAL_SEQ. This can happen if a thread
54 was woken and then canceled. */
55 if (cbuffer->cond->__data.__wakeup_seq
56 < cbuffer->cond->__data.__total_seq)
57 {
58 ++cbuffer->cond->__data.__wakeup_seq;
59 ++cbuffer->cond->__data.__futex;
60 }
61 ++cbuffer->cond->__data.__woken_seq;
62 }
63
64 cbuffer->cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
65
66 /* If pthread_cond_destroy was called on this variable already,
67 notify the pthread_cond_destroy caller all waiters have left
68 and it can be successfully destroyed. */
69 destroying = 0;
70 if (cbuffer->cond->__data.__total_seq == -1ULL
71 && cbuffer->cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT))
72 {
73 lll_futex_wake (&cbuffer->cond->__data.__nwaiters, 1, pshared);
74 destroying = 1;
75 }
76
77 /* We are done. */
78 lll_unlock (cbuffer->cond->__data.__lock, pshared);
79
80 /* Wake everybody to make sure no condvar signal gets lost. */
81 if (! destroying)
82 lll_futex_wake (&cbuffer->cond->__data.__futex, INT_MAX, pshared);
83
84 /* Get the mutex before returning unless asynchronous cancellation
85 is in effect. */
86 __pthread_mutex_cond_lock (cbuffer->mutex);
87 }
88
89
90 int
91 attribute_protected
__pthread_cond_wait(pthread_cond_t * cond,pthread_mutex_t * mutex)92 __pthread_cond_wait (
93 pthread_cond_t *cond,
94 pthread_mutex_t *mutex)
95 {
96 struct _pthread_cleanup_buffer buffer;
97 struct _condvar_cleanup_buffer cbuffer;
98 int err;
99 int pshared = (cond->__data.__mutex == (void *) ~0l)
100 ? LLL_SHARED : LLL_PRIVATE;
101
102 /* Make sure we are along. */
103 lll_lock (cond->__data.__lock, pshared);
104
105 /* Now we can release the mutex. */
106 err = __pthread_mutex_unlock_usercnt (mutex, 0);
107 if (__builtin_expect (err, 0))
108 {
109 lll_unlock (cond->__data.__lock, pshared);
110 return err;
111 }
112
113 /* We have one new user of the condvar. */
114 ++cond->__data.__total_seq;
115 ++cond->__data.__futex;
116 cond->__data.__nwaiters += 1 << COND_NWAITERS_SHIFT;
117
118 /* Remember the mutex we are using here. If there is already a
119 different address store this is a bad user bug. Do not store
120 anything for pshared condvars. */
121 if (cond->__data.__mutex != (void *) ~0l)
122 cond->__data.__mutex = mutex;
123
124 /* Prepare structure passed to cancellation handler. */
125 cbuffer.cond = cond;
126 cbuffer.mutex = mutex;
127
128 /* Before we block we enable cancellation. Therefore we have to
129 install a cancellation handler. */
130 __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
131
132 /* The current values of the wakeup counter. The "woken" counter
133 must exceed this value. */
134 unsigned long long int val;
135 unsigned long long int seq;
136 val = seq = cond->__data.__wakeup_seq;
137 /* Remember the broadcast counter. */
138 cbuffer.bc_seq = cond->__data.__broadcast_seq;
139
140 do
141 {
142 unsigned int futex_val = cond->__data.__futex;
143
144 /* Prepare to wait. Release the condvar futex. */
145 lll_unlock (cond->__data.__lock, pshared);
146
147 /* Enable asynchronous cancellation. Required by the standard. */
148 cbuffer.oldtype = __pthread_enable_asynccancel ();
149
150 /* Wait until woken by signal or broadcast. */
151 lll_futex_wait (&cond->__data.__futex, futex_val, pshared);
152
153 /* Disable asynchronous cancellation. */
154 __pthread_disable_asynccancel (cbuffer.oldtype);
155
156 /* We are going to look at shared data again, so get the lock. */
157 lll_lock (cond->__data.__lock, pshared);
158
159 /* If a broadcast happened, we are done. */
160 if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
161 goto bc_out;
162
163 /* Check whether we are eligible for wakeup. */
164 val = cond->__data.__wakeup_seq;
165 }
166 while (val == seq || cond->__data.__woken_seq == val);
167
168 /* Another thread woken up. */
169 ++cond->__data.__woken_seq;
170
171 bc_out:
172
173 cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
174
175 /* If pthread_cond_destroy was called on this varaible already,
176 notify the pthread_cond_destroy caller all waiters have left
177 and it can be successfully destroyed. */
178 if (cond->__data.__total_seq == -1ULL
179 && cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT))
180 lll_futex_wake (&cond->__data.__nwaiters, 1, pshared);
181
182 /* We are done with the condvar. */
183 lll_unlock (cond->__data.__lock, pshared);
184
185 /* The cancellation handling is back to normal, remove the handler. */
186 __pthread_cleanup_pop (&buffer, 0);
187
188 /* Get the mutex before returning. */
189 return __pthread_mutex_cond_lock (mutex);
190 }
191
192 weak_alias(__pthread_cond_wait, pthread_cond_wait)
193