Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 1 | #ifndef __LINUX_SPINLOCK_UP_H |
| 2 | #define __LINUX_SPINLOCK_UP_H |
| 3 | |
| 4 | #ifndef __LINUX_SPINLOCK_H |
| 5 | # error "please don't include this file directly" |
| 6 | #endif |
| 7 | |
Stephen Rothwell | d974d90 | 2011-05-20 15:48:17 +1000 | [diff] [blame] | 8 | #include <asm/processor.h> /* for cpu_relax() */ |
Peter Zijlstra | 726328d | 2016-05-26 10:35:03 +0200 | [diff] [blame] | 9 | #include <asm/barrier.h> |
Stephen Rothwell | d974d90 | 2011-05-20 15:48:17 +1000 | [diff] [blame] | 10 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 11 | /* |
| 12 | * include/linux/spinlock_up.h - UP-debug version of spinlocks. |
| 13 | * |
| 14 | * portions Copyright 2005, Red Hat, Inc., Ingo Molnar |
| 15 | * Released under the General Public License (GPL). |
| 16 | * |
| 17 | * In the debug case, 1 means unlocked, 0 means locked. (the values |
| 18 | * are inverted, to catch initialization bugs) |
| 19 | * |
Linus Torvalds | 386afc9 | 2013-04-09 10:48:33 -0700 | [diff] [blame] | 20 | * No atomicity anywhere, we are on UP. However, we still need |
| 21 | * the compiler barriers, because we do not want the compiler to |
| 22 | * move potentially faulting instructions (notably user accesses) |
| 23 | * into the locked sequence, resulting in non-atomic execution. |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #ifdef CONFIG_DEBUG_SPINLOCK |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 27 | #define arch_spin_is_locked(x) ((x)->slock == 0) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 28 | |
Peter Zijlstra | 726328d | 2016-05-26 10:35:03 +0200 | [diff] [blame] | 29 | static inline void arch_spin_unlock_wait(arch_spinlock_t *lock) |
| 30 | { |
| 31 | smp_cond_load_acquire(&lock->slock, VAL); |
| 32 | } |
| 33 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 34 | static inline void arch_spin_lock(arch_spinlock_t *lock) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 35 | { |
| 36 | lock->slock = 0; |
Linus Torvalds | 386afc9 | 2013-04-09 10:48:33 -0700 | [diff] [blame] | 37 | barrier(); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static inline void |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 41 | arch_spin_lock_flags(arch_spinlock_t *lock, unsigned long flags) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 42 | { |
| 43 | local_irq_save(flags); |
| 44 | lock->slock = 0; |
Linus Torvalds | 386afc9 | 2013-04-09 10:48:33 -0700 | [diff] [blame] | 45 | barrier(); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 48 | static inline int arch_spin_trylock(arch_spinlock_t *lock) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 49 | { |
| 50 | char oldval = lock->slock; |
| 51 | |
| 52 | lock->slock = 0; |
Linus Torvalds | 386afc9 | 2013-04-09 10:48:33 -0700 | [diff] [blame] | 53 | barrier(); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 54 | |
| 55 | return oldval > 0; |
| 56 | } |
| 57 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 58 | static inline void arch_spin_unlock(arch_spinlock_t *lock) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 59 | { |
Linus Torvalds | 386afc9 | 2013-04-09 10:48:33 -0700 | [diff] [blame] | 60 | barrier(); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 61 | lock->slock = 1; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Read-write spinlocks. No debug version. |
| 66 | */ |
Linus Torvalds | 386afc9 | 2013-04-09 10:48:33 -0700 | [diff] [blame] | 67 | #define arch_read_lock(lock) do { barrier(); (void)(lock); } while (0) |
| 68 | #define arch_write_lock(lock) do { barrier(); (void)(lock); } while (0) |
| 69 | #define arch_read_trylock(lock) ({ barrier(); (void)(lock); 1; }) |
| 70 | #define arch_write_trylock(lock) ({ barrier(); (void)(lock); 1; }) |
| 71 | #define arch_read_unlock(lock) do { barrier(); (void)(lock); } while (0) |
| 72 | #define arch_write_unlock(lock) do { barrier(); (void)(lock); } while (0) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 73 | |
| 74 | #else /* DEBUG_SPINLOCK */ |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 75 | #define arch_spin_is_locked(lock) ((void)(lock), 0) |
Peter Zijlstra | 726328d | 2016-05-26 10:35:03 +0200 | [diff] [blame] | 76 | #define arch_spin_unlock_wait(lock) do { barrier(); (void)(lock); } while (0) |
Viresh Kumar | 0a0fca9 | 2013-06-04 13:10:24 +0530 | [diff] [blame] | 77 | /* for sched/core.c and kernel_lock.c: */ |
Linus Torvalds | 386afc9 | 2013-04-09 10:48:33 -0700 | [diff] [blame] | 78 | # define arch_spin_lock(lock) do { barrier(); (void)(lock); } while (0) |
| 79 | # define arch_spin_lock_flags(lock, flags) do { barrier(); (void)(lock); } while (0) |
| 80 | # define arch_spin_unlock(lock) do { barrier(); (void)(lock); } while (0) |
| 81 | # define arch_spin_trylock(lock) ({ barrier(); (void)(lock); 1; }) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 82 | #endif /* DEBUG_SPINLOCK */ |
| 83 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 84 | #define arch_spin_is_contended(lock) (((void)(lock), 0)) |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 85 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 86 | #define arch_read_can_lock(lock) (((void)(lock), 1)) |
| 87 | #define arch_write_can_lock(lock) (((void)(lock), 1)) |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 88 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 89 | #endif /* __LINUX_SPINLOCK_UP_H */ |