atomic.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #ifndef _TOOLS_LINUX_ASM_X86_ATOMIC_H
  2. #define _TOOLS_LINUX_ASM_X86_ATOMIC_H
  3. #include <linux/compiler.h>
  4. #include <linux/types.h>
  5. #include "rmwcc.h"
  6. #define LOCK_PREFIX "\n\tlock; "
  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_inc - increment atomic variable
  35. * @v: pointer of type atomic_t
  36. *
  37. * Atomically increments @v by 1.
  38. */
  39. static inline void atomic_inc(atomic_t *v)
  40. {
  41. asm volatile(LOCK_PREFIX "incl %0"
  42. : "+m" (v->counter));
  43. }
  44. /**
  45. * atomic_dec_and_test - decrement and test
  46. * @v: pointer of type atomic_t
  47. *
  48. * Atomically decrements @v by 1 and
  49. * returns true if the result is 0, or false for all other
  50. * cases.
  51. */
  52. static inline int atomic_dec_and_test(atomic_t *v)
  53. {
  54. GEN_UNARY_RMWcc(LOCK_PREFIX "decl", v->counter, "%0", "e");
  55. }
  56. #endif /* _TOOLS_LINUX_ASM_X86_ATOMIC_H */