~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/include/asm-mips/spinlock.h

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* $Id: spinlock.h,v 1.8 2000/01/23 21:15:52 ralf Exp $
  2  *
  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.
  6  *
  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
 12 
 13 /*
 14  * Your basic SMP spinlocks, allowing only a single CPU anywhere
 15  */
 16 
 17 typedef struct {
 18         volatile unsigned int lock;
 19 } spinlock_t;
 20 
 21 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
 22 
 23 #define spin_lock_init(x)       do { (x)->lock = 0; } while(0);
 24 
 25 /*
 26  * Simple spin lock operations.  There are two variants, one clears IRQ's
 27  * on the local processor, one does not.
 28  *
 29  * We make no fairness assumptions.  They have a cost.
 30  */
 31 
 32 typedef struct { unsigned long a[100]; } __dummy_lock_t;
 33 #define __dummy_lock(lock) (*(__dummy_lock_t *)(lock))
 34 
 35 static inline void spin_lock(spinlock_t *lock)
 36 {
 37         unsigned int tmp;
 38 
 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");
 51 }
 52 
 53 static inline void spin_unlock(spinlock_t *lock)
 54 {
 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");
 63 }
 64 
 65 #define spin_trylock(lock) (!test_and_set_bit(0,(lock)))
 66 
 67 /*
 68  * Read-write spinlocks, allowing multiple readers but only one writer.
 69  *
 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.
 74  */
 75 
 76 typedef struct {
 77         volatile unsigned int lock;
 78 } rwlock_t;
 79 
 80 #define RW_LOCK_UNLOCKED (rwlock_t) { 0 }
 81 
 82 static inline void read_lock(rwlock_t *rw)
 83 {
 84         unsigned int tmp;
 85 
 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");
 98 }
 99 
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)
104 {
105         unsigned int tmp;
106 
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");
117 }
118 
119 static inline void write_lock(rwlock_t *rw)
120 {
121         unsigned int tmp;
122 
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");
135 }
136 
137 static inline void write_unlock(rwlock_t *rw)
138 {
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");
147 }
148 
149 #endif /* _ASM_SPINLOCK_H */
150 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.