* sysdeps/unix/sysv/linux/powerpc/lowlevellock.h (FUTEX_WAKE_OP,
[glibc.git] / nptl / sysdeps / unix / sysv / linux / sparc / lowlevellock.h
blob8d12db3a16cc2ad3c55efc2bfd3f0d9f85fcebdd
1 /* Copyright (C) 2003, 2004 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 /* Returns non-zero if error happened, zero if success. */
82 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2) \
83 ({ \
84 INTERNAL_SYSCALL_DECL (__err); \
85 long int __ret; \
87 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
88 (futexp), FUTEX_WAKE_OP, (nr_wake), \
89 (nr_wake2), (futexp2), \
90 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
91 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
94 #ifdef __sparc32_atomic_do_lock
95 #error SPARC < v9 does not support compare and swap which is essential for futex based locking
96 #endif
98 static inline int
99 __attribute__ ((always_inline))
100 __lll_mutex_trylock (int *futex)
102 return atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0;
104 #define lll_mutex_trylock(futex) __lll_mutex_trylock (&(futex))
106 static inline int
107 __attribute__ ((always_inline))
108 __lll_mutex_cond_trylock (int *futex)
110 return atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0;
112 #define lll_mutex_cond_trylock(futex) __lll_mutex_cond_trylock (&(futex))
115 extern void __lll_lock_wait (int *futex) attribute_hidden;
118 static inline void
119 __attribute__ ((always_inline))
120 __lll_mutex_lock (int *futex)
122 int val = atomic_compare_and_exchange_val_acq (futex, 1, 0);
124 if (__builtin_expect (val != 0, 0))
125 __lll_lock_wait (futex);
127 #define lll_mutex_lock(futex) __lll_mutex_lock (&(futex))
130 static inline void
131 __attribute__ ((always_inline))
132 __lll_mutex_cond_lock (int *futex)
134 int val = atomic_compare_and_exchange_val_acq (futex, 2, 0);
136 if (__builtin_expect (val != 0, 0))
137 __lll_lock_wait (futex);
139 #define lll_mutex_cond_lock(futex) __lll_mutex_cond_lock (&(futex))
142 extern int __lll_timedlock_wait (int *futex, const struct timespec *)
143 attribute_hidden;
146 static inline int
147 __attribute__ ((always_inline))
148 __lll_mutex_timedlock (int *futex, const struct timespec *abstime)
150 int val = atomic_compare_and_exchange_val_acq (futex, 1, 0);
151 int result = 0;
153 if (__builtin_expect (val != 0, 0))
154 result = __lll_timedlock_wait (futex, abstime);
155 return result;
157 #define lll_mutex_timedlock(futex, abstime) \
158 __lll_mutex_timedlock (&(futex), abstime)
160 #define lll_mutex_unlock(lock) \
161 ((void) ({ \
162 int *__futex = &(lock); \
163 int __val = atomic_exchange_rel (__futex, 0); \
164 if (__builtin_expect (__val > 1, 0)) \
165 lll_futex_wake (__futex, 1); \
168 #define lll_mutex_unlock_force(lock) \
169 ((void) ({ \
170 int *__futex = &(lock); \
171 (void) atomic_exchange_rel (__futex, 0); \
172 lll_futex_wake (__futex, 1); \
175 #define lll_mutex_islocked(futex) \
176 (futex != 0)
179 /* We have a separate internal lock implementation which is not tied
180 to binary compatibility. We can use the lll_mutex_*. */
182 /* Type for lock object. */
183 typedef int lll_lock_t;
185 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
187 /* Initializers for lock. */
188 #define LLL_LOCK_INITIALIZER (0)
189 #define LLL_LOCK_INITIALIZER_LOCKED (1)
191 #define lll_trylock(futex) lll_mutex_trylock (futex)
192 #define lll_lock(futex) lll_mutex_lock (futex)
193 #define lll_unlock(futex) lll_mutex_unlock (futex)
194 #define lll_islocked(futex) lll_mutex_islocked (futex)
197 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
198 wakeup when the clone terminates. The memory location contains the
199 thread ID while the clone is running and is reset to zero
200 afterwards. */
201 #define lll_wait_tid(tid) \
202 do \
204 __typeof (tid) __tid; \
205 while ((__tid = (tid)) != 0) \
206 lll_futex_wait (&(tid), __tid); \
208 while (0)
210 extern int __lll_timedwait_tid (int *, const struct timespec *)
211 attribute_hidden;
213 #define lll_timedwait_tid(tid, abstime) \
214 ({ \
215 int __res = 0; \
216 if ((tid) != 0) \
217 __res = __lll_timedwait_tid (&(tid), (abstime)); \
218 __res; \
222 /* Conditional variable handling. */
224 extern void __lll_cond_wait (pthread_cond_t *cond)
225 attribute_hidden;
226 extern int __lll_cond_timedwait (pthread_cond_t *cond,
227 const struct timespec *abstime)
228 attribute_hidden;
229 extern void __lll_cond_wake (pthread_cond_t *cond)
230 attribute_hidden;
231 extern void __lll_cond_broadcast (pthread_cond_t *cond)
232 attribute_hidden;
234 #define lll_cond_wait(cond) \
235 __lll_cond_wait (cond)
236 #define lll_cond_timedwait(cond, abstime) \
237 __lll_cond_timedwait (cond, abstime)
238 #define lll_cond_wake(cond) \
239 __lll_cond_wake (cond)
240 #define lll_cond_broadcast(cond) \
241 __lll_cond_broadcast (cond)
243 #endif /* lowlevellock.h */