(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nptl / sysdeps / unix / sysv / linux / alpha / lowlevellock.h
blobfd4a7ca4bb4feab5d4ae30498acce3ce46d76a43
1 /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Libr \ary; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #ifndef _LOWLEVELLOCK_H
20 #define _LOWLEVELLOCK_H 1
22 #include <time.h>
23 #include <sys/param.h>
24 #include <bits/pthreadtypes.h>
25 #include <atomic.h>
26 #include <sysdep.h>
29 #define __NR_futex 394
30 #define FUTEX_WAIT 0
31 #define FUTEX_WAKE 1
32 #define FUTEX_REQUEUE 3
33 #define FUTEX_CMP_REQUEUE 4
35 /* Initializer for compatibility lock. */
36 #define LLL_MUTEX_LOCK_INITIALIZER (0)
38 #define lll_futex_wait(futexp, val) \
39 ({ \
40 INTERNAL_SYSCALL_DECL (__err); \
41 long int __ret; \
42 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
43 (futexp), FUTEX_WAIT, (val), 0); \
44 INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __ret; \
47 #define lll_futex_timed_wait(futexp, val, timespec) \
48 ({ \
49 INTERNAL_SYSCALL_DECL (__err); \
50 long int __ret; \
51 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
52 (futexp), FUTEX_WAIT, (val), (timespec)); \
53 INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __ret; \
56 #define lll_futex_wake(futexp, nr) \
57 ({ \
58 INTERNAL_SYSCALL_DECL (__err); \
59 long int __ret; \
60 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
61 (futexp), FUTEX_WAKE, (nr), 0); \
62 INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __ret; \
65 /* Returns non-zero if error happened, zero if success. */
66 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
67 ({ \
68 INTERNAL_SYSCALL_DECL (__err); \
69 long int __ret; \
70 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
71 (futexp), FUTEX_CMP_REQUEUE, (nr_wake), \
72 (nr_move), (mutex), (val)); \
73 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
77 static inline int __attribute__((always_inline))
78 __lll_mutex_trylock(int *futex)
80 return atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0;
82 #define lll_mutex_trylock(lock) __lll_mutex_trylock (&(lock))
85 static inline int __attribute__((always_inline))
86 __lll_mutex_cond_trylock(int *futex)
88 return atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0;
90 #define lll_mutex_cond_trylock(lock) __lll_mutex_cond_trylock (&(lock))
93 extern void __lll_lock_wait (int *futex) attribute_hidden;
95 static inline void __attribute__((always_inline))
96 __lll_mutex_lock(int *futex)
98 if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
99 __lll_lock_wait (futex);
101 #define lll_mutex_lock(futex) __lll_mutex_lock (&(futex))
104 static inline void __attribute__ ((always_inline))
105 __lll_mutex_cond_lock (int *futex)
107 if (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0)
108 __lll_lock_wait (futex);
110 #define lll_mutex_cond_lock(futex) __lll_mutex_cond_lock (&(futex))
113 extern int __lll_timedlock_wait (int *futex, const struct timespec *)
114 attribute_hidden;
116 static inline int __attribute__ ((always_inline))
117 __lll_mutex_timedlock (int *futex, const struct timespec *abstime)
119 int result = 0;
120 if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
121 result = __lll_timedlock_wait (futex, abstime);
122 return result;
124 #define lll_mutex_timedlock(futex, abstime) \
125 __lll_mutex_timedlock (&(futex), abstime)
128 static inline void __attribute__ ((always_inline))
129 __lll_mutex_unlock (int *futex)
131 int val = atomic_exchange_rel (futex, 0);
132 if (__builtin_expect (val > 1, 0))
133 lll_futex_wake (futex, 1);
135 #define lll_mutex_unlock(futex) __lll_mutex_unlock(&(futex))
138 static inline void __attribute__ ((always_inline))
139 __lll_mutex_unlock_force (int *futex)
141 (void) atomic_exchange_rel (futex, 0);
142 lll_futex_wake (futex, 1);
144 #define lll_mutex_unlock_force(futex) __lll_mutex_unlock_force(&(futex))
147 #define lll_mutex_islocked(futex) \
148 (futex != 0)
151 /* Our internal lock implementation is identical to the binary-compatible
152 mutex implementation. */
154 /* Type for lock object. */
155 typedef int lll_lock_t;
157 /* Initializers for lock. */
158 #define LLL_LOCK_INITIALIZER (0)
159 #define LLL_LOCK_INITIALIZER_LOCKED (1)
161 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
163 /* The states of a lock are:
164 0 - untaken
165 1 - taken by one user
166 >1 - taken by more users */
168 #define lll_trylock(lock) lll_mutex_trylock (lock)
169 #define lll_lock(lock) lll_mutex_lock (lock)
170 #define lll_unlock(lock) lll_mutex_unlock (lock)
171 #define lll_islocked(lock) lll_mutex_islocked (lock)
173 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
174 wakeup when the clone terminates. The memory location contains the
175 thread ID while the clone is running and is reset to zero
176 afterwards. */
177 #define lll_wait_tid(tid) \
178 do { \
179 __typeof (tid) __tid; \
180 while ((__tid = (tid)) != 0) \
181 lll_futex_wait (&(tid), __tid); \
182 } while (0)
184 extern int __lll_timedwait_tid (int *, const struct timespec *)
185 attribute_hidden;
187 #define lll_timedwait_tid(tid, abstime) \
188 ({ \
189 int __res = 0; \
190 if ((tid) != 0) \
191 __res = __lll_timedwait_tid (&(tid), (abstime)); \
192 __res; \
196 /* Conditional variable handling. */
198 extern void __lll_cond_wait (pthread_cond_t *cond)
199 attribute_hidden;
200 extern int __lll_cond_timedwait (pthread_cond_t *cond,
201 const struct timespec *abstime)
202 attribute_hidden;
203 extern void __lll_cond_wake (pthread_cond_t *cond)
204 attribute_hidden;
205 extern void __lll_cond_broadcast (pthread_cond_t *cond)
206 attribute_hidden;
208 #define lll_cond_wait(cond) \
209 __lll_cond_wait (cond)
210 #define lll_cond_timedwait(cond, abstime) \
211 __lll_cond_timedwait (cond, abstime)
212 #define lll_cond_wake(cond) \
213 __lll_cond_wake (cond)
214 #define lll_cond_broadcast(cond) \
215 __lll_cond_broadcast (cond)
217 #endif /* lowlevellock.h */