From 5ca92ecd67d95a15baaf93eb80a1be594a514779 Mon Sep 17 00:00:00 2001 From: Gregory Hainaut Date: Mon, 22 Feb 2016 22:14:29 +0100 Subject: [PATCH] Common: port code to std::atomic --- common/include/Utilities/General.h | 2 +- common/include/Utilities/PersistentThread.h | 8 ++++---- common/src/Utilities/Mutex.cpp | 6 +++--- common/src/Utilities/ThreadTools.cpp | 6 +++--- common/src/Utilities/wxHelpers.cpp | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/common/include/Utilities/General.h b/common/include/Utilities/General.h index f2182e5fad..074157fd72 100644 --- a/common/include/Utilities/General.h +++ b/common/include/Utilities/General.h @@ -111,7 +111,7 @@ protected: class BaseDeletableObject : public virtual IDeletableObject { protected: - volatile vol_t m_IsBeingDeleted; + std::atomic m_IsBeingDeleted; public: BaseDeletableObject(); diff --git a/common/include/Utilities/PersistentThread.h b/common/include/Utilities/PersistentThread.h index 247020dbd8..491930855f 100644 --- a/common/include/Utilities/PersistentThread.h +++ b/common/include/Utilities/PersistentThread.h @@ -103,8 +103,8 @@ namespace Threading MutexRecursive m_mtx_start; // used to lock the Start() code from starting simultaneous threads accidentally. Mutex m_mtx_ThreadName; - volatile vol_t m_detached; // a boolean value which indicates if the m_thread handle is valid - volatile s32 m_running; // set true by Start(), and set false by Cancel(), Block(), etc. + std::atomic m_detached; // a boolean value which indicates if the m_thread handle is valid + std::atomic m_running; // set true by Start(), and set false by Cancel(), Block(), etc. // exception handle, set non-NULL if the thread terminated with an exception // Use RethrowException() to re-throw the exception using its original exception type. @@ -243,8 +243,8 @@ namespace Threading class BaseTaskThread : public pxThread { protected: - volatile bool m_Done; - volatile bool m_TaskPending; + std::atomic m_Done; + std::atomic m_TaskPending; Semaphore m_post_TaskComplete; Mutex m_lock_TaskComplete; diff --git a/common/src/Utilities/Mutex.cpp b/common/src/Utilities/Mutex.cpp index 19d0735c16..9822b3bb21 100644 --- a/common/src/Utilities/Mutex.cpp +++ b/common/src/Utilities/Mutex.cpp @@ -23,7 +23,7 @@ namespace Threading { - static vol_t _attr_refcount = 0; + static std::atomic _attr_refcount(0); static pthread_mutexattr_t _attr_recursive; } @@ -120,7 +120,7 @@ Threading::Mutex::~Mutex() throw() Threading::MutexRecursive::MutexRecursive() : Mutex( false ) { - if( _InterlockedIncrement( &_attr_refcount ) == 1 ) + if( _attr_refcount.fetch_add(1) == 1 ) { if( 0 != pthread_mutexattr_init( &_attr_recursive ) ) throw Exception::OutOfMemory(L"Recursive mutexing attributes"); @@ -134,7 +134,7 @@ Threading::MutexRecursive::MutexRecursive() : Mutex( false ) Threading::MutexRecursive::~MutexRecursive() throw() { - if( _InterlockedDecrement( &_attr_refcount ) == 0 ) + if( _attr_refcount.fetch_sub(1) == 0 ) pthread_mutexattr_destroy( &_attr_recursive ); } diff --git a/common/src/Utilities/ThreadTools.cpp b/common/src/Utilities/ThreadTools.cpp index 324abca580..7c9820a210 100644 --- a/common/src/Utilities/ThreadTools.cpp +++ b/common/src/Utilities/ThreadTools.cpp @@ -291,7 +291,7 @@ bool Threading::pxThread::Detach() { AffinityAssert_DisallowFromSelf(pxDiagSpot); - if( _InterlockedExchange( &m_detached, true ) ) return false; + if( m_detached.exchange(true) ) return false; pthread_detach( m_thread ); return true; } @@ -386,7 +386,7 @@ bool Threading::pxThread::IsSelf() const bool Threading::pxThread::IsRunning() const { - return !!m_running; + return m_running; } void Threading::pxThread::AddListener( EventListener_Thread& evt ) @@ -669,7 +669,7 @@ void Threading::pxThread::OnCleanupInThread() m_native_handle = 0; m_native_id = 0; - + m_evtsrc_OnDelete.Dispatch( 0 ); } diff --git a/common/src/Utilities/wxHelpers.cpp b/common/src/Utilities/wxHelpers.cpp index dbb2bd1f0b..21242e6a37 100644 --- a/common/src/Utilities/wxHelpers.cpp +++ b/common/src/Utilities/wxHelpers.cpp @@ -49,7 +49,7 @@ pxDialogCreationFlags pxDialogFlags() // bool BaseDeletableObject::MarkForDeletion() { - return !_InterlockedExchange( &m_IsBeingDeleted, true ); + return !m_IsBeingDeleted.exchange(true); } void BaseDeletableObject::DeleteSelf()