atomic.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include <asm/cmpxchg.h>
  19. #ifndef __ASSEMBLY__
  20. #include <linux/compiler.h>
  21. #include <linux/types.h>
  22. #define ATOMIC_INIT(i) { (i) }
  23. /**
  24. * atomic_read - read atomic variable
  25. * @v: pointer of type atomic_t
  26. *
  27. * Atomically reads the value of @v.
  28. */
  29. static inline int atomic_read(const atomic_t *v)
  30. {
  31. return ACCESS_ONCE(v->counter);
  32. }
  33. /**
  34. * atomic_sub_return - subtract integer and return
  35. * @v: pointer of type atomic_t
  36. * @i: integer value to subtract
  37. *
  38. * Atomically subtracts @i from @v and returns @v - @i
  39. */
  40. #define atomic_sub_return(i, v) atomic_add_return((int)(-(i)), (v))
  41. /**
  42. * atomic_sub - subtract integer from atomic variable
  43. * @i: integer value to subtract
  44. * @v: pointer of type atomic_t
  45. *
  46. * Atomically subtracts @i from @v.
  47. */
  48. #define atomic_sub(i, v) atomic_add((int)(-(i)), (v))
  49. /**
  50. * atomic_sub_and_test - subtract value from variable and test result
  51. * @i: integer value to subtract
  52. * @v: pointer of type atomic_t
  53. *
  54. * Atomically subtracts @i from @v and returns true if the result is
  55. * zero, or false for all other cases.
  56. */
  57. #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
  58. /**
  59. * atomic_inc_return - increment memory and return
  60. * @v: pointer of type atomic_t
  61. *
  62. * Atomically increments @v by 1 and returns the new value.
  63. */
  64. #define atomic_inc_return(v) atomic_add_return(1, (v))
  65. /**
  66. * atomic_dec_return - decrement memory and return
  67. * @v: pointer of type atomic_t
  68. *
  69. * Atomically decrements @v by 1 and returns the new value.
  70. */
  71. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  72. /**
  73. * atomic_inc - increment atomic variable
  74. * @v: pointer of type atomic_t
  75. *
  76. * Atomically increments @v by 1.
  77. */
  78. #define atomic_inc(v) atomic_add(1, (v))
  79. /**
  80. * atomic_dec - decrement atomic variable
  81. * @v: pointer of type atomic_t
  82. *
  83. * Atomically decrements @v by 1.
  84. */
  85. #define atomic_dec(v) atomic_sub(1, (v))
  86. /**
  87. * atomic_dec_and_test - decrement and test
  88. * @v: pointer of type atomic_t
  89. *
  90. * Atomically decrements @v by 1 and returns true if the result is 0.
  91. */
  92. #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
  93. /**
  94. * atomic_inc_and_test - increment and test
  95. * @v: pointer of type atomic_t
  96. *
  97. * Atomically increments @v by 1 and returns true if the result is 0.
  98. */
  99. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  100. /**
  101. * atomic_add_negative - add and test if negative
  102. * @v: pointer of type atomic_t
  103. * @i: integer value to add
  104. *
  105. * Atomically adds @i to @v and returns true if the result is
  106. * negative, or false when result is greater than or equal to zero.
  107. */
  108. #define atomic_add_negative(i, v) (atomic_add_return((i), (v)) < 0)
  109. #endif /* __ASSEMBLY__ */
  110. #ifndef __tilegx__
  111. #include <asm/atomic_32.h>
  112. #else
  113. #include <asm/atomic_64.h>
  114. #endif
  115. #endif /* _ASM_TILE_ATOMIC_H */