Path: blob/master/include/asm-generic/bitops/generic-non-atomic.h
26289 views
/* SPDX-License-Identifier: GPL-2.0-only */12#ifndef __ASM_GENERIC_BITOPS_GENERIC_NON_ATOMIC_H3#define __ASM_GENERIC_BITOPS_GENERIC_NON_ATOMIC_H45#include <linux/bits.h>6#include <asm/barrier.h>78#ifndef _LINUX_BITOPS_H9#error only <linux/bitops.h> can be included directly10#endif1112/*13* Generic definitions for bit operations, should not be used in regular code14* directly.15*/1617/**18* generic___set_bit - Set a bit in memory19* @nr: the bit to set20* @addr: the address to start counting from21*22* Unlike set_bit(), this function is non-atomic and may be reordered.23* If it's called on the same region of memory simultaneously, the effect24* may be that only one operation succeeds.25*/26static __always_inline void27generic___set_bit(unsigned long nr, volatile unsigned long *addr)28{29unsigned long mask = BIT_MASK(nr);30unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);3132*p |= mask;33}3435static __always_inline void36generic___clear_bit(unsigned long nr, volatile unsigned long *addr)37{38unsigned long mask = BIT_MASK(nr);39unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);4041*p &= ~mask;42}4344/**45* generic___change_bit - Toggle a bit in memory46* @nr: the bit to change47* @addr: the address to start counting from48*49* Unlike change_bit(), this function is non-atomic and may be reordered.50* If it's called on the same region of memory simultaneously, the effect51* may be that only one operation succeeds.52*/53static __always_inline void54generic___change_bit(unsigned long nr, volatile unsigned long *addr)55{56unsigned long mask = BIT_MASK(nr);57unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);5859*p ^= mask;60}6162/**63* generic___test_and_set_bit - Set a bit and return its old value64* @nr: Bit to set65* @addr: Address to count from66*67* This operation is non-atomic and can be reordered.68* If two examples of this operation race, one can appear to succeed69* but actually fail. You must protect multiple accesses with a lock.70*/71static __always_inline bool72generic___test_and_set_bit(unsigned long nr, volatile unsigned long *addr)73{74unsigned long mask = BIT_MASK(nr);75unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);76unsigned long old = *p;7778*p = old | mask;79return (old & mask) != 0;80}8182/**83* generic___test_and_clear_bit - Clear a bit and return its old value84* @nr: Bit to clear85* @addr: Address to count from86*87* This operation is non-atomic and can be reordered.88* If two examples of this operation race, one can appear to succeed89* but actually fail. You must protect multiple accesses with a lock.90*/91static __always_inline bool92generic___test_and_clear_bit(unsigned long nr, volatile unsigned long *addr)93{94unsigned long mask = BIT_MASK(nr);95unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);96unsigned long old = *p;9798*p = old & ~mask;99return (old & mask) != 0;100}101102/* WARNING: non atomic and it can be reordered! */103static __always_inline bool104generic___test_and_change_bit(unsigned long nr, volatile unsigned long *addr)105{106unsigned long mask = BIT_MASK(nr);107unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);108unsigned long old = *p;109110*p = old ^ mask;111return (old & mask) != 0;112}113114/**115* generic_test_bit - Determine whether a bit is set116* @nr: bit number to test117* @addr: Address to start counting from118*/119static __always_inline bool120generic_test_bit(unsigned long nr, const volatile unsigned long *addr)121{122/*123* Unlike the bitops with the '__' prefix above, this one *is* atomic,124* so `volatile` must always stay here with no cast-aways. See125* `Documentation/atomic_bitops.txt` for the details.126*/127return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));128}129130/**131* generic_test_bit_acquire - Determine, with acquire semantics, whether a bit is set132* @nr: bit number to test133* @addr: Address to start counting from134*/135static __always_inline bool136generic_test_bit_acquire(unsigned long nr, const volatile unsigned long *addr)137{138unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);139return 1UL & (smp_load_acquire(p) >> (nr & (BITS_PER_LONG-1)));140}141142/*143* const_*() definitions provide good compile-time optimizations when144* the passed arguments can be resolved at compile time.145*/146#define const___set_bit generic___set_bit147#define const___clear_bit generic___clear_bit148#define const___change_bit generic___change_bit149#define const___test_and_set_bit generic___test_and_set_bit150#define const___test_and_clear_bit generic___test_and_clear_bit151#define const___test_and_change_bit generic___test_and_change_bit152#define const_test_bit_acquire generic_test_bit_acquire153154/**155* const_test_bit - Determine whether a bit is set156* @nr: bit number to test157* @addr: Address to start counting from158*159* A version of generic_test_bit() which discards the `volatile` qualifier to160* allow a compiler to optimize code harder. Non-atomic and to be called only161* for testing compile-time constants, e.g. by the corresponding macros, not162* directly from "regular" code.163*/164static __always_inline bool165const_test_bit(unsigned long nr, const volatile unsigned long *addr)166{167const unsigned long *p = (const unsigned long *)addr + BIT_WORD(nr);168unsigned long mask = BIT_MASK(nr);169unsigned long val = *p;170171return !!(val & mask);172}173174#endif /* __ASM_GENERIC_BITOPS_GENERIC_NON_ATOMIC_H */175176177