atomic.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. *
  14. * Atomic primitives.
  15. */
  16. #ifndef _ASM_TILE_ATOMIC_H
  17. #define _ASM_TILE_ATOMIC_H
  18. #ifndef __ASSEMBLY__
  19. #include <linux/compiler.h>
  20. #include <asm/system.h>
  21. #define ATOMIC_INIT(i) { (i) }
  22. /**
  23. * atomic_read - read atomic variable
  24. * @v: pointer of type atomic_t
  25. *
  26. * Atomically reads the value of @v.
  27. */
  28. static inline int atomic_read(const atomic_t *v)
  29. {
  30. return ACCESS_ONCE(v->counter);
  31. }
  32. /**
  33. * atomic_sub_return - subtract integer and return
  34. * @v: pointer of type atomic_t
  35. * @i: integer value to subtract
  36. *
  37. * Atomically subtracts @i from @v and returns @v - @i
  38. */
  39. #define atomic_sub_return(i, v) atomic_add_return((int)(-(i)), (v))
  40. /**
  41. * atomic_sub - subtract integer from atomic variable
  42. * @i: integer value to subtract
  43. * @v: pointer of type atomic_t
  44. *
  45. * Atomically subtracts @i from @v.
  46. */
  47. #define atomic_sub(i, v) atomic_add((int)(-(i)), (v))
  48. /**
  49. * atomic_sub_and_test - subtract value from variable and test result
  50. * @i: integer value to subtract
  51. * @v: pointer of type atomic_t
  52. *
  53. * Atomically subtracts @i from @v and returns true if the result is
  54. * zero, or false for all other cases.
  55. */
  56. #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
  57. /**
  58. * atomic_inc_return - increment memory and return
  59. * @v: pointer of type atomic_t
  60. *
  61. * Atomically increments @v by 1 and returns the new value.
  62. */
  63. #define atomic_inc_return(v) atomic_add_return(1, (v))
  64. /**
  65. * atomic_dec_return - decrement memory and return
  66. * @v: pointer of type atomic_t
  67. *
  68. * Atomically decrements @v by 1 and returns the new value.
  69. */
  70. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  71. /**
  72. * atomic_inc - increment atomic variable
  73. * @v: pointer of type atomic_t
  74. *
  75. * Atomically increments @v by 1.
  76. */
  77. #define atomic_inc(v) atomic_add(1, (v))
  78. /**
  79. * atomic_dec - decrement atomic variable
  80. * @v: pointer of type atomic_t
  81. *
  82. * Atomically decrements @v by 1.
  83. */
  84. #define atomic_dec(v) atomic_sub(1, (v))
  85. /**
  86. * atomic_dec_and_test - decrement and test
  87. * @v: pointer of type atomic_t
  88. *
  89. * Atomically decrements @v by 1 and returns true if the result is 0.
  90. */
  91. #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
  92. /**
  93. * atomic_inc_and_test - increment and test
  94. * @v: pointer of type atomic_t
  95. *
  96. * Atomically increments @v by 1 and returns true if the result is 0.
  97. */
  98. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  99. /**
  100. * atomic_add_negative - add and test if negative
  101. * @v: pointer of type atomic_t
  102. * @i: integer value to add
  103. *
  104. * Atomically adds @i to @v and returns true if the result is
  105. * negative, or false when result is greater than or equal to zero.
  106. */
  107. #define atomic_add_negative(i, v) (atomic_add_return((i), (v)) < 0)
  108. /**
  109. * atomic_inc_not_zero - increment unless the number is zero
  110. * @v: pointer of type atomic_t
  111. *
  112. * Atomically increments @v by 1, so long as @v is non-zero.
  113. * Returns non-zero if @v was non-zero, and zero otherwise.
  114. */
  115. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  116. /*
  117. * We define xchg() and cmpxchg() in the included headers.
  118. * Note that we do not define __HAVE_ARCH_CMPXCHG, since that would imply
  119. * that cmpxchg() is an efficient operation, which is not particularly true.
  120. */
  121. /* Nonexistent functions intended to cause link errors. */
  122. extern unsigned long __xchg_called_with_bad_pointer(void);
  123. extern unsigned long __cmpxchg_called_with_bad_pointer(void);
  124. #define tas(ptr) (xchg((ptr), 1))
  125. #endif /* __ASSEMBLY__ */
  126. #ifndef __tilegx__
  127. #include <asm/atomic_32.h>
  128. #else
  129. #include <asm/atomic_64.h>
  130. #endif
  131. /* Provide the appropriate atomic_long_t definitions. */
  132. #ifndef __ASSEMBLY__
  133. #include <asm-generic/atomic-long.h>
  134. #endif
  135. #endif /* _ASM_TILE_ATOMIC_H */