atomic.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* Copyright (C) 2017 Free Software Foundation, Inc.
  2. Contributed by Agustina Arzille <avarzille@riseup.net>, 2017.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either
  6. version 2 of the license, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public
  12. License along with this program; if not, see
  13. <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef _KERN_ATOMIC_H_
  16. #define _KERN_ATOMIC_H_ 1
  17. /* Atomically compare *PTR with EXP and set it to NVAL if they're equal.
  18. * Evaluates to a boolean, indicating whether the comparison was successful.*/
  19. #define __atomic_cas_helper(ptr, exp, nval, mo) \
  20. ({ \
  21. typeof(exp) __e = (exp); \
  22. __atomic_compare_exchange_n ((ptr), &__e, (nval), 0, \
  23. __ATOMIC_##mo, __ATOMIC_RELAXED); \
  24. })
  25. #define atomic_cas_acq(ptr, exp, nval) \
  26. __atomic_cas_helper (ptr, exp, nval, ACQUIRE)
  27. #define atomic_cas_rel(ptr, exp, nval) \
  28. __atomic_cas_helper (ptr, exp, nval, RELEASE)
  29. #define atomic_cas_seq(ptr, exp, nval) \
  30. __atomic_cas_helper (ptr, exp, nval, SEQ_CST)
  31. /* Atomically exchange the value of *PTR with VAL, evaluating to
  32. * its previous value. */
  33. #define __atomic_swap_helper(ptr, val, mo) \
  34. __atomic_exchange_n ((ptr), (val), __ATOMIC_##mo)
  35. #define atomic_swap_acq(ptr, val) \
  36. __atomic_swap_helper (ptr, val, ACQUIRE)
  37. #define atomic_swap_rel(ptr, val) \
  38. __atomic_swap_helper (ptr, val, RELEASE)
  39. #define atomic_swap_seq(ptr, val) \
  40. __atomic_swap_helper (ptr, val, SEQ_CST)
  41. #endif