Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / include / asm-mips / spinlock.h
blob724d105200fc73419721d84dd319e6e65055fe94
1 /* $Id: spinlock.h,v 1.8 2000/01/23 21:15:52 ralf Exp $
3 * This file is subject to the terms and conditions of the GNU General Public
4 * License. See the file "COPYING" in the main directory of this archive
5 * for more details.
7 * Copyright (C) 1999, 2000 by Ralf Baechle
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9 */
10 #ifndef _ASM_SPINLOCK_H
11 #define _ASM_SPINLOCK_H
14 * Your basic SMP spinlocks, allowing only a single CPU anywhere
17 typedef struct {
18 volatile unsigned int lock;
19 } spinlock_t;
21 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
23 #define spin_lock_init(x) do { (x)->lock = 0; } while(0);
26 * Simple spin lock operations. There are two variants, one clears IRQ's
27 * on the local processor, one does not.
29 * We make no fairness assumptions. They have a cost.
32 typedef struct { unsigned long a[100]; } __dummy_lock_t;
33 #define __dummy_lock(lock) (*(__dummy_lock_t *)(lock))
35 static inline void spin_lock(spinlock_t *lock)
37 unsigned int tmp;
39 __asm__ __volatile__(
40 ".set\tnoreorder\t\t\t# spin_lock\n"
41 "1:\tll\t%1, %2\n\t"
42 "bnez\t%1, 1b\n\t"
43 " li\t%1, 1\n\t"
44 "sc\t%1, %0\n\t"
45 "beqz\t%1, 1b\n\t"
46 " sync\n\t"
47 ".set\treorder"
48 : "=o" (__dummy_lock(lock)), "=&r" (tmp)
49 : "o" (__dummy_lock(lock))
50 : "memory");
53 static inline void spin_unlock(spinlock_t *lock)
55 __asm__ __volatile__(
56 ".set\tnoreorder\t\t\t# spin_unlock\n\t"
57 "sync\n\t"
58 "sw\t$0, %0\n\t"
59 ".set\treorder"
60 : "=o" (__dummy_lock(lock))
61 : "o" (__dummy_lock(lock))
62 : "memory");
65 #define spin_trylock(lock) (!test_and_set_bit(0,(lock)))
68 * Read-write spinlocks, allowing multiple readers but only one writer.
70 * NOTE! it is quite common to have readers in interrupts but no interrupt
71 * writers. For those circumstances we can "mix" irq-safe locks - any writer
72 * needs to get a irq-safe write-lock, but readers can get non-irqsafe
73 * read-locks.
76 typedef struct {
77 volatile unsigned int lock;
78 } rwlock_t;
80 #define RW_LOCK_UNLOCKED (rwlock_t) { 0 }
82 static inline void read_lock(rwlock_t *rw)
84 unsigned int tmp;
86 __asm__ __volatile__(
87 ".set\tnoreorder\t\t\t# read_lock\n"
88 "1:\tll\t%1, %2\n\t"
89 "bltz\t%1, 1b\n\t"
90 " addu\t%1, 1\n\t"
91 "sc\t%1, %0\n\t"
92 "beqz\t%1, 1b\n\t"
93 " sync\n\t"
94 ".set\treorder"
95 : "=o" (__dummy_lock(rw)), "=&r" (tmp)
96 : "o" (__dummy_lock(rw))
97 : "memory");
100 /* Note the use of sub, not subu which will make the kernel die with an
101 overflow exception if we ever try to unlock an rwlock that is already
102 unlocked or is being held by a writer. */
103 static inline void read_unlock(rwlock_t *rw)
105 unsigned int tmp;
107 __asm__ __volatile__(
108 ".set\tnoreorder\t\t\t# read_unlock\n"
109 "1:\tll\t%1, %2\n\t"
110 "sub\t%1, 1\n\t"
111 "sc\t%1, %0\n\t"
112 "beqz\t%1, 1b\n\t"
113 ".set\treorder"
114 : "=o" (__dummy_lock(rw)), "=&r" (tmp)
115 : "o" (__dummy_lock(rw))
116 : "memory");
119 static inline void write_lock(rwlock_t *rw)
121 unsigned int tmp;
123 __asm__ __volatile__(
124 ".set\tnoreorder\t\t\t# write_lock\n"
125 "1:\tll\t%1, %2\n\t"
126 "bnez\t%1, 1b\n\t"
127 " lui\t%1, 0x8000\n\t"
128 "sc\t%1, %0\n\t"
129 "beqz\t%1, 1b\n\t"
130 " sync\n\t"
131 ".set\treorder"
132 : "=o" (__dummy_lock(rw)), "=&r" (tmp)
133 : "o" (__dummy_lock(rw))
134 : "memory");
137 static inline void write_unlock(rwlock_t *rw)
139 __asm__ __volatile__(
140 ".set\tnoreorder\t\t\t# write_unlock\n\t"
141 "sync\n\t"
142 "sw\t$0, %0\n\t"
143 ".set\treorder"
144 : "=o" (__dummy_lock(rw))
145 : "o" (__dummy_lock(rw))
146 : "memory");
149 #endif /* _ASM_SPINLOCK_H */