blob: 3b5bafce433763a4e7c6bb0302fa373dec7364fb [file] [log] [blame]
Ingo Molnarfb1c8f92005-09-10 00:25:56 -07001#ifndef __LINUX_BIT_SPINLOCK_H
2#define __LINUX_BIT_SPINLOCK_H
3
Nick Piggin626d6072011-01-07 17:50:04 +11004#include <linux/kernel.h>
5#include <linux/preempt.h>
Arun Sharma600634972011-07-26 16:09:06 -07006#include <linux/atomic.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -05007#include <linux/bug.h>
Nick Piggin626d6072011-01-07 17:50:04 +11008
Ingo Molnarfb1c8f92005-09-10 00:25:56 -07009/*
10 * bit-based spin_lock()
11 *
12 * Don't use this unless you really need to: spin_lock() and spin_unlock()
13 * are significantly faster.
14 */
15static inline void bit_spin_lock(int bitnum, unsigned long *addr)
16{
17 /*
18 * Assuming the lock is uncontended, this never enters
19 * the body of the outer loop. If it is contended, then
20 * within the inner loop a non-atomic test is used to
21 * busywait with less bus contention for a good time to
22 * attempt to acquire the lock bit.
23 */
24 preempt_disable();
25#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
Nick Pigginb8dc93c2007-10-18 03:06:54 -070026 while (unlikely(test_and_set_bit_lock(bitnum, addr))) {
Linus Torvalds3dd2ee42011-04-25 18:10:58 -070027 preempt_enable();
28 do {
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070029 cpu_relax();
Linus Torvalds3dd2ee42011-04-25 18:10:58 -070030 } while (test_bit(bitnum, addr));
31 preempt_disable();
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070032 }
33#endif
34 __acquire(bitlock);
35}
36
37/*
38 * Return true if it was acquired
39 */
40static inline int bit_spin_trylock(int bitnum, unsigned long *addr)
41{
42 preempt_disable();
43#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
Nick Pigginb8dc93c2007-10-18 03:06:54 -070044 if (unlikely(test_and_set_bit_lock(bitnum, addr))) {
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070045 preempt_enable();
46 return 0;
47 }
48#endif
49 __acquire(bitlock);
50 return 1;
51}
52
53/*
54 * bit-based spin_unlock()
55 */
56static inline void bit_spin_unlock(int bitnum, unsigned long *addr)
57{
Nick Pigginb8dc93c2007-10-18 03:06:54 -070058#ifdef CONFIG_DEBUG_SPINLOCK
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070059 BUG_ON(!test_bit(bitnum, addr));
Nick Pigginb8dc93c2007-10-18 03:06:54 -070060#endif
61#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
62 clear_bit_unlock(bitnum, addr);
63#endif
64 preempt_enable();
65 __release(bitlock);
66}
67
68/*
69 * bit-based spin_unlock()
70 * non-atomic version, which can be used eg. if the bit lock itself is
71 * protecting the rest of the flags in the word.
72 */
73static inline void __bit_spin_unlock(int bitnum, unsigned long *addr)
74{
75#ifdef CONFIG_DEBUG_SPINLOCK
76 BUG_ON(!test_bit(bitnum, addr));
77#endif
78#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
79 __clear_bit_unlock(bitnum, addr);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070080#endif
81 preempt_enable();
82 __release(bitlock);
83}
84
85/*
86 * Return true if the lock is held.
87 */
88static inline int bit_spin_is_locked(int bitnum, unsigned long *addr)
89{
90#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
91 return test_bit(bitnum, addr);
Frederic Weisbeckerbdd4e852011-06-08 01:13:27 +020092#elif defined CONFIG_PREEMPT_COUNT
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070093 return preempt_count();
94#else
95 return 1;
96#endif
97}
98
99#endif /* __LINUX_BIT_SPINLOCK_H */
100