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.
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.
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.
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
23 #include <lowlevellock.h>
27 #include <shlib-compat.h>
30 struct _condvar_cleanup_buffer
34 pthread_mutex_t
*mutex
;
38 __attribute__ ((visibility ("hidden")))
39 __condvar_cleanup (void *arg
)
41 struct _condvar_cleanup_buffer
*cbuffer
=
42 (struct _condvar_cleanup_buffer
*) arg
;
44 /* We are going to modify shared data. */
45 lll_mutex_lock (cbuffer
->cond
->__data
.__lock
);
47 /* This thread is not waiting anymore. Adjust the sequence counters
49 ++cbuffer
->cond
->__data
.__wakeup_seq
;
50 ++cbuffer
->cond
->__data
.__woken_seq
;
53 lll_mutex_unlock (cbuffer
->cond
->__data
.__lock
);
55 /* Get the mutex before returning unless asynchronous cancellation
57 if (!(cbuffer
->oldtype
& CANCELTYPE_BITMASK
))
58 __pthread_mutex_lock_internal (cbuffer
->mutex
);
63 __pthread_cond_wait (cond
, mutex
)
65 pthread_mutex_t
*mutex
;
67 struct _pthread_cleanup_buffer buffer
;
68 struct _condvar_cleanup_buffer cbuffer
;
71 /* Make sure we are along. */
72 lll_mutex_lock (cond
->__data
.__lock
);
74 /* Now we can release the mutex. */
75 err
= __pthread_mutex_unlock_internal (mutex
);
78 lll_mutex_unlock (cond
->__data
.__lock
);
82 /* We have one new user of the condvar. */
83 ++cond
->__data
.__total_seq
;
85 /* Prepare structure passed to cancellation handler. */
87 cbuffer
.mutex
= mutex
;
89 /* Before we block we enable cancellation. Therefore we have to
90 install a cancellation handler. */
91 __pthread_cleanup_push (&buffer
, __condvar_cleanup
, &cbuffer
);
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
;
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;
106 # error "No valid byte order"
111 /* Prepare to wait. Release the condvar futex. */
112 lll_mutex_unlock (cond
->__data
.__lock
);
114 /* Enable asynchronous cancellation. Required by the standard. */
115 cbuffer
.oldtype
= __pthread_enable_asynccancel ();
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
);
121 /* Disable asynchronous cancellation. */
122 __pthread_disable_asynccancel (cbuffer
.oldtype
);
124 /* We are going to look at shared data again, so get the lock. */
125 lll_mutex_lock(cond
->__data
.__lock
);
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
)
134 /* Another thread woken up. */
135 ++cond
->__data
.__woken_seq
;
137 /* We are done with the condvar. */
138 lll_mutex_unlock (cond
->__data
.__lock
);
140 /* The cancellation handling is back to normal, remove the handler. */
141 __pthread_cleanup_pop (&buffer
, 0);
143 /* Get the mutex before returning. */
144 return __pthread_mutex_lock_internal (mutex
);
147 versioned_symbol (libpthread
, __pthread_cond_wait
, pthread_cond_wait
,