atomic.h 801 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __TOOLS_LINUX_ATOMIC_H
  3. #define __TOOLS_LINUX_ATOMIC_H
  4. #include <asm/atomic.h>
  5. void atomic_long_set(atomic_long_t *v, long i);
  6. /* atomic_cmpxchg_relaxed */
  7. #ifndef atomic_cmpxchg_relaxed
  8. #define atomic_cmpxchg_relaxed atomic_cmpxchg
  9. #define atomic_cmpxchg_release atomic_cmpxchg
  10. #endif /* atomic_cmpxchg_relaxed */
  11. static inline bool atomic_try_cmpxchg(atomic_t *ptr, int *oldp, int new)
  12. {
  13. int ret, old = *oldp;
  14. ret = atomic_cmpxchg(ptr, old, new);
  15. if (ret != old)
  16. *oldp = ret;
  17. return ret == old;
  18. }
  19. static inline bool atomic_inc_unless_negative(atomic_t *v)
  20. {
  21. int c = atomic_read(v);
  22. do {
  23. if (unlikely(c < 0))
  24. return false;
  25. } while (!atomic_try_cmpxchg(v, &c, c + 1));
  26. return true;
  27. }
  28. #endif /* __TOOLS_LINUX_ATOMIC_H */