(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nptl / sysdeps / unix / sysv / linux / sparc / lowlevellock.h
blob4626aec5245a5a20a0d29e5f9a54710c52cbbcf9
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
34 /* Initializer for compatibility lock. */
35 #define LLL_MUTEX_LOCK_INITIALIZER (0)
37 #define lll_futex_wait(futexp, val) \
38 ({ \
39 INTERNAL_SYSCALL_DECL (__err); \
40 long int __ret; \
42 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
43 (futexp), FUTEX_WAIT, (val), 0); \
44 __ret; \
47 #define lll_futex_timed_wait(futexp, val, timespec) \
48 ({ \
49 INTERNAL_SYSCALL_DECL (__err); \
50 long int __ret; \
52 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
53 (futexp), FUTEX_WAIT, (val), (timespec)); \
54 __ret; \
57 #define lll_futex_wake(futexp, nr) \
58 ({ \
59 INTERNAL_SYSCALL_DECL (__err); \
60 long int __ret; \
62 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
63 (futexp), FUTEX_WAKE, (nr), 0); \
64 __ret; \
67 /* Returns non-zero if error happened, zero if success. */
68 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
69 ({ \
70 INTERNAL_SYSCALL_DECL (__err); \
71 long int __ret; \
73 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
74 (futexp), FUTEX_CMP_REQUEUE, (nr_wake), \
75 (nr_move), (mutex), (val)); \
76 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
79 #ifdef __sparc32_atomic_do_lock
80 #error SPARC < v9 does not support compare and swap which is essential for futex based locking
81 #endif
83 static inline int
84 __attribute__ ((always_inline))
85 __lll_mutex_trylock (int *futex)
87 return atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0;
89 #define lll_mutex_trylock(futex) __lll_mutex_trylock (&(futex))
91 static inline int
92 __attribute__ ((always_inline))
93 __lll_mutex_cond_trylock (int *futex)
95 return atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0;
97 #define lll_mutex_cond_trylock(futex) __lll_mutex_cond_trylock (&(futex))
100 extern void __lll_lock_wait (int *futex) attribute_hidden;
103 static inline void
104 __attribute__ ((always_inline))
105 __lll_mutex_lock (int *futex)
107 int val = atomic_compare_and_exchange_val_acq (futex, 1, 0);
109 if (__builtin_expect (val != 0, 0))
110 __lll_lock_wait (futex);
112 #define lll_mutex_lock(futex) __lll_mutex_lock (&(futex))
115 static inline void
116 __attribute__ ((always_inline))
117 __lll_mutex_cond_lock (int *futex)
119 int val = atomic_compare_and_exchange_val_acq (futex, 2, 0);
121 if (__builtin_expect (val != 0, 0))
122 __lll_lock_wait (futex);
124 #define lll_mutex_cond_lock(futex) __lll_mutex_cond_lock (&(futex))
127 extern int __lll_timedlock_wait (int *futex, const struct timespec *)
128 attribute_hidden;
131 static inline int
132 __attribute__ ((always_inline))
133 __lll_mutex_timedlock (int *futex, const struct timespec *abstime)
135 int val = atomic_compare_and_exchange_val_acq (futex, 1, 0);
136 int result = 0;
138 if (__builtin_expect (val != 0, 0))
139 result = __lll_timedlock_wait (futex, abstime);
140 return result;
142 #define lll_mutex_timedlock(futex, abstime) \
143 __lll_mutex_timedlock (&(futex), abstime)
145 #define lll_mutex_unlock(lock) \
146 ((void) ({ \
147 int *__futex = &(lock); \
148 int __val = atomic_exchange_rel (__futex, 0); \
149 if (__builtin_expect (__val > 1, 0)) \
150 lll_futex_wake (__futex, 1); \
153 #define lll_mutex_unlock_force(lock) \
154 ((void) ({ \
155 int *__futex = &(lock); \
156 (void) atomic_exchange_rel (__futex, 0); \
157 lll_futex_wake (__futex, 1); \
160 #define lll_mutex_islocked(futex) \
161 (futex != 0)
164 /* We have a separate internal lock implementation which is not tied
165 to binary compatibility. We can use the lll_mutex_*. */
167 /* Type for lock object. */
168 typedef int lll_lock_t;
170 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
172 /* Initializers for lock. */
173 #define LLL_LOCK_INITIALIZER (0)
174 #define LLL_LOCK_INITIALIZER_LOCKED (1)
176 #define lll_trylock(futex) lll_mutex_trylock (futex)
177 #define lll_lock(futex) lll_mutex_lock (futex)
178 #define lll_unlock(futex) lll_mutex_unlock (futex)
179 #define lll_islocked(futex) lll_mutex_islocked (futex)
182 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
183 wakeup when the clone terminates. The memory location contains the
184 thread ID while the clone is running and is reset to zero
185 afterwards. */
186 #define lll_wait_tid(tid) \
187 do \
189 __typeof (tid) __tid; \
190 while ((__tid = (tid)) != 0) \
191 lll_futex_wait (&(tid), __tid); \
193 while (0)
195 extern int __lll_timedwait_tid (int *, const struct timespec *)
196 attribute_hidden;
198 #define lll_timedwait_tid(tid, abstime) \
199 ({ \
200 int __res = 0; \
201 if ((tid) != 0) \
202 __res = __lll_timedwait_tid (&(tid), (abstime)); \
203 __res; \
207 /* Conditional variable handling. */
209 extern void __lll_cond_wait (pthread_cond_t *cond)
210 attribute_hidden;
211 extern int __lll_cond_timedwait (pthread_cond_t *cond,
212 const struct timespec *abstime)
213 attribute_hidden;
214 extern void __lll_cond_wake (pthread_cond_t *cond)
215 attribute_hidden;
216 extern void __lll_cond_broadcast (pthread_cond_t *cond)
217 attribute_hidden;
219 #define lll_cond_wait(cond) \
220 __lll_cond_wait (cond)
221 #define lll_cond_timedwait(cond, abstime) \
222 __lll_cond_timedwait (cond, abstime)
223 #define lll_cond_wake(cond) \
224 __lll_cond_wake (cond)
225 #define lll_cond_broadcast(cond) \
226 __lll_cond_broadcast (cond)
228 #endif /* lowlevellock.h */