atomic.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #ifndef __ATOMIC_H
  2. #define __ATOMIC_H
  3. #include "asm-generic/atomic.h"
  4. typedef struct {
  5. volatile int counter;
  6. } atomic_t;
  7. #ifdef __i386__
  8. /**
  9. * atomic_read - read atomic variable
  10. * @v: pointer of type atomic_t
  11. *
  12. * Atomically reads the value of @v.
  13. */
  14. static inline int atomic_read(const atomic_t *v)
  15. {
  16. return v->counter;
  17. }
  18. /**
  19. * atomic_set - set atomic variable
  20. * @v: pointer of type atomic_t
  21. * @i: required value
  22. *
  23. * Atomically sets the value of @v to @i.
  24. */
  25. static inline void atomic_set(atomic_t *v, int i)
  26. {
  27. v->counter = i;
  28. }
  29. /**
  30. * atomic_inc - increment atomic variable
  31. * @v: pointer of type atomic_t
  32. *
  33. * Atomically increments @v by 1.
  34. */
  35. static inline void atomic_inc(atomic_t *v)
  36. {
  37. asm volatile("lock incl %0"
  38. : "+m" (v->counter));
  39. }
  40. /**
  41. * atomic_dec - decrement atomic variable
  42. * @v: pointer of type atomic_t
  43. *
  44. * Atomically decrements @v by 1.
  45. */
  46. static inline void atomic_dec(atomic_t *v)
  47. {
  48. asm volatile("lock decl %0"
  49. : "+m" (v->counter));
  50. }
  51. typedef struct {
  52. u64 __attribute__((aligned(8))) counter;
  53. } atomic64_t;
  54. #define ATOMIC64_INIT(val) { (val) }
  55. /**
  56. * atomic64_read - read atomic64 variable
  57. * @ptr: pointer to type atomic64_t
  58. *
  59. * Atomically reads the value of @ptr and returns it.
  60. */
  61. static inline u64 atomic64_read(atomic64_t *ptr)
  62. {
  63. u64 res;
  64. /*
  65. * Note, we inline this atomic64_t primitive because
  66. * it only clobbers EAX/EDX and leaves the others
  67. * untouched. We also (somewhat subtly) rely on the
  68. * fact that cmpxchg8b returns the current 64-bit value
  69. * of the memory location we are touching:
  70. */
  71. asm volatile("mov %%ebx, %%eax\n\t"
  72. "mov %%ecx, %%edx\n\t"
  73. "lock cmpxchg8b %1\n"
  74. : "=&A" (res)
  75. : "m" (*ptr)
  76. );
  77. return res;
  78. }
  79. u64 atomic64_cmpxchg(atomic64_t *v, u64 old, u64 new);
  80. #elif defined(__x86_64__)
  81. /**
  82. * atomic_read - read atomic variable
  83. * @v: pointer of type atomic_t
  84. *
  85. * Atomically reads the value of @v.
  86. */
  87. static inline int atomic_read(const atomic_t *v)
  88. {
  89. return v->counter;
  90. }
  91. /**
  92. * atomic_set - set atomic variable
  93. * @v: pointer of type atomic_t
  94. * @i: required value
  95. *
  96. * Atomically sets the value of @v to @i.
  97. */
  98. static inline void atomic_set(atomic_t *v, int i)
  99. {
  100. v->counter = i;
  101. }
  102. /**
  103. * atomic_inc - increment atomic variable
  104. * @v: pointer of type atomic_t
  105. *
  106. * Atomically increments @v by 1.
  107. */
  108. static inline void atomic_inc(atomic_t *v)
  109. {
  110. asm volatile("lock incl %0"
  111. : "=m" (v->counter)
  112. : "m" (v->counter));
  113. }
  114. /**
  115. * atomic_dec - decrement atomic variable
  116. * @v: pointer of type atomic_t
  117. *
  118. * Atomically decrements @v by 1.
  119. */
  120. static inline void atomic_dec(atomic_t *v)
  121. {
  122. asm volatile("lock decl %0"
  123. : "=m" (v->counter)
  124. : "m" (v->counter));
  125. }
  126. typedef struct {
  127. long long counter;
  128. } atomic64_t;
  129. #define ATOMIC64_INIT(i) { (i) }
  130. /**
  131. * atomic64_read - read atomic64 variable
  132. * @v: pointer of type atomic64_t
  133. *
  134. * Atomically reads the value of @v.
  135. * Doesn't imply a read memory barrier.
  136. */
  137. static inline long atomic64_read(const atomic64_t *v)
  138. {
  139. return v->counter;
  140. }
  141. u64 atomic64_cmpxchg(atomic64_t *v, u64 old, u64 new);
  142. #endif
  143. #endif