* sysdeps/sparc/sparc32/fpu/libm-test-ulps: Update.
[glibc.git] / nptl / sysdeps / unix / sysv / linux / sparc / lowlevellock.h
blob77eefc546c0a50b2b8b67ac6fa3b322dd3dce76b
1 /* Copyright (C) 2003, 2004, 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.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 Libr \ary; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifndef _LOWLEVELLOCK_H
21 #define _LOWLEVELLOCK_H 1
23 #include <time.h>
24 #include <sys/param.h>
25 #include <bits/pthreadtypes.h>
26 #include <atomic.h>
29 #define FUTEX_WAIT 0
30 #define FUTEX_WAKE 1
31 #define FUTEX_REQUEUE 3
32 #define FUTEX_CMP_REQUEUE 4
33 #define FUTEX_WAKE_OP 5
34 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
36 /* Initializer for compatibility lock. */
37 #define LLL_MUTEX_LOCK_INITIALIZER (0)
39 #define lll_futex_wait(futexp, val) \
40 ({ \
41 INTERNAL_SYSCALL_DECL (__err); \
42 long int __ret; \
44 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
45 (futexp), FUTEX_WAIT, (val), 0); \
46 __ret; \
49 #define lll_futex_timed_wait(futexp, val, timespec) \
50 ({ \
51 INTERNAL_SYSCALL_DECL (__err); \
52 long int __ret; \
54 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
55 (futexp), FUTEX_WAIT, (val), (timespec)); \
56 __ret; \
59 #define lll_futex_wake(futexp, nr) \
60 ({ \
61 INTERNAL_SYSCALL_DECL (__err); \
62 long int __ret; \
64 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
65 (futexp), FUTEX_WAKE, (nr), 0); \
66 __ret; \
69 /* Returns non-zero if error happened, zero if success. */
70 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
71 ({ \
72 INTERNAL_SYSCALL_DECL (__err); \
73 long int __ret; \
75 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
76 (futexp), FUTEX_CMP_REQUEUE, (nr_wake), \
77 (nr_move), (mutex), (val)); \
78 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
81 #define lll_robust_mutex_dead(futexv) \
82 do \
83 { \
84 int *__futexp = &(futexv); \
85 atomic_or (__futexp, FUTEX_OWNER_DIED); \
86 lll_futex_wake (__futexp, 1); \
87 } \
88 while (0)
90 /* Returns non-zero if error happened, zero if success. */
91 #ifdef __sparc32_atomic_do_lock
92 /* Avoid FUTEX_WAKE_OP if supporting pre-v9 CPUs. */
93 # define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2) 1
94 #else
95 # define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2) \
96 ({ \
97 INTERNAL_SYSCALL_DECL (__err); \
98 long int __ret; \
100 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
101 (futexp), FUTEX_WAKE_OP, (nr_wake), \
102 (nr_wake2), (futexp2), \
103 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
104 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
106 #endif
108 static inline int
109 __attribute__ ((always_inline))
110 __lll_mutex_trylock (int *futex)
112 return atomic_compare_and_exchange_val_24_acq (futex, 1, 0) != 0;
114 #define lll_mutex_trylock(futex) __lll_mutex_trylock (&(futex))
116 static inline int
117 __attribute__ ((always_inline))
118 __lll_mutex_cond_trylock (int *futex)
120 return atomic_compare_and_exchange_val_24_acq (futex, 2, 0) != 0;
122 #define lll_mutex_cond_trylock(futex) __lll_mutex_cond_trylock (&(futex))
124 static inline int
125 __attribute__ ((always_inline))
126 __lll_robust_mutex_trylock (int *futex, int id)
128 return atomic_compare_and_exchange_val_acq (futex, id, 0) != 0;
130 #define lll_robust_mutex_trylock(futex, id) \
131 __lll_robust_mutex_trylock (&(futex), id)
134 extern void __lll_lock_wait (int *futex) attribute_hidden;
135 extern int __lll_robust_lock_wait (int *futex) attribute_hidden;
137 static inline void
138 __attribute__ ((always_inline))
139 __lll_mutex_lock (int *futex)
141 int val = atomic_compare_and_exchange_val_24_acq (futex, 1, 0);
143 if (__builtin_expect (val != 0, 0))
144 __lll_lock_wait (futex);
146 #define lll_mutex_lock(futex) __lll_mutex_lock (&(futex))
148 static inline int
149 __attribute__ ((always_inline))
150 __lll_robust_mutex_lock (int *futex, int id)
152 int result = 0;
153 if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
154 result = __lll_robust_lock_wait (futex);
155 return result;
157 #define lll_robust_mutex_lock(futex, id) \
158 __lll_robust_mutex_lock (&(futex), id)
160 static inline void
161 __attribute__ ((always_inline))
162 __lll_mutex_cond_lock (int *futex)
164 int val = atomic_compare_and_exchange_val_24_acq (futex, 2, 0);
166 if (__builtin_expect (val != 0, 0))
167 __lll_lock_wait (futex);
169 #define lll_mutex_cond_lock(futex) __lll_mutex_cond_lock (&(futex))
171 #define lll_robust_mutex_cond_lock(futex, id) \
172 __lll_robust_mutex_lock (&(futex), (id) | FUTEX_WAITERS)
175 extern int __lll_timedlock_wait (int *futex, const struct timespec *)
176 attribute_hidden;
177 extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *)
178 attribute_hidden;
180 static inline int
181 __attribute__ ((always_inline))
182 __lll_mutex_timedlock (int *futex, const struct timespec *abstime)
184 int val = atomic_compare_and_exchange_val_24_acq (futex, 1, 0);
185 int result = 0;
187 if (__builtin_expect (val != 0, 0))
188 result = __lll_timedlock_wait (futex, abstime);
189 return result;
191 #define lll_mutex_timedlock(futex, abstime) \
192 __lll_mutex_timedlock (&(futex), abstime)
194 static inline int
195 __attribute__ ((always_inline))
196 __lll_robust_mutex_timedlock (int *futex, const struct timespec *abstime,
197 int id)
199 int result = 0;
200 if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
201 result = __lll_robust_timedlock_wait (futex, abstime);
202 return result;
204 #define lll_robust_mutex_timedlock(futex, abstime, id) \
205 __lll_robust_mutex_timedlock (&(futex), abstime, id)
207 #define lll_mutex_unlock(lock) \
208 ((void) ({ \
209 int *__futex = &(lock); \
210 int __val = atomic_exchange_24_rel (__futex, 0); \
211 if (__builtin_expect (__val > 1, 0)) \
212 lll_futex_wake (__futex, 1); \
215 #define lll_robust_mutex_unlock(lock) \
216 ((void) ({ \
217 int *__futex = &(lock); \
218 int __val = atomic_exchange_rel (__futex, 0); \
219 if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
220 lll_futex_wake (__futex, 1); \
223 #define lll_mutex_unlock_force(lock) \
224 ((void) ({ \
225 int *__futex = &(lock); \
226 (void) atomic_exchange_24_rel (__futex, 0); \
227 lll_futex_wake (__futex, 1); \
230 #define lll_mutex_islocked(futex) \
231 (futex != 0)
234 /* We have a separate internal lock implementation which is not tied
235 to binary compatibility. We can use the lll_mutex_*. */
237 /* Type for lock object. */
238 typedef int lll_lock_t;
240 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
242 /* Initializers for lock. */
243 #define LLL_LOCK_INITIALIZER (0)
244 #define LLL_LOCK_INITIALIZER_LOCKED (1)
246 #define lll_trylock(futex) lll_mutex_trylock (futex)
247 #define lll_lock(futex) lll_mutex_lock (futex)
248 #define lll_unlock(futex) lll_mutex_unlock (futex)
249 #define lll_islocked(futex) lll_mutex_islocked (futex)
252 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
253 wakeup when the clone terminates. The memory location contains the
254 thread ID while the clone is running and is reset to zero
255 afterwards. */
256 #define lll_wait_tid(tid) \
257 do \
259 __typeof (tid) __tid; \
260 while ((__tid = (tid)) != 0) \
261 lll_futex_wait (&(tid), __tid); \
263 while (0)
265 extern int __lll_timedwait_tid (int *, const struct timespec *)
266 attribute_hidden;
268 #define lll_timedwait_tid(tid, abstime) \
269 ({ \
270 int __res = 0; \
271 if ((tid) != 0) \
272 __res = __lll_timedwait_tid (&(tid), (abstime)); \
273 __res; \
277 /* Conditional variable handling. */
279 extern void __lll_cond_wait (pthread_cond_t *cond)
280 attribute_hidden;
281 extern int __lll_cond_timedwait (pthread_cond_t *cond,
282 const struct timespec *abstime)
283 attribute_hidden;
284 extern void __lll_cond_wake (pthread_cond_t *cond)
285 attribute_hidden;
286 extern void __lll_cond_broadcast (pthread_cond_t *cond)
287 attribute_hidden;
289 #define lll_cond_wait(cond) \
290 __lll_cond_wait (cond)
291 #define lll_cond_timedwait(cond, abstime) \
292 __lll_cond_timedwait (cond, abstime)
293 #define lll_cond_wake(cond) \
294 __lll_cond_wake (cond)
295 #define lll_cond_broadcast(cond) \
296 __lll_cond_broadcast (cond)
298 #endif /* lowlevellock.h */