1 #ifndef _ASM_GENERIC_BITOPS_NON_ATOMIC_H_
2 #define _ASM_GENERIC_BITOPS_NON_ATOMIC_H_
4 #include <linux/types.h>
6 #define BITOP_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
7 #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
10 * __set_bit - Set a bit in memory
12 * @addr: the address to start counting from
14 * Unlike set_bit(), this function is non-atomic and may be reordered.
15 * If it's called on the same region of memory simultaneously, the effect
16 * may be that only one operation succeeds.
18 static inline void __set_bit(int nr, volatile unsigned long *addr)
20 unsigned long mask = BITOP_MASK(nr);
21 unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
26 static inline void __clear_bit(int nr, volatile unsigned long *addr)
28 unsigned long mask = BITOP_MASK(nr);
29 unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
35 * __change_bit - Toggle a bit in memory
36 * @nr: the bit to change
37 * @addr: the address to start counting from
39 * Unlike change_bit(), this function is non-atomic and may be reordered.
40 * If it's called on the same region of memory simultaneously, the effect
41 * may be that only one operation succeeds.
43 static inline void __change_bit(int nr, volatile unsigned long *addr)
45 unsigned long mask = BITOP_MASK(nr);
46 unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
52 * __test_and_set_bit - Set a bit and return its old value
54 * @addr: Address to count from
56 * This operation is non-atomic and can be reordered.
57 * If two examples of this operation race, one can appear to succeed
58 * but actually fail. You must protect multiple accesses with a lock.
60 static inline int __test_and_set_bit(int nr, volatile unsigned long *addr)
62 unsigned long mask = BITOP_MASK(nr);
63 unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
64 unsigned long old = *p;
67 return (old & mask) != 0;
71 * __test_and_clear_bit - Clear a bit and return its old value
73 * @addr: Address to count from
75 * This operation is non-atomic and can be reordered.
76 * If two examples of this operation race, one can appear to succeed
77 * but actually fail. You must protect multiple accesses with a lock.
79 static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr)
81 unsigned long mask = BITOP_MASK(nr);
82 unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
83 unsigned long old = *p;
86 return (old & mask) != 0;
89 /* WARNING: non atomic and it can be reordered! */
90 static inline int __test_and_change_bit(int nr,
91 volatile unsigned long *addr)
93 unsigned long mask = BITOP_MASK(nr);
94 unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
95 unsigned long old = *p;
98 return (old & mask) != 0;
102 * test_bit - Determine whether a bit is set
103 * @nr: bit number to test
104 * @addr: Address to start counting from
106 static inline int test_bit(int nr, const volatile unsigned long *addr)
108 return 1UL & (addr[BITOP_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
112 * __ffs - find first bit in word.
113 * @word: The word to search
115 * Undefined if no bit exists, so code should check against 0 first.
117 static inline unsigned long __ffs(unsigned long word)
121 if ((word & 0xffffffff) == 0) {
125 if ((word & 0xffff) == 0) {
129 if ((word & 0xff) == 0) {
133 if ((word & 0xf) == 0) {
137 if ((word & 0x3) == 0) {
141 if ((word & 0x1) == 0)
146 unsigned long find_next_bit(const unsigned long *addr,
148 unsigned long offset);
150 #endif /* _ASM_GENERIC_BITOPS_NON_ATOMIC_H_ */