From 953f102f8a081a7d3314b0f34b4ffd9c6398b067 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Thu, 20 Apr 2017 21:55:54 +0200 Subject: [PATCH] Switch to using QAtomicInc instead of "volatile" flags in more places. --- src/IPCChannel.cpp | 11 +++++------ src/OSSupport_Win32.cpp | 12 +++++++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/IPCChannel.cpp b/src/IPCChannel.cpp index 5b1e6f4..a129c05 100644 --- a/src/IPCChannel.cpp +++ b/src/IPCChannel.cpp @@ -152,7 +152,7 @@ namespace MUtils friend class IPCChannel; protected: - volatile bool initialized; + QAtomicInt initialized; QScopedPointer sharedmem; QScopedPointer semaphore_rd; QScopedPointer semaphore_wr; @@ -172,7 +172,6 @@ MUtils::IPCChannel::IPCChannel(const QString &applicationId, const quint32 &appV m_appVersionNo(appVersionNo), m_headerStr(QCryptographicHash::hash(MAKE_ID(applicationId, appVersionNo, channelId, "header").toLatin1(), QCryptographicHash::Sha1).toHex()) { - p->initialized = false; if(m_headerStr.length() != Internal::HDR_LEN) { MUTILS_THROW("Invalid header length has been detected!"); @@ -181,7 +180,7 @@ MUtils::IPCChannel::IPCChannel(const QString &applicationId, const quint32 &appV MUtils::IPCChannel::~IPCChannel(void) { - if(p->initialized) + if(MUTILS_BOOLIFY(p->initialized)) { if(p->sharedmem->isAttached()) { @@ -200,7 +199,7 @@ int MUtils::IPCChannel::initialize(void) { QWriteLocker writeLock(&p->lock); - if(p->initialized) + if(MUTILS_BOOLIFY(p->initialized)) { return RET_ALREADY_INITIALIZED; } @@ -258,7 +257,7 @@ int MUtils::IPCChannel::initialize(void) qWarning("Failed to access shared memory: %s", MUTILS_UTF8(errorMessage)); return RET_FAILURE; } - p->initialized = true; + p->initialized.ref(); return RET_SUCCESS_SLAVE; } else @@ -300,7 +299,7 @@ int MUtils::IPCChannel::initialize(void) //qDebug("IPC KEY #2: %s", MUTILS_UTF8(p->semaphore_rd->key())); //qDebug("IPC KEY #3: %s", MUTILS_UTF8(p->semaphore_wr->key())); - p->initialized = true; + p->initialized.ref(); return RET_SUCCESS_MASTER; } diff --git a/src/OSSupport_Win32.cpp b/src/OSSupport_Win32.cpp index 80cc753..fc2a641 100644 --- a/src/OSSupport_Win32.cpp +++ b/src/OSSupport_Win32.cpp @@ -1668,7 +1668,7 @@ static volatile bool g_debug_check = check_debugger_helper(); /////////////////////////////////////////////////////////////////////////////// static MUtils::Internal::CriticalSection g_fatal_exit_lock; -static volatile bool g_fatal_exit_flag = true; +static QAtomicInt g_fatal_exit_flag; static DWORD WINAPI fatal_exit_helper(LPVOID lpParameter) { @@ -1680,13 +1680,11 @@ void MUtils::OS::fatal_exit(const wchar_t* const errorMessage) { g_fatal_exit_lock.enter(); - if(!g_fatal_exit_flag) + if(!g_fatal_exit_flag.testAndSetOrdered(0, 1)) { return; /*prevent recursive invocation*/ } - g_fatal_exit_flag = false; - if(g_main_thread_id != GetCurrentThreadId()) { if(HANDLE hThreadMain = OpenThread(THREAD_SUSPEND_RESUME, FALSE, g_main_thread_id)) @@ -1695,10 +1693,14 @@ void MUtils::OS::fatal_exit(const wchar_t* const errorMessage) } } - if(HANDLE hThread = CreateThread(NULL, 0, fatal_exit_helper, (LPVOID) errorMessage, 0, NULL)) + if(HANDLE hThread = CreateThread(NULL, 0, fatal_exit_helper, (LPVOID)errorMessage, 0, NULL)) { WaitForSingleObject(hThread, INFINITE); } + else + { + fatal_exit_helper((LPVOID)errorMessage); + } for(;;) {