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
20 #ifndef _LOWLEVELLOCK_H
21 #define _LOWLEVELLOCK_H 1
24 #include <sys/param.h>
25 #include <bits/pthreadtypes.h>
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) \
41 INTERNAL_SYSCALL_DECL (__err); \
44 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
45 (futexp), FUTEX_WAIT, (val), 0); \
49 #define lll_futex_timed_wait(futexp, val, timespec) \
51 INTERNAL_SYSCALL_DECL (__err); \
54 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
55 (futexp), FUTEX_WAIT, (val), (timespec)); \
59 #define lll_futex_wake(futexp, nr) \
61 INTERNAL_SYSCALL_DECL (__err); \
64 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
65 (futexp), FUTEX_WAKE, (nr), 0); \
69 /* Returns non-zero if error happened, zero if success. */
70 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
72 INTERNAL_SYSCALL_DECL (__err); \
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) \
84 INTERNAL_SYSCALL_DECL (__err); \
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
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))
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
;
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))
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
*)
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);
153 if (__builtin_expect (val
!= 0, 0))
154 result
= __lll_timedlock_wait (futex
, abstime
);
157 #define lll_mutex_timedlock(futex, abstime) \
158 __lll_mutex_timedlock (&(futex), abstime)
160 #define lll_mutex_unlock(lock) \
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) \
170 int *__futex = &(lock); \
171 (void) atomic_exchange_rel (__futex, 0); \
172 lll_futex_wake (__futex, 1); \
175 #define lll_mutex_islocked(futex) \
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
201 #define lll_wait_tid(tid) \
204 __typeof (tid) __tid; \
205 while ((__tid = (tid)) != 0) \
206 lll_futex_wait (&(tid), __tid); \
210 extern int __lll_timedwait_tid (int *, const struct timespec
*)
213 #define lll_timedwait_tid(tid, abstime) \
217 __res = __lll_timedwait_tid (&(tid), (abstime)); \
222 /* Conditional variable handling. */
224 extern void __lll_cond_wait (pthread_cond_t
*cond
)
226 extern int __lll_cond_timedwait (pthread_cond_t
*cond
,
227 const struct timespec
*abstime
)
229 extern void __lll_cond_wake (pthread_cond_t
*cond
)
231 extern void __lll_cond_broadcast (pthread_cond_t
*cond
)
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 */