brintos

brintos / linux-shallow public Read only

0
0
Text · 3.1 KiB · 0c472a8 Raw
116 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2#ifndef _ASM_GENERIC_BITOPS_NON_ATOMIC_H_3#define _ASM_GENERIC_BITOPS_NON_ATOMIC_H_4 5#include <linux/bits.h>6 7/**8 * ___set_bit - Set a bit in memory9 * @nr: the bit to set10 * @addr: the address to start counting from11 *12 * Unlike set_bit(), this function is non-atomic and may be reordered.13 * If it's called on the same region of memory simultaneously, the effect14 * may be that only one operation succeeds.15 */16static __always_inline void17___set_bit(unsigned long nr, volatile unsigned long *addr)18{19	unsigned long mask = BIT_MASK(nr);20	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);21 22	*p  |= mask;23}24 25static __always_inline void26___clear_bit(unsigned long nr, volatile unsigned long *addr)27{28	unsigned long mask = BIT_MASK(nr);29	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);30 31	*p &= ~mask;32}33 34/**35 * ___change_bit - Toggle a bit in memory36 * @nr: the bit to change37 * @addr: the address to start counting from38 *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 effect41 * may be that only one operation succeeds.42 */43static __always_inline void44___change_bit(unsigned long nr, volatile unsigned long *addr)45{46	unsigned long mask = BIT_MASK(nr);47	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);48 49	*p ^= mask;50}51 52/**53 * ___test_and_set_bit - Set a bit and return its old value54 * @nr: Bit to set55 * @addr: Address to count from56 *57 * This operation is non-atomic and can be reordered.58 * If two examples of this operation race, one can appear to succeed59 * but actually fail.  You must protect multiple accesses with a lock.60 */61static __always_inline bool62___test_and_set_bit(unsigned long nr, volatile unsigned long *addr)63{64	unsigned long mask = BIT_MASK(nr);65	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);66	unsigned long old = *p;67 68	*p = old | mask;69	return (old & mask) != 0;70}71 72/**73 * ___test_and_clear_bit - Clear a bit and return its old value74 * @nr: Bit to clear75 * @addr: Address to count from76 *77 * This operation is non-atomic and can be reordered.78 * If two examples of this operation race, one can appear to succeed79 * but actually fail.  You must protect multiple accesses with a lock.80 */81static __always_inline bool82___test_and_clear_bit(unsigned long nr, volatile unsigned long *addr)83{84	unsigned long mask = BIT_MASK(nr);85	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);86	unsigned long old = *p;87 88	*p = old & ~mask;89	return (old & mask) != 0;90}91 92/* WARNING: non atomic and it can be reordered! */93static __always_inline bool94___test_and_change_bit(unsigned long nr, volatile unsigned long *addr)95{96	unsigned long mask = BIT_MASK(nr);97	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);98	unsigned long old = *p;99 100	*p = old ^ mask;101	return (old & mask) != 0;102}103 104/**105 * _test_bit - Determine whether a bit is set106 * @nr: bit number to test107 * @addr: Address to start counting from108 */109static __always_inline bool110_test_bit(unsigned long nr, const volatile unsigned long *addr)111{112	return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));113}114 115#endif /* _ASM_GENERIC_BITOPS_NON_ATOMIC_H_ */116