atomic.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Atomic operations that C can't guarantee us. Useful for
  3. * resource counting etc.
  4. *
  5. * But use these as seldom as possible since they are slower than
  6. * regular operations.
  7. *
  8. * Copyright (C) 2004-2006 Atmel Corporation
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #ifndef __ASM_AVR32_ATOMIC_H
  15. #define __ASM_AVR32_ATOMIC_H
  16. #include <asm/system.h>
  17. typedef struct { volatile int counter; } atomic_t;
  18. #define ATOMIC_INIT(i) { (i) }
  19. #define atomic_read(v) ((v)->counter)
  20. #define atomic_set(v, i) (((v)->counter) = i)
  21. /*
  22. * atomic_sub_return - subtract the atomic variable
  23. * @i: integer value to subtract
  24. * @v: pointer of type atomic_t
  25. *
  26. * Atomically subtracts @i from @v. Returns the resulting value.
  27. */
  28. static inline int atomic_sub_return(int i, atomic_t *v)
  29. {
  30. int result;
  31. asm volatile(
  32. "/* atomic_sub_return */\n"
  33. "1: ssrf 5\n"
  34. " ld.w %0, %2\n"
  35. " sub %0, %3\n"
  36. " stcond %1, %0\n"
  37. " brne 1b"
  38. : "=&r"(result), "=o"(v->counter)
  39. : "m"(v->counter), "ir"(i)
  40. : "cc");
  41. return result;
  42. }
  43. /*
  44. * atomic_add_return - add integer to atomic variable
  45. * @i: integer value to add
  46. * @v: pointer of type atomic_t
  47. *
  48. * Atomically adds @i to @v. Returns the resulting value.
  49. */
  50. static inline int atomic_add_return(int i, atomic_t *v)
  51. {
  52. int result;
  53. if (__builtin_constant_p(i))
  54. result = atomic_sub_return(-i, v);
  55. else
  56. asm volatile(
  57. "/* atomic_add_return */\n"
  58. "1: ssrf 5\n"
  59. " ld.w %0, %1\n"
  60. " add %0, %3\n"
  61. " stcond %2, %0\n"
  62. " brne 1b"
  63. : "=&r"(result), "=o"(v->counter)
  64. : "m"(v->counter), "r"(i)
  65. : "cc", "memory");
  66. return result;
  67. }
  68. /*
  69. * atomic_sub_unless - sub unless the number is a given value
  70. * @v: pointer of type atomic_t
  71. * @a: the amount to add to v...
  72. * @u: ...unless v is equal to u.
  73. *
  74. * If the atomic value v is not equal to u, this function subtracts a
  75. * from v, and returns non zero. If v is equal to u then it returns
  76. * zero. This is done as an atomic operation.
  77. */
  78. static inline int atomic_sub_unless(atomic_t *v, int a, int u)
  79. {
  80. int tmp, result = 0;
  81. asm volatile(
  82. "/* atomic_sub_unless */\n"
  83. "1: ssrf 5\n"
  84. " ld.w %0, %3\n"
  85. " cp.w %0, %5\n"
  86. " breq 1f\n"
  87. " sub %0, %4\n"
  88. " stcond %2, %0\n"
  89. " brne 1b\n"
  90. " mov %1, 1\n"
  91. "1:"
  92. : "=&r"(tmp), "=&r"(result), "=o"(v->counter)
  93. : "m"(v->counter), "ir"(a), "ir"(u)
  94. : "cc", "memory");
  95. return result;
  96. }
  97. /*
  98. * atomic_add_unless - add unless the number is a given value
  99. * @v: pointer of type atomic_t
  100. * @a: the amount to add to v...
  101. * @u: ...unless v is equal to u.
  102. *
  103. * If the atomic value v is not equal to u, this function adds a to v,
  104. * and returns non zero. If v is equal to u then it returns zero. This
  105. * is done as an atomic operation.
  106. */
  107. static inline int atomic_add_unless(atomic_t *v, int a, int u)
  108. {
  109. int tmp, result;
  110. if (__builtin_constant_p(a))
  111. result = atomic_sub_unless(v, -a, u);
  112. else {
  113. result = 0;
  114. asm volatile(
  115. "/* atomic_add_unless */\n"
  116. "1: ssrf 5\n"
  117. " ld.w %0, %3\n"
  118. " cp.w %0, %5\n"
  119. " breq 1f\n"
  120. " add %0, %4\n"
  121. " stcond %2, %0\n"
  122. " brne 1b\n"
  123. " mov %1, 1\n"
  124. "1:"
  125. : "=&r"(tmp), "=&r"(result), "=o"(v->counter)
  126. : "m"(v->counter), "r"(a), "ir"(u)
  127. : "cc", "memory");
  128. }
  129. return result;
  130. }
  131. /*
  132. * atomic_sub_if_positive - conditionally subtract integer from atomic variable
  133. * @i: integer value to subtract
  134. * @v: pointer of type atomic_t
  135. *
  136. * Atomically test @v and subtract @i if @v is greater or equal than @i.
  137. * The function returns the old value of @v minus @i.
  138. */
  139. static inline int atomic_sub_if_positive(int i, atomic_t *v)
  140. {
  141. int result;
  142. asm volatile(
  143. "/* atomic_sub_if_positive */\n"
  144. "1: ssrf 5\n"
  145. " ld.w %0, %2\n"
  146. " sub %0, %3\n"
  147. " brlt 1f\n"
  148. " stcond %1, %0\n"
  149. " brne 1b\n"
  150. "1:"
  151. : "=&r"(result), "=o"(v->counter)
  152. : "m"(v->counter), "ir"(i)
  153. : "cc", "memory");
  154. return result;
  155. }
  156. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  157. #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
  158. #define atomic_sub(i, v) (void)atomic_sub_return(i, v)
  159. #define atomic_add(i, v) (void)atomic_add_return(i, v)
  160. #define atomic_dec(v) atomic_sub(1, (v))
  161. #define atomic_inc(v) atomic_add(1, (v))
  162. #define atomic_dec_return(v) atomic_sub_return(1, v)
  163. #define atomic_inc_return(v) atomic_add_return(1, v)
  164. #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
  165. #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
  166. #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
  167. #define atomic_add_negative(i, v) (atomic_add_return(i, v) < 0)
  168. #define atomic_inc_not_zero(v) atomic_add_unless(v, 1, 0)
  169. #define atomic_dec_if_positive(v) atomic_sub_if_positive(1, v)
  170. #define smp_mb__before_atomic_dec() barrier()
  171. #define smp_mb__after_atomic_dec() barrier()
  172. #define smp_mb__before_atomic_inc() barrier()
  173. #define smp_mb__after_atomic_inc() barrier()
  174. #include <asm-generic/atomic.h>
  175. #endif /* __ASM_AVR32_ATOMIC_H */