atomic.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #define ATOMIC_INIT(i) { (i) }
  12. #define atomic_read(v) (*(volatile int *)&(v)->counter)
  13. #define atomic_set(v,i) ((v)->counter = (i))
  14. #if defined(CONFIG_GUSA_RB)
  15. #include <asm/atomic-grb.h>
  16. #elif defined(CONFIG_CPU_SH4A)
  17. #include <asm/atomic-llsc.h>
  18. #else
  19. #include <asm/atomic-irq.h>
  20. #endif
  21. #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
  22. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  23. #define atomic_inc_return(v) atomic_add_return(1, (v))
  24. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  25. #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
  26. #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
  27. #define atomic_inc(v) atomic_add(1, (v))
  28. #define atomic_dec(v) atomic_sub(1, (v))
  29. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  30. #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
  31. /**
  32. * __atomic_add_unless - add unless the number is a given value
  33. * @v: pointer of type atomic_t
  34. * @a: the amount to add to v...
  35. * @u: ...unless v is equal to u.
  36. *
  37. * Atomically adds @a to @v, so long as it was not @u.
  38. * Returns the old value of @v.
  39. */
  40. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  41. {
  42. int c, old;
  43. c = atomic_read(v);
  44. for (;;) {
  45. if (unlikely(c == (u)))
  46. break;
  47. old = atomic_cmpxchg((v), c, c + (a));
  48. if (likely(old == c))
  49. break;
  50. c = old;
  51. }
  52. return c;
  53. }
  54. #define smp_mb__before_atomic_dec() smp_mb()
  55. #define smp_mb__after_atomic_dec() smp_mb()
  56. #define smp_mb__before_atomic_inc() smp_mb()
  57. #define smp_mb__after_atomic_inc() smp_mb()
  58. #endif /* __ASM_SH_ATOMIC_H */