1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_BIT_SPINLOCK_H
3 #define __LINUX_BIT_SPINLOCK_H
5 #include <linux/kernel.h>
6 #include <linux/preempt.h>
7 #include <linux/atomic.h>
11 * bit-based spin_lock()
13 * Don't use this unless you really need to: spin_lock() and spin_unlock()
14 * are significantly faster.
16 static inline void bit_spin_lock(int bitnum, unsigned long *addr)
19 * Assuming the lock is uncontended, this never enters
20 * the body of the outer loop. If it is contended, then
21 * within the inner loop a non-atomic test is used to
22 * busywait with less bus contention for a good time to
23 * attempt to acquire the lock bit.
26 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
27 while (unlikely(test_and_set_bit_lock(bitnum, addr))) {
31 } while (test_bit(bitnum, addr));
39 * Return true if it was acquired
41 static inline int bit_spin_trylock(int bitnum, unsigned long *addr)
44 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
45 if (unlikely(test_and_set_bit_lock(bitnum, addr))) {
55 * bit-based spin_unlock()
57 static inline void bit_spin_unlock(int bitnum, unsigned long *addr)
59 #ifdef CONFIG_DEBUG_SPINLOCK
60 BUG_ON(!test_bit(bitnum, addr));
62 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
63 clear_bit_unlock(bitnum, addr);
70 * bit-based spin_unlock()
71 * non-atomic version, which can be used eg. if the bit lock itself is
72 * protecting the rest of the flags in the word.
74 static inline void __bit_spin_unlock(int bitnum, unsigned long *addr)
76 #ifdef CONFIG_DEBUG_SPINLOCK
77 BUG_ON(!test_bit(bitnum, addr));
79 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
80 __clear_bit_unlock(bitnum, addr);
87 * Return true if the lock is held.
89 static inline int bit_spin_is_locked(int bitnum, unsigned long *addr)
91 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
92 return test_bit(bitnum, addr);
93 #elif defined CONFIG_PREEMPT_COUNT
94 return preempt_count();
100 #endif /* __LINUX_BIT_SPINLOCK_H */