1 /* Copyright (C) 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Paul Mackerras <paulus@au.ibm.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>
29 #define __NR_futex 221
32 #define FUTEX_REQUEUE 3
34 /* Initializer for compatibility lock. */
35 #define LLL_MUTEX_LOCK_INITIALIZER (0)
37 #define lll_futex_wait(futexp, val) \
39 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); \
52 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
53 (futexp), FUTEX_WAIT, (val), (timespec)); \
54 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
57 #define lll_futex_wake(futexp, nr) \
59 INTERNAL_SYSCALL_DECL (__err); \
62 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
63 (futexp), FUTEX_WAKE, (nr), 0); \
64 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
67 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex) \
69 INTERNAL_SYSCALL_DECL (__err); \
72 __ret = INTERNAL_SYSCALL (futex, __err, 5, \
73 (futexp), FUTEX_REQUEUE, (nr_wake), (nr_move), \
75 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
79 # define __lll_acq_instr ""
80 # define __lll_rel_instr ""
82 # define __lll_acq_instr "isync"
83 # define __lll_rel_instr "sync"
86 /* Set *futex to 1 if it is 0, atomically. Returns the old value */
87 #define __lll_trylock(futex) \
89 __asm __volatile ("1: lwarx %0,0,%2\n" \
94 "2: " __lll_acq_instr \
95 : "=&r" (__val), "=m" (*futex) \
96 : "r" (futex), "r" (1), "1" (*futex) \
101 #define lll_mutex_trylock(lock) __lll_trylock (&(lock))
104 extern void __lll_lock_wait (int *futex) attribute_hidden;
106 #define lll_mutex_lock(lock) \
108 int *__futex = &(lock); \
109 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
111 __lll_lock_wait (__futex); \
114 #define lll_mutex_cond_lock(lock) \
116 int *__futex = &(lock); \
117 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
119 __lll_lock_wait (__futex); \
122 extern int __lll_timedlock_wait
123 (int *futex, const struct timespec *) attribute_hidden;
125 #define lll_mutex_timedlock(lock, abstime) \
127 int *__futex = &(lock); \
129 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
131 __val = __lll_timedlock_wait (__futex, abstime); \
135 #define lll_mutex_unlock(lock) \
137 int *__futex = &(lock); \
138 int __val = atomic_exchange_rel (__futex, 0); \
139 if (__builtin_expect (__val > 1, 0)) \
140 lll_futex_wake (__futex, 1); \
143 #define lll_mutex_unlock_force(lock) \
145 int *__futex = &(lock); \
147 __asm __volatile (__lll_rel_instr ::: "memory"); \
148 lll_futex_wake (__futex, 1); \
151 #define lll_mutex_islocked(futex) \
155 /* Our internal lock implementation is identical to the binary-compatible
156 mutex implementation. */
158 /* Type for lock object. */
159 typedef int lll_lock_t;
161 /* Initializers for lock. */
162 #define LLL_LOCK_INITIALIZER (0)
163 #define LLL_LOCK_INITIALIZER_LOCKED (1)
165 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
167 /* The states of a lock are:
169 1 - taken by one user
170 >1 - taken by more users */
172 #define lll_trylock(lock) lll_mutex_trylock (lock)
173 #define lll_lock(lock) lll_mutex_lock (lock)
174 #define lll_unlock(lock) lll_mutex_unlock (lock)
175 #define lll_islocked(lock) lll_mutex_islocked (lock)
177 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
178 wakeup when the clone terminates. The memory location contains the
179 thread ID while the clone is running and is reset to zero
181 #define lll_wait_tid(tid) \
183 __typeof (tid) __tid; \
184 while ((__tid = (tid)) != 0) \
185 lll_futex_wait (&(tid), __tid); \
188 extern int __lll_timedwait_tid (int *, const struct timespec *)
191 #define lll_timedwait_tid(tid, abstime) \
195 __res = __lll_timedwait_tid (&(tid), (abstime)); \
200 /* Conditional variable handling. */
202 extern void __lll_cond_wait (pthread_cond_t *cond)
204 extern int __lll_cond_timedwait (pthread_cond_t *cond,
205 const struct timespec *abstime)
207 extern void __lll_cond_wake (pthread_cond_t *cond)
209 extern void __lll_cond_broadcast (pthread_cond_t *cond)
212 #define lll_cond_wait(cond) \
213 __lll_cond_wait (cond)
214 #define lll_cond_timedwait(cond, abstime) \
215 __lll_cond_timedwait (cond, abstime)
216 #define lll_cond_wake(cond) \
217 __lll_cond_wake (cond)
218 #define lll_cond_broadcast(cond) \
219 __lll_cond_broadcast (cond)
221 #endif /* lowlevellock.h */