atomic.h 1.6 KB

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