Switch to using QAtomicInc instead of "volatile" flags in more places.

This commit is contained in:
LoRd_MuldeR 2017-04-20 21:55:54 +02:00
parent 74daec4d22
commit 953f102f8a
2 changed files with 12 additions and 11 deletions

View File

@ -152,7 +152,7 @@ namespace MUtils
friend class IPCChannel; friend class IPCChannel;
protected: protected:
volatile bool initialized; QAtomicInt initialized;
QScopedPointer<QSharedMemory> sharedmem; QScopedPointer<QSharedMemory> sharedmem;
QScopedPointer<QSystemSemaphore> semaphore_rd; QScopedPointer<QSystemSemaphore> semaphore_rd;
QScopedPointer<QSystemSemaphore> semaphore_wr; QScopedPointer<QSystemSemaphore> semaphore_wr;
@ -172,7 +172,6 @@ MUtils::IPCChannel::IPCChannel(const QString &applicationId, const quint32 &appV
m_appVersionNo(appVersionNo), m_appVersionNo(appVersionNo),
m_headerStr(QCryptographicHash::hash(MAKE_ID(applicationId, appVersionNo, channelId, "header").toLatin1(), QCryptographicHash::Sha1).toHex()) m_headerStr(QCryptographicHash::hash(MAKE_ID(applicationId, appVersionNo, channelId, "header").toLatin1(), QCryptographicHash::Sha1).toHex())
{ {
p->initialized = false;
if(m_headerStr.length() != Internal::HDR_LEN) if(m_headerStr.length() != Internal::HDR_LEN)
{ {
MUTILS_THROW("Invalid header length has been detected!"); 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) MUtils::IPCChannel::~IPCChannel(void)
{ {
if(p->initialized) if(MUTILS_BOOLIFY(p->initialized))
{ {
if(p->sharedmem->isAttached()) if(p->sharedmem->isAttached())
{ {
@ -200,7 +199,7 @@ int MUtils::IPCChannel::initialize(void)
{ {
QWriteLocker writeLock(&p->lock); QWriteLocker writeLock(&p->lock);
if(p->initialized) if(MUTILS_BOOLIFY(p->initialized))
{ {
return RET_ALREADY_INITIALIZED; return RET_ALREADY_INITIALIZED;
} }
@ -258,7 +257,7 @@ int MUtils::IPCChannel::initialize(void)
qWarning("Failed to access shared memory: %s", MUTILS_UTF8(errorMessage)); qWarning("Failed to access shared memory: %s", MUTILS_UTF8(errorMessage));
return RET_FAILURE; return RET_FAILURE;
} }
p->initialized = true; p->initialized.ref();
return RET_SUCCESS_SLAVE; return RET_SUCCESS_SLAVE;
} }
else else
@ -300,7 +299,7 @@ int MUtils::IPCChannel::initialize(void)
//qDebug("IPC KEY #2: %s", MUTILS_UTF8(p->semaphore_rd->key())); //qDebug("IPC KEY #2: %s", MUTILS_UTF8(p->semaphore_rd->key()));
//qDebug("IPC KEY #3: %s", MUTILS_UTF8(p->semaphore_wr->key())); //qDebug("IPC KEY #3: %s", MUTILS_UTF8(p->semaphore_wr->key()));
p->initialized = true; p->initialized.ref();
return RET_SUCCESS_MASTER; return RET_SUCCESS_MASTER;
} }

View File

@ -1668,7 +1668,7 @@ static volatile bool g_debug_check = check_debugger_helper();
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static MUtils::Internal::CriticalSection g_fatal_exit_lock; 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) 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(); g_fatal_exit_lock.enter();
if(!g_fatal_exit_flag) if(!g_fatal_exit_flag.testAndSetOrdered(0, 1))
{ {
return; /*prevent recursive invocation*/ return; /*prevent recursive invocation*/
} }
g_fatal_exit_flag = false;
if(g_main_thread_id != GetCurrentThreadId()) if(g_main_thread_id != GetCurrentThreadId())
{ {
if(HANDLE hThreadMain = OpenThread(THREAD_SUSPEND_RESUME, FALSE, g_main_thread_id)) 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); WaitForSingleObject(hThread, INFINITE);
} }
else
{
fatal_exit_helper((LPVOID)errorMessage);
}
for(;;) for(;;)
{ {