From 60fe26ff2f49c72dfee87d96fb35f68eaeb02a6f Mon Sep 17 00:00:00 2001 From: Gregory Hainaut Date: Mon, 22 Feb 2016 19:55:39 +0100 Subject: [PATCH] common:threading: Port NonblockingMutex to std::atomic_flag --- common/include/Utilities/Threading.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/common/include/Utilities/Threading.h b/common/include/Utilities/Threading.h index be0244db50..b50fa2e2d8 100644 --- a/common/include/Utilities/Threading.h +++ b/common/include/Utilities/Threading.h @@ -222,23 +222,24 @@ namespace Threading class NonblockingMutex { protected: - volatile int val; + std::atomic_flag val; public: - NonblockingMutex() : val( false ) {} + NonblockingMutex() { val.clear(); } virtual ~NonblockingMutex() throw() {} bool TryAcquire() throw() { - return !AtomicExchange( val, true ); + return !val.test_and_set(); } + // Can be done with a TryAcquire/Release but it is likely better to do it outside of the object bool IsLocked() - { return !!val; } + { pxAssertMsg(0, "IsLocked isn't supported for NonblockingMutex"); return false; } void Release() { - AtomicExchange( val, false ); + val.clear(); } };