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
19 #ifndef _LOWLEVELLOCK_H
20 #define _LOWLEVELLOCK_H 1
23 #include <sys/param.h>
24 #include <bits/pthreadtypes.h>
29 #define __NR_futex 394
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) \
40 INTERNAL_SYSCALL_DECL (__err); \
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) \
49 INTERNAL_SYSCALL_DECL (__err); \
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) \
58 INTERNAL_SYSCALL_DECL (__err); \
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) \
68 INTERNAL_SYSCALL_DECL (__err); \
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
*)
116 static inline int __attribute__ ((always_inline
))
117 __lll_mutex_timedlock (int *futex
, const struct timespec
*abstime
)
120 if (atomic_compare_and_exchange_bool_acq (futex
, 1, 0) != 0)
121 result
= __lll_timedlock_wait (futex
, abstime
);
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) \
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:
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
177 #define lll_wait_tid(tid) \
179 __typeof (tid) __tid; \
180 while ((__tid = (tid)) != 0) \
181 lll_futex_wait (&(tid), __tid); \
184 extern int __lll_timedwait_tid (int *, const struct timespec
*)
187 #define lll_timedwait_tid(tid, abstime) \
191 __res = __lll_timedwait_tid (&(tid), (abstime)); \
196 /* Conditional variable handling. */
198 extern void __lll_cond_wait (pthread_cond_t
*cond
)
200 extern int __lll_cond_timedwait (pthread_cond_t
*cond
,
201 const struct timespec
*abstime
)
203 extern void __lll_cond_wake (pthread_cond_t
*cond
)
205 extern void __lll_cond_broadcast (pthread_cond_t
*cond
)
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 */