atomic.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include <atomic>
  5. #include "intrinsics.h"
  6. namespace embree
  7. {
  8. /* compiler memory barriers */
  9. #if defined(__INTEL_COMPILER)
  10. //#define __memory_barrier() __memory_barrier()
  11. #elif defined(__GNUC__) || defined(__clang__)
  12. # define __memory_barrier() asm volatile("" ::: "memory")
  13. #elif defined(_MSC_VER)
  14. # define __memory_barrier() _ReadWriteBarrier()
  15. #endif
  16. template <typename T>
  17. struct atomic : public std::atomic<T>
  18. {
  19. atomic () {}
  20. atomic (const T& a)
  21. : std::atomic<T>(a) {}
  22. atomic (const atomic<T>& a) {
  23. this->store(a.load());
  24. }
  25. atomic& operator=(const atomic<T>& other) {
  26. this->store(other.load());
  27. return *this;
  28. }
  29. };
  30. template<typename T>
  31. __forceinline void _atomic_min(std::atomic<T>& aref, const T& bref)
  32. {
  33. const T b = bref.load();
  34. while (true) {
  35. T a = aref.load();
  36. if (a <= b) break;
  37. if (aref.compare_exchange_strong(a,b)) break;
  38. }
  39. }
  40. template<typename T>
  41. __forceinline void _atomic_max(std::atomic<T>& aref, const T& bref)
  42. {
  43. const T b = bref.load();
  44. while (true) {
  45. T a = aref.load();
  46. if (a >= b) break;
  47. if (aref.compare_exchange_strong(a,b)) break;
  48. }
  49. }
  50. }