atomic-gcc.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #ifndef __TOOLS_ASM_GENERIC_ATOMIC_H
  2. #define __TOOLS_ASM_GENERIC_ATOMIC_H
  3. #include <linux/compiler.h>
  4. #include <linux/types.h>
  5. /*
  6. * Atomic operations that C can't guarantee us. Useful for
  7. * resource counting etc..
  8. *
  9. * Excerpts obtained from the Linux kernel sources.
  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. __sync_add_and_fetch(&v->counter, 1);
  42. }
  43. /**
  44. * atomic_dec_and_test - decrement and test
  45. * @v: pointer of type atomic_t
  46. *
  47. * Atomically decrements @v by 1 and
  48. * returns true if the result is 0, or false for all other
  49. * cases.
  50. */
  51. static inline int atomic_dec_and_test(atomic_t *v)
  52. {
  53. return __sync_sub_and_fetch(&v->counter, 1) == 0;
  54. }
  55. #endif /* __TOOLS_ASM_GENERIC_ATOMIC_H */