]> sourceware.org Git - glibc.git/blob - nptl/sysdeps/unix/sysv/linux/powerpc/lowlevellock.h
2003-03-17 Roland McGrath <roland@redhat.com>
[glibc.git] / nptl / sysdeps / unix / sysv / linux / powerpc / lowlevellock.h
1 /* Copyright (C) 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Paul Mackerras <paulus@au.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 Libr \ary; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20 #ifndef _LOWLEVELLOCK_H
21 #define _LOWLEVELLOCK_H 1
22
23 #include <time.h>
24 #include <sys/param.h>
25 #include <bits/pthreadtypes.h>
26
27 #define __NR_futex 221
28 #define FUTEX_WAIT 0
29 #define FUTEX_WAKE 1
30
31 /* Initializer for compatibility lock. */
32 #define LLL_MUTEX_LOCK_INITIALIZER (0)
33
34 #define lll_futex_wait(futexp, val) \
35 ({ \
36 INTERNAL_SYSCALL_DECL (__err); \
37 long int __ret; \
38 \
39 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
40 (futexp), FUTEX_WAIT, (val), 0); \
41 INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret: 0; \
42 })
43
44 #define lll_futex_timed_wait(futexp, val, timespec) \
45 ({ \
46 INTERNAL_SYSCALL_DECL (__err); \
47 long int __ret; \
48 \
49 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
50 (futexp), FUTEX_WAIT, (val), (timespec)); \
51 INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret: 0; \
52 })
53
54 #define lll_futex_wake(futexp, nr) \
55 ({ \
56 INTERNAL_SYSCALL_DECL (__err); \
57 long int __ret; \
58 \
59 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
60 (futexp), FUTEX_WAKE, (nr), 0); \
61 INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret: 0; \
62 })
63
64 #ifdef UP
65 #define __lll_acq_instr ""
66 #define __lll_rel_instr ""
67 #else
68 #define __lll_acq_instr "isync"
69 #define __lll_rel_instr "sync"
70 #endif
71
72 /* Set *futex to 1 if it is 0, atomically. Returns the old value */
73 #define __lll_trylock(futex) \
74 ({ int __val; \
75 __asm __volatile ("1: lwarx %0,0,%2\n" \
76 " cmpwi 0,%0,0\n" \
77 " bne 2f\n" \
78 " stwcx. %3,0,%2\n" \
79 " bne- 1b\n" \
80 "2: " __lll_acq_instr \
81 : "=&r" (__val), "=m" (*futex) \
82 : "r" (futex), "r" (1), "1" (*futex) \
83 : "cr0", "memory"); \
84 __val; \
85 })
86
87 #define lll_mutex_trylock(lock) __lll_trylock(&(lock))
88
89 /* Add inc to *futex atomically and return the old value. */
90 #define __lll_add(futex, inc) \
91 ({ int __val, __tmp; \
92 __asm __volatile ("1: lwarx %0,0,%3\n" \
93 " addi %1,%0,%4\n" \
94 " stwcx. %1,0,%3\n" \
95 " bne- 1b" \
96 : "=&b" (__val), "=&r" (__tmp), "=m" (*futex) \
97 : "r" (futex), "I" (inc), "2" (*futex) \
98 : "cr0"); \
99 __val; \
100 })
101
102 extern void __lll_lock_wait (int *futex, int val) attribute_hidden;
103
104 #define lll_mutex_lock(lock) \
105 (void) ({ \
106 int *__futex = &(lock); \
107 int __val = __lll_add (__futex, 1); \
108 __asm __volatile (__lll_acq_instr ::: "memory"); \
109 if (__builtin_expect (__val != 0, 0)) \
110 __lll_lock_wait (__futex, __val); \
111 })
112
113 extern int __lll_timedlock_wait
114 (int *futex, int val, const struct timespec *) attribute_hidden;
115
116 #define lll_mutex_timedlock(lock, abstime) \
117 ({ int *__futex = &(lock); \
118 int __val = __lll_add (__futex, 1); \
119 __asm __volatile (__lll_acq_instr ::: "memory"); \
120 if (__builtin_expect (__val != 0, 0)) \
121 __val = __lll_timedlock_wait (__futex, __val, (abstime)); \
122 __val; \
123 })
124
125 #define lll_mutex_unlock(lock) \
126 (void) ({ \
127 int *__futex = &(lock); \
128 __asm __volatile (__lll_rel_instr ::: "memory"); \
129 int __val = __lll_add (__futex, -1); \
130 if (__builtin_expect (__val != 1, 0)) \
131 { \
132 *__futex = 0; \
133 lll_futex_wake (__futex, 1); \
134 } \
135 })
136
137 #define lll_mutex_islocked(futex) \
138 (futex != 0)
139
140
141 /* Our internal lock implementation is identical to the binary-compatible
142 mutex implementation. */
143
144 /* Type for lock object. */
145 typedef int lll_lock_t;
146
147 /* Initializers for lock. */
148 #define LLL_LOCK_INITIALIZER (0)
149 #define LLL_LOCK_INITIALIZER_LOCKED (1)
150
151 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
152
153 /* The states of a lock are:
154 0 - untaken
155 1 - taken by one user
156 >1 - taken by more users */
157
158 #define lll_trylock(lock) lll_mutex_trylock (lock)
159 #define lll_lock(lock) lll_mutex_lock (lock)
160 #define lll_unlock(lock) lll_mutex_unlock (lock)
161 #define lll_islocked(lock) lll_mutex_islocked (lock)
162
163 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
164 wakeup when the clone terminates. The memory location contains the
165 thread ID while the clone is running and is reset to zero
166 afterwards. */
167 #define lll_wait_tid(tid) \
168 do { \
169 __typeof (tid) __tid; \
170 while ((__tid = (tid)) != 0) \
171 lll_futex_wait (&(tid), __tid); \
172 } while (0)
173
174 extern int __lll_timedwait_tid (int *, const struct timespec *)
175 attribute_hidden;
176
177 #define lll_timedwait_tid(tid, abstime) \
178 ({ \
179 int __res = 0; \
180 if ((tid) != 0) \
181 __res = __lll_timedwait_tid (&(tid), (abstime)); \
182 __res; \
183 })
184
185
186 /* Decrement *futex if it is > 0, and return the old value */
187 #define __lll_dec_if_positive(futex) \
188 ({ int __val, __tmp; \
189 __asm __volatile ("1: lwarx %0,0,%3\n" \
190 " cmpwi 0,%0,0\n" \
191 " addi %1,%0,-1\n" \
192 " ble 2f\n" \
193 " stwcx. %1,0,%3\n" \
194 " bne- 1b\n" \
195 "2: " __lll_acq_instr \
196 : "=&b" (__val), "=&r" (__tmp), "=m" (*futex) \
197 : "r" (futex), "2" (*futex) \
198 : "cr0"); \
199 __val; \
200 })
201
202
203 /* Conditional variable handling. */
204
205 extern void __lll_cond_wait (pthread_cond_t *cond)
206 attribute_hidden;
207 extern int __lll_cond_timedwait (pthread_cond_t *cond,
208 const struct timespec *abstime)
209 attribute_hidden;
210 extern void __lll_cond_wake (pthread_cond_t *cond)
211 attribute_hidden;
212 extern void __lll_cond_broadcast (pthread_cond_t *cond)
213 attribute_hidden;
214
215 #define lll_cond_wait(cond) \
216 __lll_cond_wait (cond)
217 #define lll_cond_timedwait(cond, abstime) \
218 __lll_cond_timedwait (cond, abstime)
219 #define lll_cond_wake(cond) \
220 __lll_cond_wake (cond)
221 #define lll_cond_broadcast(cond) \
222 __lll_cond_broadcast (cond)
223
224 #endif /* lowlevellock.h */
This page took 0.047238 seconds and 5 git commands to generate.