local.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef _ALPHA_LOCAL_H
  2. #define _ALPHA_LOCAL_H
  3. #include <linux/percpu.h>
  4. #include <linux/atomic.h>
  5. typedef struct
  6. {
  7. atomic_long_t a;
  8. } local_t;
  9. #define LOCAL_INIT(i) { ATOMIC_LONG_INIT(i) }
  10. #define local_read(l) atomic_long_read(&(l)->a)
  11. #define local_set(l,i) atomic_long_set(&(l)->a, (i))
  12. #define local_inc(l) atomic_long_inc(&(l)->a)
  13. #define local_dec(l) atomic_long_dec(&(l)->a)
  14. #define local_add(i,l) atomic_long_add((i),(&(l)->a))
  15. #define local_sub(i,l) atomic_long_sub((i),(&(l)->a))
  16. static __inline__ long local_add_return(long i, local_t * l)
  17. {
  18. long temp, result;
  19. __asm__ __volatile__(
  20. "1: ldq_l %0,%1\n"
  21. " addq %0,%3,%2\n"
  22. " addq %0,%3,%0\n"
  23. " stq_c %0,%1\n"
  24. " beq %0,2f\n"
  25. ".subsection 2\n"
  26. "2: br 1b\n"
  27. ".previous"
  28. :"=&r" (temp), "=m" (l->a.counter), "=&r" (result)
  29. :"Ir" (i), "m" (l->a.counter) : "memory");
  30. return result;
  31. }
  32. static __inline__ long local_sub_return(long i, local_t * l)
  33. {
  34. long temp, result;
  35. __asm__ __volatile__(
  36. "1: ldq_l %0,%1\n"
  37. " subq %0,%3,%2\n"
  38. " subq %0,%3,%0\n"
  39. " stq_c %0,%1\n"
  40. " beq %0,2f\n"
  41. ".subsection 2\n"
  42. "2: br 1b\n"
  43. ".previous"
  44. :"=&r" (temp), "=m" (l->a.counter), "=&r" (result)
  45. :"Ir" (i), "m" (l->a.counter) : "memory");
  46. return result;
  47. }
  48. #define local_cmpxchg(l, o, n) \
  49. (cmpxchg_local(&((l)->a.counter), (o), (n)))
  50. #define local_xchg(l, n) (xchg_local(&((l)->a.counter), (n)))
  51. /**
  52. * local_add_unless - add unless the number is a given value
  53. * @l: pointer of type local_t
  54. * @a: the amount to add to l...
  55. * @u: ...unless l is equal to u.
  56. *
  57. * Atomically adds @a to @l, so long as it was not @u.
  58. * Returns non-zero if @l was not @u, and zero otherwise.
  59. */
  60. #define local_add_unless(l, a, u) \
  61. ({ \
  62. long c, old; \
  63. c = local_read(l); \
  64. for (;;) { \
  65. if (unlikely(c == (u))) \
  66. break; \
  67. old = local_cmpxchg((l), c, c + (a)); \
  68. if (likely(old == c)) \
  69. break; \
  70. c = old; \
  71. } \
  72. c != (u); \
  73. })
  74. #define local_inc_not_zero(l) local_add_unless((l), 1, 0)
  75. #define local_add_negative(a, l) (local_add_return((a), (l)) < 0)
  76. #define local_dec_return(l) local_sub_return(1,(l))
  77. #define local_inc_return(l) local_add_return(1,(l))
  78. #define local_sub_and_test(i,l) (local_sub_return((i), (l)) == 0)
  79. #define local_inc_and_test(l) (local_add_return(1, (l)) == 0)
  80. #define local_dec_and_test(l) (local_sub_return(1, (l)) == 0)
  81. /* Verify if faster than atomic ops */
  82. #define __local_inc(l) ((l)->a.counter++)
  83. #define __local_dec(l) ((l)->a.counter++)
  84. #define __local_add(i,l) ((l)->a.counter+=(i))
  85. #define __local_sub(i,l) ((l)->a.counter-=(i))
  86. #endif /* _ALPHA_LOCAL_H */