atomic.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2006 Atmark Techno, Inc.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. */
  8. #ifndef _ASM_MICROBLAZE_ATOMIC_H
  9. #define _ASM_MICROBLAZE_ATOMIC_H
  10. #include <linux/types.h>
  11. #include <linux/compiler.h> /* likely */
  12. #include <asm/system.h> /* local_irq_XXX and friends */
  13. #define ATOMIC_INIT(i) { (i) }
  14. #define atomic_read(v) ((v)->counter)
  15. #define atomic_set(v, i) (((v)->counter) = (i))
  16. #define atomic_inc(v) (atomic_add_return(1, (v)))
  17. #define atomic_dec(v) (atomic_sub_return(1, (v)))
  18. #define atomic_add(i, v) (atomic_add_return(i, (v)))
  19. #define atomic_sub(i, v) (atomic_sub_return(i, (v)))
  20. #define atomic_inc_return(v) (atomic_add_return(1, (v)))
  21. #define atomic_dec_return(v) (atomic_sub_return(1, (v)))
  22. #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
  23. #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
  24. #define atomic_inc_not_zero(v) (atomic_add_unless((v), 1, 0))
  25. #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
  26. static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
  27. {
  28. int ret;
  29. unsigned long flags;
  30. local_irq_save(flags);
  31. ret = v->counter;
  32. if (likely(ret == old))
  33. v->counter = new;
  34. local_irq_restore(flags);
  35. return ret;
  36. }
  37. static inline int atomic_add_unless(atomic_t *v, int a, int u)
  38. {
  39. int c, old;
  40. c = atomic_read(v);
  41. while (c != u && (old = atomic_cmpxchg((v), c, c + a)) != c)
  42. c = old;
  43. return c != u;
  44. }
  45. static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
  46. {
  47. unsigned long flags;
  48. local_irq_save(flags);
  49. *addr &= ~mask;
  50. local_irq_restore(flags);
  51. }
  52. /**
  53. * atomic_add_return - add and return
  54. * @i: integer value to add
  55. * @v: pointer of type atomic_t
  56. *
  57. * Atomically adds @i to @v and returns @i + @v
  58. */
  59. static inline int atomic_add_return(int i, atomic_t *v)
  60. {
  61. unsigned long flags;
  62. int val;
  63. local_irq_save(flags);
  64. val = v->counter;
  65. v->counter = val += i;
  66. local_irq_restore(flags);
  67. return val;
  68. }
  69. static inline int atomic_sub_return(int i, atomic_t *v)
  70. {
  71. return atomic_add_return(-i, v);
  72. }
  73. /*
  74. * Atomically test *v and decrement if it is greater than 0.
  75. * The function returns the old value of *v minus 1.
  76. */
  77. static inline int atomic_dec_if_positive(atomic_t *v)
  78. {
  79. unsigned long flags;
  80. int res;
  81. local_irq_save(flags);
  82. res = v->counter - 1;
  83. if (res >= 0)
  84. v->counter = res;
  85. local_irq_restore(flags);
  86. return res;
  87. }
  88. #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
  89. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  90. /* Atomic operations are already serializing */
  91. #define smp_mb__before_atomic_dec() barrier()
  92. #define smp_mb__after_atomic_dec() barrier()
  93. #define smp_mb__before_atomic_inc() barrier()
  94. #define smp_mb__after_atomic_inc() barrier()
  95. #include <asm-generic/atomic.h>
  96. #endif /* _ASM_MICROBLAZE_ATOMIC_H */