atomic.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /******************************************************************************
  3. *
  4. * Copyright © International Business Machines Corp., 2009
  5. *
  6. * DESCRIPTION
  7. * GCC atomic builtin wrappers
  8. * http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html
  9. *
  10. * AUTHOR
  11. * Darren Hart <[email protected]>
  12. *
  13. * HISTORY
  14. * 2009-Nov-17: Initial version by Darren Hart <[email protected]>
  15. *
  16. *****************************************************************************/
  17. #ifndef _ATOMIC_H
  18. #define _ATOMIC_H
  19. typedef struct {
  20. volatile int val;
  21. } atomic_t;
  22. #define ATOMIC_INITIALIZER { 0 }
  23. /**
  24. * atomic_cmpxchg() - Atomic compare and exchange
  25. * @uaddr: The address of the futex to be modified
  26. * @oldval: The expected value of the futex
  27. * @newval: The new value to try and assign the futex
  28. *
  29. * Return the old value of addr->val.
  30. */
  31. static inline int
  32. atomic_cmpxchg(atomic_t *addr, int oldval, int newval)
  33. {
  34. return __sync_val_compare_and_swap(&addr->val, oldval, newval);
  35. }
  36. /**
  37. * atomic_inc() - Atomic incrememnt
  38. * @addr: Address of the variable to increment
  39. *
  40. * Return the new value of addr->val.
  41. */
  42. static inline int
  43. atomic_inc(atomic_t *addr)
  44. {
  45. return __sync_add_and_fetch(&addr->val, 1);
  46. }
  47. /**
  48. * atomic_dec() - Atomic decrement
  49. * @addr: Address of the variable to decrement
  50. *
  51. * Return the new value of addr-val.
  52. */
  53. static inline int
  54. atomic_dec(atomic_t *addr)
  55. {
  56. return __sync_sub_and_fetch(&addr->val, 1);
  57. }
  58. /**
  59. * atomic_set() - Atomic set
  60. * @addr: Address of the variable to set
  61. * @newval: New value for the atomic_t
  62. *
  63. * Return the new value of addr->val.
  64. */
  65. static inline int
  66. atomic_set(atomic_t *addr, int newval)
  67. {
  68. addr->val = newval;
  69. return newval;
  70. }
  71. #endif