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/futex.h>
7 #include <urcu/futex.h>
10 * The futex wait op wants an explicit 32-bit address and value. If the bitmap
11 * used for the spinlock is 64-bit, cast down and pass the right 32-bit region
12 * for the in-kernel checks. The value is the copy that has already been read
15 * The futex wake op interprets the value as the number of waiters to wake (up
16 * to INT_MAX), so pass that along directly.
18 static inline void do_futex(int nr, unsigned long *addr, unsigned long v, int futex_flags)
20 u32 *addr32 = (u32 *) addr;
21 u32 *v32 = (u32 *) &v;
24 futex_flags |= FUTEX_PRIVATE_FLAG;
26 #if BITS_PER_LONG == 64
27 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
28 shift = (nr >= 32) ? 1 : 0;
30 shift = (nr < 32) ? 1 : 0;
38 * The shift to determine the futex address may have cast away a
39 * literal wake count value. The value is capped to INT_MAX and thus
40 * always in the low bytes of v regardless of bit nr. Copy in the wake
41 * count to whatever 32-bit range was selected.
43 if (futex_flags == FUTEX_WAKE_PRIVATE)
45 futex(addr32, futex_flags, *v32, NULL, NULL, 0);
48 static inline void bit_spin_lock(int nr, unsigned long *_addr)
51 unsigned long *addr = _addr + (nr / BITS_PER_LONG);
54 nr &= BITS_PER_LONG - 1;
58 v = __atomic_fetch_or(addr, mask, __ATOMIC_ACQUIRE);
62 do_futex(nr, addr, v, FUTEX_WAIT);
66 static inline void bit_spin_wake(int nr, unsigned long *_addr)
68 do_futex(nr, _addr, INT_MAX, FUTEX_WAKE);
71 static inline void bit_spin_unlock(int nr, unsigned long *_addr)
74 unsigned long *addr = _addr + (nr / BITS_PER_LONG);
76 nr &= BITS_PER_LONG - 1;
79 __atomic_and_fetch(addr, ~mask, __ATOMIC_RELEASE);
80 do_futex(nr, addr, INT_MAX, FUTEX_WAKE);
83 #endif /* __LINUX_BIT_SPINLOCK_H */