2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1999, 2000 by Ralf Baechle
7 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9 #ifndef _ASM_SPINLOCK_H
10 #define _ASM_SPINLOCK_H
13 * Your basic SMP spinlocks, allowing only a single CPU anywhere
17 volatile unsigned int lock
;
20 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
22 #define spin_lock_init(x) do { (x)->lock = 0; } while(0)
24 #define spin_is_locked(x) ((x)->lock != 0)
25 #define spin_unlock_wait(x) do { barrier(); } while ((x)->lock)
26 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
29 * Simple spin lock operations. There are two variants, one clears IRQ's
30 * on the local processor, one does not.
32 * We make no fairness assumptions. They have a cost.
35 static inline void _raw_spin_lock(spinlock_t
*lock
)
40 ".set\tnoreorder\t\t\t# _raw_spin_lock\n"
48 : "=m" (lock
->lock
), "=&r" (tmp
)
53 static inline void _raw_spin_unlock(spinlock_t
*lock
)
56 ".set\tnoreorder\t\t\t# _raw_spin_unlock\n\t"
65 static inline unsigned int _raw_spin_trylock(spinlock_t
*lock
)
67 unsigned int temp
, res
;
70 ".set\tnoreorder\t\t\t# _raw_spin_trylock\n\t"
75 " andi\t%2, %0, 1\n\t"
77 : "=&r" (temp
), "=m" (lock
->lock
), "=&r" (res
)
85 * Read-write spinlocks, allowing multiple readers but only one writer.
87 * NOTE! it is quite common to have readers in interrupts but no interrupt
88 * writers. For those circumstances we can "mix" irq-safe locks - any writer
89 * needs to get a irq-safe write-lock, but readers can get non-irqsafe
94 volatile unsigned int lock
;
97 #define RW_LOCK_UNLOCKED (rwlock_t) { 0 }
99 #define rwlock_init(x) do { *(x) = RW_LOCK_UNLOCKED; } while(0)
101 #define rwlock_is_locked(x) ((x)->lock)
103 static inline void _raw_read_lock(rwlock_t
*rw
)
107 __asm__
__volatile__(
108 ".set\tnoreorder\t\t\t# _raw_read_lock\n"
116 : "=m" (rw
->lock
), "=&r" (tmp
)
121 /* Note the use of sub, not subu which will make the kernel die with an
122 overflow exception if we ever try to unlock an rwlock that is already
123 unlocked or is being held by a writer. */
124 static inline void _raw_read_unlock(rwlock_t
*rw
)
128 __asm__
__volatile__(
129 ".set\tnoreorder\t\t\t# _raw_read_unlock\n"
136 : "=m" (rw
->lock
), "=&r" (tmp
)
141 static inline void _raw_write_lock(rwlock_t
*rw
)
145 __asm__
__volatile__(
146 ".set\tnoreorder\t\t\t# _raw_write_lock\n"
149 " lui\t%1, 0x8000\n\t"
154 : "=m" (rw
->lock
), "=&r" (tmp
)
159 static inline void _raw_write_unlock(rwlock_t
*rw
)
161 __asm__
__volatile__(
162 ".set\tnoreorder\t\t\t# _raw_write_unlock\n\t"
171 static inline int _raw_write_trylock(rwlock_t
*rw
)
176 __asm__
__volatile__(
177 ".set\tnoreorder\t\t\t# _raw_write_trylock\n"
181 "lui\t%1, 0x8000\n\t"
188 : "=m" (rw
->lock
), "=&r" (tmp
), "=&r" (ret
)
195 #endif /* _ASM_SPINLOCK_H */