2 * include/asm-xtensa/spinlock.h
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (C) 2001 - 2005 Tensilica Inc.
11 #ifndef _XTENSA_SPINLOCK_H
12 #define _XTENSA_SPINLOCK_H
14 #include <asm/barrier.h>
15 #include <asm/processor.h>
20 * There is at most one owner of a spinlock. There are not different
21 * types of spinlock owners like there are for rwlocks (see below).
23 * When trying to obtain a spinlock, the function "spins" forever, or busy-
24 * waits, until the lock is obtained. When spinning, presumably some other
25 * owner will soon give up the spinlock making it available to others. Use
26 * the trylock functions to avoid spinning forever.
30 * 0 nobody owns the spinlock
31 * 1 somebody owns the spinlock
34 #define arch_spin_is_locked(x) ((x)->slock != 0)
36 #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
38 static inline void arch_spin_lock(arch_spinlock_t
*lock
)
44 " wsr %0, scompare1\n"
53 /* Returns 1 if the lock is obtained, 0 otherwise. */
55 static inline int arch_spin_trylock(arch_spinlock_t
*lock
)
61 " wsr %0, scompare1\n"
68 return tmp
== 0 ? 1 : 0;
71 static inline void arch_spin_unlock(arch_spinlock_t
*lock
)
86 * Read-write locks are really a more flexible spinlock. They allow
87 * multiple readers but only one writer. Write ownership is exclusive
88 * (i.e., all other readers and writers are blocked from ownership while
89 * there is a write owner). These rwlocks are unfair to writers. Writers
90 * can be starved for an indefinite time by readers.
94 * 0 nobody owns the rwlock
95 * >0 one or more readers own the rwlock
96 * (the positive value is the actual number of readers)
97 * 0x80000000 one writer owns the rwlock, no other writers, no readers
100 #define arch_write_can_lock(x) ((x)->lock == 0)
102 static inline void arch_write_lock(arch_rwlock_t
*rw
)
106 __asm__
__volatile__(
108 " wsr %0, scompare1\n"
111 " s32c1i %0, %1, 0\n"
118 /* Returns 1 if the lock is obtained, 0 otherwise. */
120 static inline int arch_write_trylock(arch_rwlock_t
*rw
)
124 __asm__
__volatile__(
126 " wsr %0, scompare1\n"
129 " s32c1i %0, %1, 0\n"
134 return tmp
== 0 ? 1 : 0;
137 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
141 __asm__
__volatile__(
149 static inline void arch_read_lock(arch_rwlock_t
*rw
)
152 unsigned long result
;
154 __asm__
__volatile__(
155 "1: l32i %1, %2, 0\n"
157 " wsr %1, scompare1\n"
159 " s32c1i %0, %2, 0\n"
161 : "=&a" (result
), "=&a" (tmp
)
166 /* Returns 1 if the lock is obtained, 0 otherwise. */
168 static inline int arch_read_trylock(arch_rwlock_t
*rw
)
170 unsigned long result
;
173 __asm__
__volatile__(
177 " wsr %1, scompare1\n"
178 " s32c1i %0, %2, 0\n"
181 : "=&a" (result
), "=&a" (tmp
)
188 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
190 unsigned long tmp1
, tmp2
;
192 __asm__
__volatile__(
193 "1: l32i %1, %2, 0\n"
195 " wsr %1, scompare1\n"
196 " s32c1i %0, %2, 0\n"
198 : "=&a" (tmp1
), "=&a" (tmp2
)
203 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
204 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
206 #endif /* _XTENSA_SPINLOCK_H */