non-atomic.h 3.1 KB

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