atomic.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _TOOLS_LINUX_ASM_X86_ATOMIC_H
  3. #define _TOOLS_LINUX_ASM_X86_ATOMIC_H
  4. #include <linux/compiler.h>
  5. #include <linux/types.h>
  6. #include "rmwcc.h"
  7. #define LOCK_PREFIX "\n\tlock; "
  8. #include <asm/cmpxchg.h>
  9. /*
  10. * Atomic operations that C can't guarantee us. Useful for
  11. * resource counting etc..
  12. */
  13. #define ATOMIC_INIT(i) { (i) }
  14. /**
  15. * atomic_read - read atomic variable
  16. * @v: pointer of type atomic_t
  17. *
  18. * Atomically reads the value of @v.
  19. */
  20. static inline int atomic_read(const atomic_t *v)
  21. {
  22. return READ_ONCE((v)->counter);
  23. }
  24. /**
  25. * atomic_set - set atomic variable
  26. * @v: pointer of type atomic_t
  27. * @i: required value
  28. *
  29. * Atomically sets the value of @v to @i.
  30. */
  31. static inline void atomic_set(atomic_t *v, int i)
  32. {
  33. v->counter = i;
  34. }
  35. /**
  36. * atomic_inc - increment atomic variable
  37. * @v: pointer of type atomic_t
  38. *
  39. * Atomically increments @v by 1.
  40. */
  41. static inline void atomic_inc(atomic_t *v)
  42. {
  43. asm volatile(LOCK_PREFIX "incl %0"
  44. : "+m" (v->counter));
  45. }
  46. /**
  47. * atomic_dec_and_test - decrement and test
  48. * @v: pointer of type atomic_t
  49. *
  50. * Atomically decrements @v by 1 and
  51. * returns true if the result is 0, or false for all other
  52. * cases.
  53. */
  54. static inline int atomic_dec_and_test(atomic_t *v)
  55. {
  56. GEN_UNARY_RMWcc(LOCK_PREFIX "decl", v->counter, "%0", "e");
  57. }
  58. static __always_inline int atomic_cmpxchg(atomic_t *v, int old, int new)
  59. {
  60. return cmpxchg(&v->counter, old, new);
  61. }
  62. #endif /* _TOOLS_LINUX_ASM_X86_ATOMIC_H */