ConditionVariableData_FutexBased.inl.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #pragma once
  2. #include "../Atomic.h"
  3. namespace baselib
  4. {
  5. BASELIB_CPP_INTERFACE
  6. {
  7. namespace detail
  8. {
  9. struct ConditionVariableData
  10. {
  11. atomic<int32_t> waiters;
  12. atomic<int32_t> wakeups;
  13. ConditionVariableData() : waiters(0), wakeups(0) {}
  14. inline bool HasWaiters() const
  15. {
  16. return waiters.load(memory_order_acquire) > 0;
  17. }
  18. inline bool TryConsumeWakeup()
  19. {
  20. int32_t previousCount = wakeups.load(memory_order_relaxed);
  21. while (previousCount > 0)
  22. {
  23. if (wakeups.compare_exchange_weak(previousCount, previousCount - 1, memory_order_acquire, memory_order_relaxed))
  24. {
  25. return true;
  26. }
  27. }
  28. return false;
  29. }
  30. };
  31. }
  32. }
  33. }