1 /* Copyright (C) 2003, 2004 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
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); \
43 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
44 (futexp), FUTEX_WAIT, (val), 0); \
45 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
48 #define lll_futex_timed_wait(futexp, val, timespec) \
50 INTERNAL_SYSCALL_DECL (__err); \
53 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
54 (futexp), FUTEX_WAIT, (val), (timespec)); \
55 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
58 #define lll_futex_wake(futexp, nr) \
60 INTERNAL_SYSCALL_DECL (__err); \
63 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
64 (futexp), FUTEX_WAKE, (nr), 0); \
65 INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
68 /* Returns non-zero if error happened, zero if success. */
69 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
71 INTERNAL_SYSCALL_DECL (__err); \
74 __ret = INTERNAL_SYSCALL (futex, __err, 6, \
75 (futexp), FUTEX_CMP_REQUEUE, (nr_wake), \
76 (nr_move), (mutex), (val)); \
77 INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
81 # define __lll_acq_instr ""
82 # define __lll_rel_instr ""
84 # define __lll_acq_instr "isync"
85 # define __lll_rel_instr "sync"
88 /* Set *futex to 1 if it is 0, atomically. Returns the old value */
89 #define __lll_trylock(futex) \
91 __asm __volatile ("1: lwarx %0,0,%2\n" \
96 "2: " __lll_acq_instr \
97 : "=&r" (__val), "=m" (*futex) \
98 : "r" (futex), "r" (1), "m" (*futex) \
103 #define lll_mutex_trylock(lock) __lll_trylock (&(lock))
105 /* Set *futex to 2 if it is 0, atomically. Returns the old value */
106 #define __lll_cond_trylock(futex) \
108 __asm __volatile ("1: lwarx %0,0,%2\n" \
111 " stwcx. %3,0,%2\n" \
113 "2: " __lll_acq_instr \
114 : "=&r" (__val), "=m" (*futex) \
115 : "r" (futex), "r" (2), "m" (*futex) \
116 : "cr0", "memory"); \
119 #define lll_mutex_cond_trylock(lock) __lll_cond_trylock (&(lock))
122 extern void __lll_lock_wait (int *futex) attribute_hidden;
124 #define lll_mutex_lock(lock) \
126 int *__futex = &(lock); \
127 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
129 __lll_lock_wait (__futex); \
132 #define lll_mutex_cond_lock(lock) \
134 int *__futex = &(lock); \
135 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
137 __lll_lock_wait (__futex); \
140 extern int __lll_timedlock_wait
141 (int *futex, const struct timespec *) attribute_hidden;
143 #define lll_mutex_timedlock(lock, abstime) \
145 int *__futex = &(lock); \
147 if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
149 __val = __lll_timedlock_wait (__futex, abstime); \
153 #define lll_mutex_unlock(lock) \
155 int *__futex = &(lock); \
156 int __val = atomic_exchange_rel (__futex, 0); \
157 if (__builtin_expect (__val > 1, 0)) \
158 lll_futex_wake (__futex, 1); \
161 #define lll_mutex_unlock_force(lock) \
163 int *__futex = &(lock); \
165 __asm __volatile (__lll_rel_instr ::: "memory"); \
166 lll_futex_wake (__futex, 1); \
169 #define lll_mutex_islocked(futex) \
173 /* Our internal lock implementation is identical to the binary-compatible
174 mutex implementation. */
176 /* Type for lock object. */
177 typedef int lll_lock_t;
179 /* Initializers for lock. */
180 #define LLL_LOCK_INITIALIZER (0)
181 #define LLL_LOCK_INITIALIZER_LOCKED (1)
183 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
185 /* The states of a lock are:
187 1 - taken by one user
188 >1 - taken by more users */
190 #define lll_trylock(lock) lll_mutex_trylock (lock)
191 #define lll_lock(lock) lll_mutex_lock (lock)
192 #define lll_unlock(lock) lll_mutex_unlock (lock)
193 #define lll_islocked(lock) lll_mutex_islocked (lock)
195 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
196 wakeup when the clone terminates. The memory location contains the
197 thread ID while the clone is running and is reset to zero
199 #define lll_wait_tid(tid) \
201 __typeof (tid) __tid; \
202 while ((__tid = (tid)) != 0) \
203 lll_futex_wait (&(tid), __tid); \
206 extern int __lll_timedwait_tid (int *, const struct timespec *)
209 #define lll_timedwait_tid(tid, abstime) \
213 __res = __lll_timedwait_tid (&(tid), (abstime)); \
218 /* Conditional variable handling. */
220 extern void __lll_cond_wait (pthread_cond_t *cond)
222 extern int __lll_cond_timedwait (pthread_cond_t *cond,
223 const struct timespec *abstime)
225 extern void __lll_cond_wake (pthread_cond_t *cond)
227 extern void __lll_cond_broadcast (pthread_cond_t *cond)
230 #define lll_cond_wait(cond) \
231 __lll_cond_wait (cond)
232 #define lll_cond_timedwait(cond, abstime) \
233 __lll_cond_timedwait (cond, abstime)
234 #define lll_cond_wake(cond) \
235 __lll_cond_wake (cond)
236 #define lll_cond_broadcast(cond) \
237 __lll_cond_broadcast (cond)
239 #endif /* lowlevellock.h */