1 #ifndef __LINUX_BIT_SPINLOCK_H
2 #define __LINUX_BIT_SPINLOCK_H
4 #include <linux/kernel.h>
5 #include <linux/preempt.h>
6 #include <linux/atomic.h>
10 * bit-based spin_lock()
12 * Don't use this unless you really need to: spin_lock() and spin_unlock()
13 * are significantly faster.
15 static inline void bit_spin_lock(int bitnum, unsigned long *addr)
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.
25 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
26 while (unlikely(test_and_set_bit_lock(bitnum, addr))) {
30 } while (test_bit(bitnum, addr));
38 * Return true if it was acquired
40 static inline int bit_spin_trylock(int bitnum, unsigned long *addr)
43 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
44 if (unlikely(test_and_set_bit_lock(bitnum, addr))) {
54 * bit-based spin_unlock()
56 static inline void bit_spin_unlock(int bitnum, unsigned long *addr)
58 #ifdef CONFIG_DEBUG_SPINLOCK
59 BUG_ON(!test_bit(bitnum, addr));
61 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
62 clear_bit_unlock(bitnum, addr);
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.
73 static inline void __bit_spin_unlock(int bitnum, unsigned long *addr)
75 #ifdef CONFIG_DEBUG_SPINLOCK
76 BUG_ON(!test_bit(bitnum, addr));
78 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
79 __clear_bit_unlock(bitnum, addr);
86 * Return true if the lock is held.
88 static inline int bit_spin_is_locked(int bitnum, unsigned long *addr)
90 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
91 return test_bit(bitnum, addr);
92 #elif defined CONFIG_PREEMPT_COUNT
93 return preempt_count();
99 #endif /* __LINUX_BIT_SPINLOCK_H */