non-atomic.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <asm/types.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 inline void __set_bit(int nr, volatile unsigned long *addr)
  15. {
  16. unsigned long mask = BIT_MASK(nr);
  17. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  18. *p |= mask;
  19. }
  20. static inline void __clear_bit(int nr, volatile unsigned long *addr)
  21. {
  22. unsigned long mask = BIT_MASK(nr);
  23. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  24. *p &= ~mask;
  25. }
  26. /**
  27. * __change_bit - Toggle a bit in memory
  28. * @nr: the bit to change
  29. * @addr: the address to start counting from
  30. *
  31. * Unlike change_bit(), this function is non-atomic and may be reordered.
  32. * If it's called on the same region of memory simultaneously, the effect
  33. * may be that only one operation succeeds.
  34. */
  35. static inline void __change_bit(int nr, volatile unsigned long *addr)
  36. {
  37. unsigned long mask = BIT_MASK(nr);
  38. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  39. *p ^= mask;
  40. }
  41. /**
  42. * __test_and_set_bit - Set a bit and return its old value
  43. * @nr: Bit to set
  44. * @addr: Address to count from
  45. *
  46. * This operation is non-atomic and can be reordered.
  47. * If two examples of this operation race, one can appear to succeed
  48. * but actually fail. You must protect multiple accesses with a lock.
  49. */
  50. static inline int __test_and_set_bit(int nr, volatile unsigned long *addr)
  51. {
  52. unsigned long mask = BIT_MASK(nr);
  53. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  54. unsigned long old = *p;
  55. *p = old | mask;
  56. return (old & mask) != 0;
  57. }
  58. /**
  59. * __test_and_clear_bit - Clear a bit and return its old value
  60. * @nr: Bit to clear
  61. * @addr: Address to count from
  62. *
  63. * This operation is non-atomic and can be reordered.
  64. * If two examples of this operation race, one can appear to succeed
  65. * but actually fail. You must protect multiple accesses with a lock.
  66. */
  67. static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr)
  68. {
  69. unsigned long mask = BIT_MASK(nr);
  70. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  71. unsigned long old = *p;
  72. *p = old & ~mask;
  73. return (old & mask) != 0;
  74. }
  75. /* WARNING: non atomic and it can be reordered! */
  76. static inline int __test_and_change_bit(int nr,
  77. volatile unsigned long *addr)
  78. {
  79. unsigned long mask = BIT_MASK(nr);
  80. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  81. unsigned long old = *p;
  82. *p = old ^ mask;
  83. return (old & mask) != 0;
  84. }
  85. /**
  86. * test_bit - Determine whether a bit is set
  87. * @nr: bit number to test
  88. * @addr: Address to start counting from
  89. */
  90. static inline int test_bit(int nr, const volatile unsigned long *addr)
  91. {
  92. return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
  93. }
  94. #endif /* _ASM_GENERIC_BITOPS_NON_ATOMIC_H_ */