]> sourceware.org Git - glibc.git/blob - nptl/sysdeps/pthread/pthread_cond_wait.c
Update.
[glibc.git] / nptl / sysdeps / pthread / pthread_cond_wait.c
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
30 struct _condvar_cleanup_buffer
31 {
32 int oldtype;
33 pthread_cond_t *cond;
34 pthread_mutex_t *mutex;
35 };
36
37 void
38 __attribute__ ((visibility ("hidden")))
39 __condvar_cleanup (void *arg)
40 {
41 struct _condvar_cleanup_buffer *cbuffer =
42 (struct _condvar_cleanup_buffer *) arg;
43
44 /* We are going to modify shared data. */
45 lll_mutex_lock (cbuffer->cond->__data.__lock);
46
47 /* This thread is not waiting anymore. Adjust the sequence counters
48 appropriately. */
49 ++cbuffer->cond->__data.__wakeup_seq;
50 ++cbuffer->cond->__data.__woken_seq;
51
52 /* We are done. */
53 lll_mutex_unlock (cbuffer->cond->__data.__lock);
54
55 /* Get the mutex before returning unless asynchronous cancellation
56 is in effect. */
57 if (!(cbuffer->oldtype & CANCELTYPE_BITMASK))
58 __pthread_mutex_lock_internal (cbuffer->mutex);
59 }
60
61
62 int
63 __pthread_cond_wait (cond, mutex)
64 pthread_cond_t *cond;
65 pthread_mutex_t *mutex;
66 {
67 struct _pthread_cleanup_buffer buffer;
68 struct _condvar_cleanup_buffer cbuffer;
69 int err;
70
71 /* Make sure we are along. */
72 lll_mutex_lock (cond->__data.__lock);
73
74 /* Now we can release the mutex. */
75 err = __pthread_mutex_unlock_internal (mutex);
76 if (err)
77 {
78 lll_mutex_unlock (cond->__data.__lock);
79 return err;
80 }
81
82 /* We have one new user of the condvar. */
83 ++cond->__data.__total_seq;
84
85 /* Prepare structure passed to cancellation handler. */
86 cbuffer.cond = cond;
87 cbuffer.mutex = mutex;
88
89 /* Before we block we enable cancellation. Therefore we have to
90 install a cancellation handler. */
91 __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
92
93 /* The current values of the wakeup counter. The "woken" counter
94 must exceed this value. */
95 unsigned long long int val;
96 unsigned long long int seq;
97 val = seq = cond->__data.__wakeup_seq;
98
99 /* The futex syscall operates on a 32-bit word. That is fine, we
100 just use the low 32 bits of the sequence counter. */
101 #if BYTE_ORDER == LITTLE_ENDIAN
102 int *futex = ((int *) (&cond->__data.__wakeup_seq));
103 #elif BYTE_ORDER == BIG_ENDIAN
104 int *futex = ((int *) (&cond->__data.__wakeup_seq)) + 1;
105 #else
106 # error "No valid byte order"
107 #endif
108
109 while (1)
110 {
111 /* Prepare to wait. Release the condvar futex. */
112 lll_mutex_unlock (cond->__data.__lock);
113
114 /* Enable asynchronous cancellation. Required by the standard. */
115 cbuffer.oldtype = __pthread_enable_asynccancel ();
116
117 /* Wait until woken by signal or broadcast. Note that we
118 truncate the 'val' value to 32 bits. */
119 lll_futex_wait (futex, (unsigned int) val);
120
121 /* Disable asynchronous cancellation. */
122 __pthread_disable_asynccancel (cbuffer.oldtype);
123
124 /* We are going to look at shared data again, so get the lock. */
125 lll_mutex_lock(cond->__data.__lock);
126
127 /* Check whether we are eligible for wakeup. */
128 val = cond->__data.__wakeup_seq;
129 if (cond->__data.__woken_seq >= seq
130 && cond->__data.__woken_seq < val)
131 break;
132 }
133
134 /* Another thread woken up. */
135 ++cond->__data.__woken_seq;
136
137 /* We are done with the condvar. */
138 lll_mutex_unlock (cond->__data.__lock);
139
140 /* The cancellation handling is back to normal, remove the handler. */
141 __pthread_cleanup_pop (&buffer, 0);
142
143 /* Get the mutex before returning. */
144 return __pthread_mutex_lock_internal (mutex);
145 }
146
147 versioned_symbol (libpthread, __pthread_cond_wait, pthread_cond_wait,
148 GLIBC_2_3_2);
This page took 0.042598 seconds and 5 git commands to generate.