atomic.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/system.h>
  11. #define ATOMIC_INIT(i) ( (atomic_t) { (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_not_zero(v) atomic_add_unless((v), 1, 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 non-zero if @v was not @u, and zero otherwise.
  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 != (u);
  54. }
  55. #define smp_mb__before_atomic_dec() smp_mb()
  56. #define smp_mb__after_atomic_dec() smp_mb()
  57. #define smp_mb__before_atomic_inc() smp_mb()
  58. #define smp_mb__after_atomic_inc() smp_mb()
  59. #include <asm-generic/atomic-long.h>
  60. #include <asm-generic/atomic64.h>
  61. #endif /* __ASM_SH_ATOMIC_H */