atomic.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef _ASM_X86_ATOMIC_H
  2. #define _ASM_X86_ATOMIC_H
  3. #include <linux/compiler.h>
  4. #include <linux/types.h>
  5. #include <asm/processor.h>
  6. typedef struct { volatile int counter; } atomic_t;
  7. /*
  8. * Atomic operations that C can't guarantee us. Useful for
  9. * resource counting etc..
  10. */
  11. #define ATOMIC_INIT(i) { (i) }
  12. /**
  13. * atomic_read - read atomic variable
  14. * @v: pointer of type atomic_t
  15. *
  16. * Atomically reads the value of @v.
  17. */
  18. static inline int atomic_read(const atomic_t *v)
  19. {
  20. return ACCESS_ONCE((v)->counter);
  21. }
  22. /**
  23. * atomic_set - set atomic variable
  24. * @v: pointer of type atomic_t
  25. * @i: required value
  26. *
  27. * Atomically sets the value of @v to @i.
  28. */
  29. static inline void atomic_set(atomic_t *v, int i)
  30. {
  31. v->counter = i;
  32. }
  33. /**
  34. * atomic_add - add integer to atomic variable
  35. * @i: integer value to add
  36. * @v: pointer of type atomic_t
  37. *
  38. * Atomically adds @i to @v.
  39. */
  40. static inline void atomic_add(int i, atomic_t *v)
  41. {
  42. asm volatile(LOCK_PREFIX "addl %1,%0"
  43. : "+m" (v->counter)
  44. : "ir" (i));
  45. }
  46. /**
  47. * atomic_sub - subtract integer from atomic variable
  48. * @i: integer value to subtract
  49. * @v: pointer of type atomic_t
  50. *
  51. * Atomically subtracts @i from @v.
  52. */
  53. static inline void atomic_sub(int i, atomic_t *v)
  54. {
  55. asm volatile(LOCK_PREFIX "subl %1,%0"
  56. : "+m" (v->counter)
  57. : "ir" (i));
  58. }
  59. /**
  60. * atomic_inc - increment atomic variable
  61. * @v: pointer of type atomic_t
  62. *
  63. * Atomically increments @v by 1.
  64. */
  65. static inline void atomic_inc(atomic_t *v)
  66. {
  67. asm volatile(LOCK_PREFIX "incl %0"
  68. : "+m" (v->counter));
  69. }
  70. /**
  71. * atomic_dec - decrement atomic variable
  72. * @v: pointer of type atomic_t
  73. *
  74. * Atomically decrements @v by 1.
  75. */
  76. static inline void atomic_dec(atomic_t *v)
  77. {
  78. asm volatile(LOCK_PREFIX "decl %0"
  79. : "+m" (v->counter));
  80. }
  81. /**
  82. * atomic_inc_short - increment of a short integer
  83. * @v: pointer to type int
  84. *
  85. * Atomically adds 1 to @v
  86. * Returns the new value of @u
  87. */
  88. static inline short int atomic_inc_short(short int *v)
  89. {
  90. asm(LOCK_PREFIX "addw $1, %0" : "+m" (*v));
  91. return *v;
  92. }
  93. /* These are x86-specific, used by some header files */
  94. #define atomic_clear_mask(mask, addr) \
  95. asm volatile(LOCK_PREFIX "andl %0,%1" \
  96. : : "r" (~(mask)), "m" (*(addr)) : "memory")
  97. #define atomic_set_mask(mask, addr) \
  98. asm volatile(LOCK_PREFIX "orl %0,%1" \
  99. : : "r" ((unsigned)(mask)), "m" (*(addr)) \
  100. : "memory")
  101. #endif /* _ASM_X86_ATOMIC_H */