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