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>
27 #define __NR_futex 221
31 /* Initializer for compatibility lock. */
32 #define LLL_MUTEX_LOCK_INITIALIZER (0)
34 #define lll_futex_wait(futexp, val) \
36 INTERNAL_SYSCALL_DECL (__err); \
39 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
40 (futexp), FUTEX_WAIT, (val), 0); \
41 INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret: 0; \
44 #define lll_futex_timed_wait(futexp, val, timespec) \
46 INTERNAL_SYSCALL_DECL (__err); \
49 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
50 (futexp), FUTEX_WAIT, (val), (timespec)); \
51 INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret: 0; \
54 #define lll_futex_wake(futexp, nr) \
56 INTERNAL_SYSCALL_DECL (__err); \
59 __ret = INTERNAL_SYSCALL (futex, __err, 4, \
60 (futexp), FUTEX_WAKE, (nr), 0); \
61 INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret: 0; \
65 # define __lll_acq_instr ""
66 # define __lll_rel_instr ""
68 # define __lll_acq_instr "isync"
69 # define __lll_rel_instr "sync"
72 /* Set *futex to 1 if it is 0, atomically. Returns the old value */
73 #define __lll_trylock(futex) \
75 __asm __volatile ("1: lwarx %0,0,%2\n" \
80 "2: " __lll_acq_instr \
81 : "=&r" (__val), "=m" (*futex) \
82 : "r" (futex), "r" (1), "1" (*futex) \
87 #define lll_mutex_trylock(lock) __lll_trylock(&(lock))
89 /* Add inc to *futex atomically and return the old value. */
90 #define __lll_add(futex, inc) \
91 ({ int __val, __tmp; \
92 __asm __volatile ("1: lwarx %0,0,%3\n" \
96 : "=&b" (__val), "=&r" (__tmp), "=m" (*futex) \
97 : "r" (futex), "I" (inc), "2" (*futex) \
102 /* Atomically store newval and return the old value. */
103 #define __lll_test_and_set(futex, newval) \
105 __asm __volatile (__lll_rel_instr "\n" \
106 "1: lwarx %0,0,%2\n" \
107 " stwcx. %3,0,%2\n" \
109 : "=&r" (__val), "=m" (*futex) \
110 : "r" (futex), "r" (newval), "1" (*futex) \
111 : "cr0", "memory"); \
115 extern void __lll_lock_wait (int *futex, int val) attribute_hidden;
117 #define lll_mutex_lock(lock) \
119 int *__futex = &(lock); \
120 int __val = __lll_add (__futex, 1); \
121 __asm __volatile (__lll_acq_instr ::: "memory"); \
122 if (__builtin_expect (__val != 0, 0)) \
123 __lll_lock_wait (__futex, __val); \
126 extern int __lll_timedlock_wait
127 (int *futex, int val, const struct timespec *) attribute_hidden;
129 #define lll_mutex_timedlock(lock, abstime) \
130 ({ int *__futex = &(lock); \
131 int __val = __lll_add (__futex, 1); \
132 __asm __volatile (__lll_acq_instr ::: "memory"); \
133 if (__builtin_expect (__val != 0, 0)) \
134 __val = __lll_timedlock_wait (__futex, __val, (abstime)); \
138 #define lll_mutex_unlock(lock) \
140 int *__futex = &(lock); \
141 int __val = __lll_test_and_set (__futex, 0); \
142 if (__builtin_expect (__val > 1, 0)) \
143 lll_futex_wake (__futex, 1); \
146 #define lll_mutex_islocked(futex) \
150 /* Our internal lock implementation is identical to the binary-compatible
151 mutex implementation. */
153 /* Type for lock object. */
154 typedef int lll_lock_t;
156 /* Initializers for lock. */
157 #define LLL_LOCK_INITIALIZER (0)
158 #define LLL_LOCK_INITIALIZER_LOCKED (1)
160 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
162 /* The states of a lock are:
164 1 - taken by one user
165 >1 - taken by more users */
167 #define lll_trylock(lock) lll_mutex_trylock (lock)
168 #define lll_lock(lock) lll_mutex_lock (lock)
169 #define lll_unlock(lock) lll_mutex_unlock (lock)
170 #define lll_islocked(lock) lll_mutex_islocked (lock)
172 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
173 wakeup when the clone terminates. The memory location contains the
174 thread ID while the clone is running and is reset to zero
176 #define lll_wait_tid(tid) \
178 __typeof (tid) __tid; \
179 while ((__tid = (tid)) != 0) \
180 lll_futex_wait (&(tid), __tid); \
183 extern int __lll_timedwait_tid (int *, const struct timespec *)
186 #define lll_timedwait_tid(tid, abstime) \
190 __res = __lll_timedwait_tid (&(tid), (abstime)); \
195 /* Decrement *futex if it is > 0, and return the old value */
196 #define __lll_dec_if_positive(futex) \
197 ({ int __val, __tmp; \
198 __asm __volatile ("1: lwarx %0,0,%3\n" \
202 " stwcx. %1,0,%3\n" \
204 "2: " __lll_acq_instr \
205 : "=&b" (__val), "=&r" (__tmp), "=m" (*futex) \
206 : "r" (futex), "2" (*futex) \
212 /* Conditional variable handling. */
214 extern void __lll_cond_wait (pthread_cond_t *cond)
216 extern int __lll_cond_timedwait (pthread_cond_t *cond,
217 const struct timespec *abstime)
219 extern void __lll_cond_wake (pthread_cond_t *cond)
221 extern void __lll_cond_broadcast (pthread_cond_t *cond)
224 #define lll_cond_wait(cond) \
225 __lll_cond_wait (cond)
226 #define lll_cond_timedwait(cond, abstime) \
227 __lll_cond_timedwait (cond, abstime)
228 #define lll_cond_wake(cond) \
229 __lll_cond_wake (cond)
230 #define lll_cond_broadcast(cond) \
231 __lll_cond_broadcast (cond)
233 #endif /* lowlevellock.h */