atomic.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #ifndef __ARCH_BLACKFIN_ATOMIC__
  2. #define __ARCH_BLACKFIN_ATOMIC__
  3. #include <asm/system.h> /* local_irq_XXX() */
  4. /*
  5. * Atomic operations that C can't guarantee us. Useful for
  6. * resource counting etc..
  7. *
  8. * Generally we do not concern about SMP BFIN systems, so we don't have
  9. * to deal with that.
  10. *
  11. * Tony Kou (tonyko@lineo.ca) Lineo Inc. 2001
  12. */
  13. typedef struct {
  14. int counter;
  15. } atomic_t;
  16. #define ATOMIC_INIT(i) { (i) }
  17. #define atomic_read(v) ((v)->counter)
  18. #define atomic_set(v, i) (((v)->counter) = i)
  19. static __inline__ void atomic_add(int i, atomic_t * v)
  20. {
  21. long flags;
  22. local_irq_save(flags);
  23. v->counter += i;
  24. local_irq_restore(flags);
  25. }
  26. static __inline__ void atomic_sub(int i, atomic_t * v)
  27. {
  28. long flags;
  29. local_irq_save(flags);
  30. v->counter -= i;
  31. local_irq_restore(flags);
  32. }
  33. static inline int atomic_add_return(int i, atomic_t * v)
  34. {
  35. int __temp = 0;
  36. long flags;
  37. local_irq_save(flags);
  38. v->counter += i;
  39. __temp = v->counter;
  40. local_irq_restore(flags);
  41. return __temp;
  42. }
  43. #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
  44. static inline int atomic_sub_return(int i, atomic_t * v)
  45. {
  46. int __temp = 0;
  47. long flags;
  48. local_irq_save(flags);
  49. v->counter -= i;
  50. __temp = v->counter;
  51. local_irq_restore(flags);
  52. return __temp;
  53. }
  54. static __inline__ void atomic_inc(volatile atomic_t * v)
  55. {
  56. long flags;
  57. local_irq_save(flags);
  58. v->counter++;
  59. local_irq_restore(flags);
  60. }
  61. #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
  62. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  63. #define atomic_add_unless(v, a, u) \
  64. ({ \
  65. int c, old; \
  66. c = atomic_read(v); \
  67. while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
  68. c = old; \
  69. c != (u); \
  70. })
  71. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  72. static __inline__ void atomic_dec(volatile atomic_t * v)
  73. {
  74. long flags;
  75. local_irq_save(flags);
  76. v->counter--;
  77. local_irq_restore(flags);
  78. }
  79. static __inline__ void atomic_clear_mask(unsigned int mask, atomic_t * v)
  80. {
  81. long flags;
  82. local_irq_save(flags);
  83. v->counter &= ~mask;
  84. local_irq_restore(flags);
  85. }
  86. static __inline__ void atomic_set_mask(unsigned int mask, atomic_t * v)
  87. {
  88. long flags;
  89. local_irq_save(flags);
  90. v->counter |= mask;
  91. local_irq_restore(flags);
  92. }
  93. /* Atomic operations are already serializing */
  94. #define smp_mb__before_atomic_dec() barrier()
  95. #define smp_mb__after_atomic_dec() barrier()
  96. #define smp_mb__before_atomic_inc() barrier()
  97. #define smp_mb__after_atomic_inc() barrier()
  98. #define atomic_dec_return(v) atomic_sub_return(1,(v))
  99. #define atomic_inc_return(v) atomic_add_return(1,(v))
  100. /*
  101. * atomic_inc_and_test - increment and test
  102. * @v: pointer of type atomic_t
  103. *
  104. * Atomically increments @v by 1
  105. * and returns true if the result is zero, or false for all
  106. * other cases.
  107. */
  108. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  109. #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
  110. #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
  111. #include <asm-generic/atomic.h>
  112. #endif /* __ARCH_BLACKFIN_ATOMIC __ */