OSDN Git Service

Switch to using QAtomicInc instead of "volatile" flags in more places.
authorLoRd_MuldeR <mulder2@gmx.de>
Thu, 20 Apr 2017 19:55:54 +0000 (21:55 +0200)
committerLoRd_MuldeR <mulder2@gmx.de>
Thu, 20 Apr 2017 19:55:54 +0000 (21:55 +0200)
src/IPCChannel.cpp
src/OSSupport_Win32.cpp

index 5b1e6f4..a129c05 100644 (file)
@@ -152,7 +152,7 @@ namespace MUtils
                friend class IPCChannel;
 
        protected:
-               volatile bool initialized;
+               QAtomicInt initialized;
                QScopedPointer<QSharedMemory> sharedmem;
                QScopedPointer<QSystemSemaphore> semaphore_rd;
                QScopedPointer<QSystemSemaphore> 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;
 }
 
index 80cc753..fc2a641 100644 (file)
@@ -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(;;)
        {