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