atomic.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/asm.h>
  9. #include <asm/cmpxchg.h>
  10. /*
  11. * Atomic operations that C can't guarantee us. Useful for
  12. * resource counting etc..
  13. */
  14. #define ATOMIC_INIT(i) { (i) }
  15. /**
  16. * atomic_read - read atomic variable
  17. * @v: pointer of type atomic_t
  18. *
  19. * Atomically reads the value of @v.
  20. */
  21. static inline int atomic_read(const atomic_t *v)
  22. {
  23. return READ_ONCE((v)->counter);
  24. }
  25. /**
  26. * atomic_set - set atomic variable
  27. * @v: pointer of type atomic_t
  28. * @i: required value
  29. *
  30. * Atomically sets the value of @v to @i.
  31. */
  32. static inline void atomic_set(atomic_t *v, int i)
  33. {
  34. v->counter = i;
  35. }
  36. /**
  37. * atomic_inc - increment atomic variable
  38. * @v: pointer of type atomic_t
  39. *
  40. * Atomically increments @v by 1.
  41. */
  42. static inline void atomic_inc(atomic_t *v)
  43. {
  44. asm volatile(LOCK_PREFIX "incl %0"
  45. : "+m" (v->counter));
  46. }
  47. /**
  48. * atomic_dec_and_test - decrement and test
  49. * @v: pointer of type atomic_t
  50. *
  51. * Atomically decrements @v by 1 and
  52. * returns true if the result is 0, or false for all other
  53. * cases.
  54. */
  55. static inline int atomic_dec_and_test(atomic_t *v)
  56. {
  57. GEN_UNARY_RMWcc(LOCK_PREFIX "decl", v->counter, "%0", "e");
  58. }
  59. static __always_inline int atomic_cmpxchg(atomic_t *v, int old, int new)
  60. {
  61. return cmpxchg(&v->counter, old, new);
  62. }
  63. static inline int test_and_set_bit(long nr, unsigned long *addr)
  64. {
  65. GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(bts), *addr, "Ir", nr, "%0", "c");
  66. }
  67. static inline int test_and_clear_bit(long nr, unsigned long *addr)
  68. {
  69. GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(btc), *addr, "Ir", nr, "%0", "c");
  70. }
  71. #endif /* _TOOLS_LINUX_ASM_X86_ATOMIC_H */