2 * Copyright (c) 2003 Fabrice Bellard
4 * This 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 of the License, or (at your option) any later version.
9 * This 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 this library; if not, see <http://www.gnu.org/licenses/>
18 /* Locking primitives. Most of this code should be redundant -
19 system emulation doesn't need/use locking, NPTL userspace uses
20 pthread mutexes, and non-NPTL userspace isn't threadsafe anyway.
21 In either case a spinlock is probably the wrong kind of lock.
22 Spinlocks are only good if you know annother CPU has the lock and is
23 likely to release it soon. In environments where you have more threads
24 than physical CPUs (the extreme case being a single CPU host) a spinlock
25 simply wastes CPU until the OS decides to preempt it. */
26 #if defined(CONFIG_USE_NPTL)
29 #define spin_lock pthread_mutex_lock
30 #define spin_unlock pthread_mutex_unlock
31 #define spinlock_t pthread_mutex_t
32 #define SPIN_LOCK_UNLOCKED PTHREAD_MUTEX_INITIALIZER
38 typedef int spinlock_t
[4];
40 #define SPIN_LOCK_UNLOCKED { 1, 1, 1, 1 }
42 static inline void resetlock (spinlock_t
*p
)
44 (*p
)[0] = (*p
)[1] = (*p
)[2] = (*p
)[3] = 1;
49 typedef int spinlock_t
;
51 #define SPIN_LOCK_UNLOCKED 0
53 static inline void resetlock (spinlock_t
*p
)
55 *p
= SPIN_LOCK_UNLOCKED
;
60 #if defined(_ARCH_PPC)
61 static inline int testandset (int *p
)
64 __asm__
__volatile__ (
71 : "r" (p
), "r" (1), "r" (0)
75 #elif defined(__i386__)
76 static inline int testandset (int *p
)
80 __asm__
__volatile__ ("lock; cmpxchgl %2, %0"
81 : "+m" (*p
), "+a" (readval
)
86 #elif defined(__x86_64__)
87 static inline int testandset (int *p
)
91 __asm__
__volatile__ ("lock; cmpxchgl %2, %0"
92 : "+m" (*p
), "+a" (readval
)
97 #elif defined(__s390__)
98 static inline int testandset (int *p
)
102 __asm__
__volatile__ ("0: cs %0,%1,0(%2)\n"
105 : "r" (1), "a" (p
), "0" (*p
)
109 #elif defined(__alpha__)
110 static inline int testandset (int *p
)
115 __asm__
__volatile__ ("0: mov 1,%2\n"
122 : "=r" (ret
), "=m" (*p
), "=r" (one
)
126 #elif defined(__sparc__)
127 static inline int testandset (int *p
)
131 __asm__
__volatile__("ldstub [%1], %0"
136 return (ret
? 1 : 0);
138 #elif defined(__arm__)
139 static inline int testandset (int *spinlock
)
141 register unsigned int ret
;
142 __asm__
__volatile__("swp %0, %1, [%2]"
144 : "0"(1), "r"(spinlock
));
148 #elif defined(__mc68000)
149 static inline int testandset (int *p
)
152 __asm__
__volatile__("tas %1; sne %0"
158 #elif defined(__hppa__)
160 /* Because malloc only guarantees 8-byte alignment for malloc'd data,
161 and GCC only guarantees 8-byte alignment for stack locals, we can't
162 be assured of 16-byte alignment for atomic lock data even if we
163 specify "__attribute ((aligned(16)))" in the type declaration. So,
164 we use a struct containing an array of four ints for the atomic lock
165 type and dynamically select the 16-byte aligned int from the array
166 for the semaphore. */
167 #define __PA_LDCW_ALIGNMENT 16
168 static inline void *ldcw_align (void *p
) {
169 unsigned long a
= (unsigned long)p
;
170 a
= (a
+ __PA_LDCW_ALIGNMENT
- 1) & ~(__PA_LDCW_ALIGNMENT
- 1);
174 static inline int testandset (spinlock_t
*p
)
178 __asm__
__volatile__("ldcw 0(%1),%0"
185 #elif defined(__ia64)
187 #include <ia64intrin.h>
189 static inline int testandset (int *p
)
191 return __sync_lock_test_and_set (p
, 1);
193 #elif defined(__mips__)
194 static inline int testandset (int *p
)
198 __asm__
__volatile__ (
207 : "=r" (ret
), "+R" (*p
)
214 #error unimplemented CPU support
217 #if defined(CONFIG_USER_ONLY)
218 static inline void spin_lock(spinlock_t
*lock
)
220 while (testandset(lock
));
223 static inline void spin_unlock(spinlock_t
*lock
)
228 static inline void spin_lock(spinlock_t
*lock
)
232 static inline void spin_unlock(spinlock_t
*lock
)