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