OSDN Git Service

Added global lamexp_fatal_exit() function.
authorLoRd_MuldeR <mulder2@gmx.de>
Tue, 11 Dec 2012 21:54:00 +0000 (22:54 +0100)
committerLoRd_MuldeR <mulder2@gmx.de>
Tue, 11 Dec 2012 22:22:20 +0000 (23:22 +0100)
src/Config.h
src/Global.cpp
src/Global.h
src/Main.cpp
src/Thread_CPUObserver.cpp
src/Thread_DiskObserver.cpp
src/Thread_Process.cpp
src/Thread_RAMObserver.cpp

index 9b55426..7dc8efb 100644 (file)
@@ -30,7 +30,7 @@
 #define VER_LAMEXP_MINOR_LO                                    7
 #define VER_LAMEXP_TYPE                                                Alpha
 #define VER_LAMEXP_PATCH                                       6
-#define VER_LAMEXP_BUILD                                       1212
+#define VER_LAMEXP_BUILD                                       1214
 
 ///////////////////////////////////////////////////////////////////////////////
 // Tool versions (minimum expected versions!)
index 44f49eb..d3a897f 100644 (file)
@@ -479,14 +479,7 @@ bool lamexp_detect_wine(void)
  */
 LONG WINAPI lamexp_exception_handler(__in struct _EXCEPTION_POINTERS *ExceptionInfo)
 {
-       if(GetCurrentThreadId() != g_main_thread_id)
-       {
-               HANDLE mainThread = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id);
-               if(mainThread) TerminateThread(mainThread, ULONG_MAX);
-       }
-       
-       FatalAppExit(0, L"Unhandeled exception handler invoked, application will exit!");
-       TerminateProcess(GetCurrentProcess(), -1);
+       lamexp_fatal_exit(L"Unhandeled exception handler invoked, application will exit!");
        return LONG_MAX;
 }
 
@@ -495,15 +488,7 @@ LONG WINAPI lamexp_exception_handler(__in struct _EXCEPTION_POINTERS *ExceptionI
  */
 void lamexp_invalid_param_handler(const wchar_t* exp, const wchar_t* fun, const wchar_t* fil, unsigned int, uintptr_t)
 {
-       if(GetCurrentThreadId() != g_main_thread_id)
-       {
-               HANDLE mainThread = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id);
-               if(mainThread) TerminateThread(mainThread, ULONG_MAX);
-               
-       }
-       
-       FatalAppExit(0, L"Invalid parameter handler invoked, application will exit!");
-       TerminateProcess(GetCurrentProcess(), -1);
+       lamexp_fatal_exit(L"Invalid parameter handler invoked, application will exit!");
 }
 
 /*
@@ -523,13 +508,9 @@ static void lamexp_console_color(FILE* file, WORD attributes)
  */
 void lamexp_message_handler(QtMsgType type, const char *msg)
 {
-       static volatile bool bFatalFlag = false;
        static const char *GURU_MEDITATION = "\n\nGURU MEDITATION !!!\n\n";
 
-       if(bFatalFlag || (msg == NULL))
-       {
-               return; //We are about to terminate, discard any further messages!
-       }
+       if(msg == NULL) return;
 
        QMutexLocker lock(&g_lamexp_message_mutex);
 
@@ -552,7 +533,6 @@ void lamexp_message_handler(QtMsgType type, const char *msg)
                {
                case QtCriticalMsg:
                case QtFatalMsg:
-                       bFatalFlag = true;
                        fflush(stdout);
                        fflush(stderr);
                        lamexp_console_color(stderr, FOREGROUND_RED | FOREGROUND_INTENSITY);
@@ -597,19 +577,10 @@ void lamexp_message_handler(QtMsgType type, const char *msg)
                OutputDebugStringA(temp.toLatin1().constData());
        }
 
-       if(bFatalFlag)
+       if((type == QtCriticalMsg) || (type == QtFatalMsg))
        {
                lock.unlock();
-
-               if(GetCurrentThreadId() != g_main_thread_id)
-               {
-                       HANDLE mainThread = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id);
-                       if(mainThread) TerminateThread(mainThread, ULONG_MAX);
-               }
-
-               MessageBoxW(NULL, QWCHAR(QString::fromUtf8(msg)), L"LameXP - GURU MEDITATION", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL);
-               FatalAppExit(0, L"The application has encountered a critical error and will exit now!");
-               TerminateProcess(GetCurrentProcess(), -1);
+               lamexp_fatal_exit(L"The application has encountered a critical error and will exit now!", QWCHAR(QString::fromUtf8(msg)));
        }
 }
 
@@ -849,15 +820,7 @@ static unsigned int __stdcall lamexp_debug_thread_proc(LPVOID lpParameter)
        {
                Sleep(250);
        }
-       if(HANDLE thrd = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id))
-       {
-               if(TerminateThread(thrd, -1))
-               {
-                       FatalAppExit(0, L"Not a debug build. Please unload debugger and try again!");
-               }
-               CloseHandle(thrd);
-       }
-       TerminateProcess(GetCurrentProcess(), -1);
+       lamexp_fatal_exit(L"Not a debug build. Please unload debugger and try again!");
        return 666;
 }
 
@@ -868,8 +831,7 @@ static HANDLE lamexp_debug_thread_init(void)
 {
        if(lamexp_check_for_debugger())
        {
-               FatalAppExit(0, L"Not a debug build. Please unload debugger and try again!");
-               TerminateProcess(GetCurrentProcess(), -1);
+               lamexp_fatal_exit(L"Not a debug build. Please unload debugger and try again!");
        }
 
        return (HANDLE) _beginthreadex(NULL, 0, lamexp_debug_thread_proc, NULL, 0, NULL);
@@ -1019,8 +981,7 @@ static bool lamexp_event_filter(void *message, long *result)
 {
        if((!(LAMEXP_DEBUG)) && lamexp_check_for_debugger())
        {
-               FatalAppExit(0, L"Not a debug build. Please unload debugger and try again!");
-               TerminateProcess(GetCurrentProcess(), -1);
+               lamexp_fatal_exit(L"Not a debug build. Please unload debugger and try again!");
        }
        
        switch(reinterpret_cast<MSG*>(message)->message)
@@ -2301,8 +2262,7 @@ static DWORD lamexp_entry_check(void)
        volatile DWORD retVal = 0xA199B5AF;
        if(g_lamexp_entry_check_flag != 0x8761F64D)
        {
-               FatalAppExit(0, L"Application initialization has failed, take care!");
-               TerminateProcess(GetCurrentProcess(), -1);
+               lamexp_fatal_exit(L"Application initialization has failed, take care!");
        }
        return retVal;
 }
@@ -2318,14 +2278,11 @@ extern "C"
        {
                if((!LAMEXP_DEBUG) && lamexp_check_for_debugger())
                {
-                       FatalAppExit(0, L"Not a debug build. Please unload debugger and try again!");
-                       TerminateProcess(GetCurrentProcess(), -1);
+                       lamexp_fatal_exit(L"Not a debug build. Please unload debugger and try again!");
                }
-
                if(g_lamexp_entry_check_flag != 0x789E09B2)
                {
-                       FatalAppExit(0, L"Application initialization has failed, take care!");
-                       TerminateProcess(GetCurrentProcess(), -1);
+                       lamexp_fatal_exit(L"Application initialization has failed, take care!");
                }
 
                //Zero *before* constructors are called
@@ -2346,6 +2303,35 @@ extern "C"
 }
 
 /*
+ * Fatal application exit
+ */
+#pragma intrinsic(_InterlockedExchange)
+void lamexp_fatal_exit(const wchar_t* exitMessage, const wchar_t* errorBoxMessage)
+{
+       static volatile long bFatalFlag = 0L;
+
+       if(_InterlockedExchange(&bFatalFlag, 1L) == 0L)
+       {
+               if(GetCurrentThreadId() != g_main_thread_id)
+               {
+                       HANDLE mainThread = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id);
+                       if(mainThread) TerminateThread(mainThread, ULONG_MAX);
+               }
+       
+               if(errorBoxMessage)
+               {
+                       MessageBoxW(NULL, errorBoxMessage, L"LameXP - GURU MEDITATION", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL);
+               }
+
+               for(;;)
+               {
+                       FatalAppExit(0, exitMessage);
+                       TerminateProcess(GetCurrentProcess(), -1);
+               }
+       }
+}
+
+/*
  * Finalization function (final clean-up)
  */
 void lamexp_finalization(void)
index 9c4b630..7c4d626 100644 (file)
@@ -158,6 +158,7 @@ const QString lamexp_clean_filename(const QString &str);
 const QString lamexp_clean_filepath(const QString &str);
 void lamexp_seed_rand(void);
 unsigned int lamexp_rand(void);
+void lamexp_fatal_exit(const wchar_t* exitMessage, const wchar_t* errorBoxMessage = NULL);
 
 //Debug-only functions
 SIZE_T lamexp_dbg_private_bytes(void);
index 300fc77..dc9e65e 100644 (file)
@@ -233,24 +233,21 @@ static int _main(int argc, char* argv[])
                        fflush(stdout);
                        fflush(stderr);
                        fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error message: %s\n", error);
-                       FatalAppExit(0, L"Unhandeled C++ exception error, application will exit!");
-                       TerminateProcess(GetCurrentProcess(), -1);
+                       lamexp_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
                }
                catch(int error)
                {
                        fflush(stdout);
                        fflush(stderr);
                        fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error code: 0x%X\n", error);
-                       FatalAppExit(0, L"Unhandeled C++ exception error, application will exit!");
-                       TerminateProcess(GetCurrentProcess(), -1);
+                       lamexp_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
                }
                catch(...)
                {
                        fflush(stdout);
                        fflush(stderr);
                        fprintf(stderr, "\nGURU MEDITATION !!!\n");
-                       FatalAppExit(0, L"Unhandeled C++ exception error, application will exit!");
-                       TerminateProcess(GetCurrentProcess(), -1);
+                       lamexp_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
                }
                return iResult;
        }
@@ -277,8 +274,7 @@ int main(int argc, char* argv[])
                        fflush(stdout);
                        fflush(stderr);
                        fprintf(stderr, "\nGURU MEDITATION !!!\n\nUnhandeled structured exception error! [code: 0x%X]\n", GetExceptionCode());
-                       FatalAppExit(0, L"Unhandeled structured exception error, application will exit!");
-                       TerminateProcess(GetCurrentProcess(), -1);
+                       lamexp_fatal_exit(L"Unhandeled structured exception error, application will exit!");
                }
        }
 }
index 8af9619..6d3460b 100644 (file)
@@ -73,8 +73,7 @@ void CPUObserverThread::run(void)
                fflush(stdout);
                fflush(stderr);
                fprintf(stderr, "\nGURU MEDITATION !!!\n");
-               FatalAppExit(0, L"Unhandeled exception error, application will exit!");
-               TerminateProcess(GetCurrentProcess(), -1);
+               lamexp_fatal_exit(L"Unhandeled exception error, application will exit!");
        }
 
        while(m_semaphore.available()) m_semaphore.tryAcquire();
index d72029f..cb08942 100644 (file)
@@ -59,8 +59,7 @@ void DiskObserverThread::run(void)
                fflush(stdout);
                fflush(stderr);
                fprintf(stderr, "\nGURU MEDITATION !!!\n");
-               FatalAppExit(0, L"Unhandeled exception error, application will exit!");
-               TerminateProcess(GetCurrentProcess(), -1);
+               lamexp_fatal_exit(L"Unhandeled exception error, application will exit!");
        }
 
        while(m_semaphore.available()) m_semaphore.tryAcquire();
index 159d5bd..c89643f 100644 (file)
@@ -113,8 +113,7 @@ void ProcessThread::run()
                fflush(stdout);
                fflush(stderr);
                fprintf(stderr, "\nGURU MEDITATION !!!\n");
-               FatalAppExit(0, L"Unhandeled exception error, application will exit!");
-               TerminateProcess(GetCurrentProcess(), -1);
+               lamexp_fatal_exit(L"Unhandeled exception error, application will exit!");
        }
 }
 
index ac8efd4..3885937 100644 (file)
@@ -53,8 +53,7 @@ void RAMObserverThread::run(void)
                fflush(stdout);
                fflush(stderr);
                fprintf(stderr, "\nGURU MEDITATION !!!\n");
-               FatalAppExit(0, L"Unhandeled exception error, application will exit!");
-               TerminateProcess(GetCurrentProcess(), -1);
+               lamexp_fatal_exit(L"Unhandeled exception error, application will exit!");
        }
 
        while(m_semaphore.available()) m_semaphore.tryAcquire();