]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/sysdeps/pthread/pthread_cond_wait.c
(__pthread_mutex_init_internal): Mark hidden. (__pthread_mutex_lock_internal): Likewi...
[thirdparty/glibc.git] / nptl / sysdeps / pthread / pthread_cond_wait.c
CommitLineData
a88c9263
UD
1/* Copyright (C) 2003 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, 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
30struct _condvar_cleanup_buffer
31{
32 int oldtype;
33 pthread_cond_t *cond;
34 pthread_mutex_t *mutex;
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;
a88c9263
UD
44
45 /* We are going to modify shared data. */
7ce5c164 46 lll_mutex_lock (cbuffer->cond->__data.__lock);
a88c9263
UD
47
48 /* This thread is not waiting anymore. Adjust the sequence counters
49 appropriately. */
7ce5c164
UD
50 ++cbuffer->cond->__data.__wakeup_seq;
51 ++cbuffer->cond->__data.__woken_seq;
a88c9263 52
24a49f38
UD
53 /* We are done. */
54 lll_mutex_unlock (cbuffer->cond->__data.__lock);
55
3e976b96
UD
56 /* Wake everybody to make sure no condvar signal gets lost. */
57#if BYTE_ORDER == LITTLE_ENDIAN
58 int *futex = ((int *) (&cbuffer->cond->__data.__wakeup_seq));
59#elif BYTE_ORDER == BIG_ENDIAN
60 int *futex = ((int *) (&cbuffer->cond->__data.__wakeup_seq)) + 1;
61#else
62# error "No valid byte order"
63#endif
64 lll_futex_wake (futex, INT_MAX);
65
7ce5c164
UD
66 /* Get the mutex before returning unless asynchronous cancellation
67 is in effect. */
69431c9a 68 __pthread_mutex_cond_lock (cbuffer->mutex);
a88c9263
UD
69}
70
71
72int
73__pthread_cond_wait (cond, mutex)
74 pthread_cond_t *cond;
75 pthread_mutex_t *mutex;
76{
77 struct _pthread_cleanup_buffer buffer;
7ce5c164
UD
78 struct _condvar_cleanup_buffer cbuffer;
79 int err;
a88c9263
UD
80
81 /* Make sure we are along. */
82 lll_mutex_lock (cond->__data.__lock);
83
84 /* Now we can release the mutex. */
7ce5c164
UD
85 err = __pthread_mutex_unlock_internal (mutex);
86 if (err)
87 {
88 lll_mutex_unlock (cond->__data.__lock);
89 return err;
90 }
a88c9263
UD
91
92 /* We have one new user of the condvar. */
93 ++cond->__data.__total_seq;
94
69431c9a
UD
95 /* Remember the mutex we are using here. If there is already a
96 different address store this is a bad user bug. */
97 cond->__data.__mutex = mutex;
98
7ce5c164
UD
99 /* Prepare structure passed to cancellation handler. */
100 cbuffer.cond = cond;
101 cbuffer.mutex = mutex;
102
a88c9263
UD
103 /* Before we block we enable cancellation. Therefore we have to
104 install a cancellation handler. */
7ce5c164 105 __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
a88c9263
UD
106
107 /* The current values of the wakeup counter. The "woken" counter
108 must exceed this value. */
109 unsigned long long int val;
110 unsigned long long int seq;
111 val = seq = cond->__data.__wakeup_seq;
112
113 /* The futex syscall operates on a 32-bit word. That is fine, we
114 just use the low 32 bits of the sequence counter. */
115#if BYTE_ORDER == LITTLE_ENDIAN
116 int *futex = ((int *) (&cond->__data.__wakeup_seq));
117#elif BYTE_ORDER == BIG_ENDIAN
118 int *futex = ((int *) (&cond->__data.__wakeup_seq)) + 1;
119#else
120# error "No valid byte order"
121#endif
122
46a32546 123 do
a88c9263
UD
124 {
125 /* Prepare to wait. Release the condvar futex. */
126 lll_mutex_unlock (cond->__data.__lock);
127
128 /* Enable asynchronous cancellation. Required by the standard. */
69431c9a 129 cbuffer.oldtype = __pthread_enable_asynccancel ();
a88c9263
UD
130
131 /* Wait until woken by signal or broadcast. Note that we
132 truncate the 'val' value to 32 bits. */
133 lll_futex_wait (futex, (unsigned int) val);
134
135 /* Disable asynchronous cancellation. */
7ce5c164 136 __pthread_disable_asynccancel (cbuffer.oldtype);
a88c9263
UD
137
138 /* We are going to look at shared data again, so get the lock. */
32a589b1 139 lll_mutex_lock (cond->__data.__lock);
a88c9263
UD
140
141 /* Check whether we are eligible for wakeup. */
142 val = cond->__data.__wakeup_seq;
a88c9263 143 }
46a32546 144 while (! (val > seq && cond->__data.__woken_seq < val));
a88c9263
UD
145
146 /* Another thread woken up. */
147 ++cond->__data.__woken_seq;
148
149 /* We are done with the condvar. */
150 lll_mutex_unlock (cond->__data.__lock);
151
152 /* The cancellation handling is back to normal, remove the handler. */
153 __pthread_cleanup_pop (&buffer, 0);
154
155 /* Get the mutex before returning. */
69431c9a 156 return __pthread_mutex_cond_lock (mutex);
a88c9263
UD
157}
158
159versioned_symbol (libpthread, __pthread_cond_wait, pthread_cond_wait,
160 GLIBC_2_3_2);