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
17 * There is at most one owner of a spinlock. There are not different
18 * types of spinlock owners like there are for rwlocks (see below).
20 * When trying to obtain a spinlock, the function "spins" forever, or busy-
21 * waits, until the lock is obtained. When spinning, presumably some other
22 * owner will soon give up the spinlock making it available to others. Use
23 * the trylock functions to avoid spinning forever.
27 * 0 nobody owns the spinlock
28 * 1 somebody owns the spinlock
31 #define arch_spin_is_locked(x) ((x)->slock != 0)
32 #define arch_spin_unlock_wait(lock) \
33 do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0)
35 #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
37 static inline void arch_spin_lock(arch_spinlock_t
*lock
)
43 " wsr %0, scompare1\n"
52 /* Returns 1 if the lock is obtained, 0 otherwise. */
54 static inline int arch_spin_trylock(arch_spinlock_t
*lock
)
60 " wsr %0, scompare1\n"
67 return tmp
== 0 ? 1 : 0;
70 static inline void arch_spin_unlock(arch_spinlock_t
*lock
)
85 * Read-write locks are really a more flexible spinlock. They allow
86 * multiple readers but only one writer. Write ownership is exclusive
87 * (i.e., all other readers and writers are blocked from ownership while
88 * there is a write owner). These rwlocks are unfair to writers. Writers
89 * can be starved for an indefinite time by readers.
93 * 0 nobody owns the rwlock
94 * >0 one or more readers own the rwlock
95 * (the positive value is the actual number of readers)
96 * 0x80000000 one writer owns the rwlock, no other writers, no readers
99 #define arch_write_can_lock(x) ((x)->lock == 0)
101 static inline void arch_write_lock(arch_rwlock_t
*rw
)
105 __asm__
__volatile__(
107 " wsr %0, scompare1\n"
110 " s32c1i %0, %1, 0\n"
117 /* Returns 1 if the lock is obtained, 0 otherwise. */
119 static inline int arch_write_trylock(arch_rwlock_t
*rw
)
123 __asm__
__volatile__(
125 " wsr %0, scompare1\n"
128 " s32c1i %0, %1, 0\n"
133 return tmp
== 0 ? 1 : 0;
136 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
140 __asm__
__volatile__(
148 static inline void arch_read_lock(arch_rwlock_t
*rw
)
151 unsigned long result
;
153 __asm__
__volatile__(
154 "1: l32i %1, %2, 0\n"
156 " wsr %1, scompare1\n"
158 " s32c1i %0, %2, 0\n"
160 : "=&a" (result
), "=&a" (tmp
)
165 /* Returns 1 if the lock is obtained, 0 otherwise. */
167 static inline int arch_read_trylock(arch_rwlock_t
*rw
)
169 unsigned long result
;
172 __asm__
__volatile__(
176 " wsr %1, scompare1\n"
177 " s32c1i %0, %2, 0\n"
180 : "=&a" (result
), "=&a" (tmp
)
187 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
189 unsigned long tmp1
, tmp2
;
191 __asm__
__volatile__(
192 "1: l32i %1, %2, 0\n"
194 " wsr %1, scompare1\n"
195 " s32c1i %0, %2, 0\n"
197 : "=&a" (tmp1
), "=&a" (tmp2
)
202 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
203 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
205 #endif /* _XTENSA_SPINLOCK_H */