Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __ASM_SPINLOCK_H |
| 2 | #define __ASM_SPINLOCK_H |
| 3 | |
| 4 | #include <asm/system.h> |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 5 | #include <asm/processor.h> |
| 6 | #include <asm/spinlock_types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | |
| 8 | /* Note that PA-RISC has to use `1' to mean unlocked and `0' to mean locked |
| 9 | * since it only has load-and-zero. Moreover, at least on some PA processors, |
| 10 | * the semaphore address has to be 16-byte aligned. |
| 11 | */ |
| 12 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 13 | static inline int __raw_spin_is_locked(raw_spinlock_t *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | { |
| 15 | volatile unsigned int *a = __ldcw_align(x); |
| 16 | return *a == 0; |
| 17 | } |
| 18 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 19 | #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock) |
| 20 | #define __raw_spin_unlock_wait(x) \ |
| 21 | do { cpu_relax(); } while (__raw_spin_is_locked(x)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 23 | static inline void __raw_spin_lock(raw_spinlock_t *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | { |
| 25 | volatile unsigned int *a; |
| 26 | |
| 27 | mb(); |
| 28 | a = __ldcw_align(x); |
| 29 | while (__ldcw(a) == 0) |
| 30 | while (*a == 0); |
| 31 | mb(); |
| 32 | } |
| 33 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 34 | static inline void __raw_spin_unlock(raw_spinlock_t *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
| 36 | volatile unsigned int *a; |
| 37 | mb(); |
| 38 | a = __ldcw_align(x); |
| 39 | *a = 1; |
| 40 | mb(); |
| 41 | } |
| 42 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 43 | static inline int __raw_spin_trylock(raw_spinlock_t *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
| 45 | volatile unsigned int *a; |
| 46 | int ret; |
| 47 | |
| 48 | mb(); |
| 49 | a = __ldcw_align(x); |
| 50 | ret = __ldcw(a) != 0; |
| 51 | mb(); |
| 52 | |
| 53 | return ret; |
| 54 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * Read-write spinlocks, allowing multiple readers |
| 58 | * but only one writer. |
| 59 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 61 | #define __raw_read_trylock(lock) generic__raw_read_trylock(lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | /* read_lock, read_unlock are pretty straightforward. Of course it somehow |
| 64 | * sucks we end up saving/restoring flags twice for read_lock_irqsave aso. */ |
| 65 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 66 | static __inline__ void __raw_read_lock(raw_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
| 68 | unsigned long flags; |
| 69 | local_irq_save(flags); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 70 | __raw_spin_lock(&rw->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | rw->counter++; |
| 73 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 74 | __raw_spin_unlock(&rw->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | local_irq_restore(flags); |
| 76 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 78 | static __inline__ void __raw_read_unlock(raw_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
| 80 | unsigned long flags; |
| 81 | local_irq_save(flags); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 82 | __raw_spin_lock(&rw->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | rw->counter--; |
| 85 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 86 | __raw_spin_unlock(&rw->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | local_irq_restore(flags); |
| 88 | } |
| 89 | |
| 90 | /* write_lock is less trivial. We optimistically grab the lock and check |
| 91 | * if we surprised any readers. If so we release the lock and wait till |
| 92 | * they're all gone before trying again |
| 93 | * |
| 94 | * Also note that we don't use the _irqsave / _irqrestore suffixes here. |
| 95 | * If we're called with interrupts enabled and we've got readers (or other |
| 96 | * writers) in interrupt handlers someone fucked up and we'd dead-lock |
| 97 | * sooner or later anyway. prumpf */ |
| 98 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 99 | static __inline__ void __raw_write_lock(raw_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | retry: |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 102 | __raw_spin_lock(&rw->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
| 104 | if(rw->counter != 0) { |
| 105 | /* this basically never happens */ |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 106 | __raw_spin_unlock(&rw->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 108 | while (rw->counter != 0) |
| 109 | cpu_relax(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
| 111 | goto retry; |
| 112 | } |
| 113 | |
| 114 | /* got it. now leave without unlocking */ |
| 115 | rw->counter = -1; /* remember we are locked */ |
| 116 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | /* write_unlock is absolutely trivial - we don't have to wait for anything */ |
| 119 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 120 | static __inline__ void __raw_write_unlock(raw_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
| 122 | rw->counter = 0; |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 123 | __raw_spin_unlock(&rw->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 126 | static __inline__ int __raw_write_trylock(raw_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 128 | __raw_spin_lock(&rw->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | if (rw->counter != 0) { |
| 130 | /* this basically never happens */ |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 131 | __raw_spin_unlock(&rw->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | /* got it. now leave without unlocking */ |
| 137 | rw->counter = -1; /* remember we are locked */ |
| 138 | return 1; |
| 139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 141 | static __inline__ int __raw_is_read_locked(raw_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
| 143 | return rw->counter > 0; |
| 144 | } |
| 145 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame^] | 146 | static __inline__ int __raw_is_write_locked(raw_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
| 148 | return rw->counter < 0; |
| 149 | } |
| 150 | |
| 151 | #endif /* __ASM_SPINLOCK_H */ |