non-atomic.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_GENERIC_BITOPS_NON_ATOMIC_H_
  3. #define _ASM_GENERIC_BITOPS_NON_ATOMIC_H_
  4. #include <linux/bits.h>
  5. /**
  6. * ___set_bit - Set a bit in memory
  7. * @nr: the bit to set
  8. * @addr: the address to start counting from
  9. *
  10. * Unlike set_bit(), this function is non-atomic and may be reordered.
  11. * If it's called on the same region of memory simultaneously, the effect
  12. * may be that only one operation succeeds.
  13. */
  14. static __always_inline void
  15. ___set_bit(unsigned long nr, volatile unsigned long *addr)
  16. {
  17. unsigned long mask = BIT_MASK(nr);
  18. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  19. *p |= mask;
  20. }
  21. static __always_inline void
  22. ___clear_bit(unsigned long nr, volatile unsigned long *addr)
  23. {
  24. unsigned long mask = BIT_MASK(nr);
  25. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  26. *p &= ~mask;
  27. }
  28. /**
  29. * ___change_bit - Toggle a bit in memory
  30. * @nr: the bit to change
  31. * @addr: the address to start counting from
  32. *
  33. * Unlike change_bit(), this function is non-atomic and may be reordered.
  34. * If it's called on the same region of memory simultaneously, the effect
  35. * may be that only one operation succeeds.
  36. */
  37. static __always_inline void
  38. ___change_bit(unsigned long nr, volatile unsigned long *addr)
  39. {
  40. unsigned long mask = BIT_MASK(nr);
  41. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  42. *p ^= mask;
  43. }
  44. /**
  45. * ___test_and_set_bit - Set a bit and return its old value
  46. * @nr: Bit to set
  47. * @addr: Address to count from
  48. *
  49. * This operation is non-atomic and can be reordered.
  50. * If two examples of this operation race, one can appear to succeed
  51. * but actually fail. You must protect multiple accesses with a lock.
  52. */
  53. static __always_inline bool
  54. ___test_and_set_bit(unsigned long nr, volatile unsigned long *addr)
  55. {
  56. unsigned long mask = BIT_MASK(nr);
  57. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  58. unsigned long old = *p;
  59. *p = old | mask;
  60. return (old & mask) != 0;
  61. }
  62. /**
  63. * ___test_and_clear_bit - Clear a bit and return its old value
  64. * @nr: Bit to clear
  65. * @addr: Address to count from
  66. *
  67. * This operation is non-atomic and can be reordered.
  68. * If two examples of this operation race, one can appear to succeed
  69. * but actually fail. You must protect multiple accesses with a lock.
  70. */
  71. static __always_inline bool
  72. ___test_and_clear_bit(unsigned long nr, volatile unsigned long *addr)
  73. {
  74. unsigned long mask = BIT_MASK(nr);
  75. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  76. unsigned long old = *p;
  77. *p = old & ~mask;
  78. return (old & mask) != 0;
  79. }
  80. /* WARNING: non atomic and it can be reordered! */
  81. static __always_inline bool
  82. ___test_and_change_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. *p = old ^ mask;
  88. return (old & mask) != 0;
  89. }
  90. /**
  91. * _test_bit - Determine whether a bit is set
  92. * @nr: bit number to test
  93. * @addr: Address to start counting from
  94. */
  95. static __always_inline bool
  96. _test_bit(unsigned long nr, const volatile unsigned long *addr)
  97. {
  98. return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
  99. }
  100. #endif /* _ASM_GENERIC_BITOPS_NON_ATOMIC_H_ */