non-atomic.h 3.0 KB

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