atomic.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef __ASM_SH_ATOMIC_H
  2. #define __ASM_SH_ATOMIC_H
  3. #if defined(CONFIG_CPU_J2)
  4. #include <asm-generic/atomic.h>
  5. #else
  6. /*
  7. * Atomic operations that C can't guarantee us. Useful for
  8. * resource counting etc..
  9. *
  10. */
  11. #include <linux/compiler.h>
  12. #include <linux/types.h>
  13. #include <asm/cmpxchg.h>
  14. #include <asm/barrier.h>
  15. #define ATOMIC_INIT(i) { (i) }
  16. #define atomic_read(v) READ_ONCE((v)->counter)
  17. #define atomic_set(v,i) WRITE_ONCE((v)->counter, (i))
  18. #if defined(CONFIG_GUSA_RB)
  19. #include <asm/atomic-grb.h>
  20. #elif defined(CONFIG_CPU_SH4A)
  21. #include <asm/atomic-llsc.h>
  22. #else
  23. #include <asm/atomic-irq.h>
  24. #endif
  25. #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
  26. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  27. #define atomic_inc_return(v) atomic_add_return(1, (v))
  28. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  29. #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
  30. #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
  31. #define atomic_inc(v) atomic_add(1, (v))
  32. #define atomic_dec(v) atomic_sub(1, (v))
  33. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  34. #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
  35. /**
  36. * __atomic_add_unless - add unless the number is a given value
  37. * @v: pointer of type atomic_t
  38. * @a: the amount to add to v...
  39. * @u: ...unless v is equal to u.
  40. *
  41. * Atomically adds @a to @v, so long as it was not @u.
  42. * Returns the old value of @v.
  43. */
  44. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  45. {
  46. int c, old;
  47. c = atomic_read(v);
  48. for (;;) {
  49. if (unlikely(c == (u)))
  50. break;
  51. old = atomic_cmpxchg((v), c, c + (a));
  52. if (likely(old == c))
  53. break;
  54. c = old;
  55. }
  56. return c;
  57. }
  58. #endif /* CONFIG_CPU_J2 */
  59. #endif /* __ASM_SH_ATOMIC_H */