non-atomic.h 3.0 KB

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