Atomics.h 796 B

1234567891011121314151617181920212223242526272829303132
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <atomic>
  5. namespace JPH {
  6. /// Atomically compute the min(ioAtomic, inValue) and store it in ioAtomic, returns true if value was updated
  7. template <class T>
  8. bool AtomicMin(atomic<T> &ioAtomic, const T inValue)
  9. {
  10. T cur_value = ioAtomic;
  11. while (cur_value > inValue)
  12. if (ioAtomic.compare_exchange_weak(cur_value, inValue))
  13. return true;
  14. return false;
  15. }
  16. /// Atomically compute the max(ioAtomic, inValue) and store it in ioAtomic, returns true if value was updated
  17. template <class T>
  18. bool AtomicMax(atomic<T> &ioAtomic, const T inValue)
  19. {
  20. T cur_value = ioAtomic;
  21. while (cur_value < inValue)
  22. if (ioAtomic.compare_exchange_weak(cur_value, inValue))
  23. return true;
  24. return false;
  25. }
  26. } // JPH