From df9b5afb6887e19d9a04c8dbfeb3023ab32ca323 Mon Sep 17 00:00:00 2001 From: lordmulder Date: Fri, 10 May 2019 20:40:46 +0200 Subject: [PATCH] Refactored "common" thread code into a separate base class. Make additional thread classes use the "common" base class. --- src/thread_abstract.cpp | 99 ++++++++++++++++++++++++++ src/thread_abstract.h | 51 +++++++++++++ src/thread_encode.cpp | 62 ++++------------ src/thread_encode.h | 8 +-- src/thread_ipc_recv.cpp | 9 ++- src/thread_ipc_recv.h | 10 ++- src/thread_ipc_send.cpp | 13 +++- src/thread_ipc_send.h | 10 ++- src/thread_startup.cpp | 39 ---------- src/thread_startup.h | 22 +----- src/version.h | 2 +- x264_launcher_MSVC2013.vcxproj | 10 +++ x264_launcher_MSVC2013.vcxproj.filters | 15 +++- x264_launcher_MSVC2015.vcxproj | 10 +++ x264_launcher_MSVC2015.vcxproj.filters | 15 +++- x264_launcher_MSVC2017.vcxproj | 10 +++ x264_launcher_MSVC2017.vcxproj.filters | 15 +++- 17 files changed, 272 insertions(+), 128 deletions(-) create mode 100644 src/thread_abstract.cpp create mode 100644 src/thread_abstract.h diff --git a/src/thread_abstract.cpp b/src/thread_abstract.cpp new file mode 100644 index 0000000..5672214 --- /dev/null +++ b/src/thread_abstract.cpp @@ -0,0 +1,99 @@ +/////////////////////////////////////////////////////////////////////////////// +// Simple x264 Launcher +// Copyright (C) 2004-2019 LoRd_MuldeR +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// http://www.gnu.org/licenses/gpl-2.0.txt +/////////////////////////////////////////////////////////////////////////////// + +#include "thread_abstract.h" + +//MUtils +#include + +//Qt +#include +#include +#include + +//------------------------------------- +// Constructor +//------------------------------------- + +AbstractThread::AbstractThread(void) +{ + m_exception = false; + m_success = 0; +} + +AbstractThread::~AbstractThread(void) +{ +} + +//------------------------------------- +// Thread entry point +//------------------------------------- + +void AbstractThread::run(void) +{ + m_exception = false; + m_success = 0; + runChecked1(this, m_success, &m_exception); +} + +void AbstractThread::runChecked1(AbstractThread *const thread, volatile int &success, volatile bool *exception) +{ +#if !defined(_DEBUG) + __try + { + return runChecked2(thread, success, exception); + } + __except(1) + { + *exception = true; + qWarning("Unhandled structured exception in worker thread !!!"); + } +#else + return runChecked2(thread, success, exception); +#endif +} + +void AbstractThread::runChecked2(AbstractThread *const thread, volatile int &success, volatile bool *exception) +{ +#if !defined(_DEBUG) + try + { + success = thread->threadMain(); + } + catch(const std::exception &e) + { + *exception = true; + qWarning("Worker thread raised an C++ exception: %s", e.what()); + } + catch(char *const msg) + { + *exception = true; + qWarning("Worker thread raised an C++ exception: %s", msg); + } + catch(...) + { + *exception = true; + qWarning("Worker thread raised an C++ exception!"); + } +#else + success = thread->threadMain(); +#endif +} diff --git a/src/thread_abstract.h b/src/thread_abstract.h new file mode 100644 index 0000000..335eb93 --- /dev/null +++ b/src/thread_abstract.h @@ -0,0 +1,51 @@ +/////////////////////////////////////////////////////////////////////////////// +// Simple x264 Launcher +// Copyright (C) 2004-2019 LoRd_MuldeR +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// http://www.gnu.org/licenses/gpl-2.0.txt +/////////////////////////////////////////////////////////////////////////////// + +#pragma once + +//Qt +#include + +class AbstractThread : public QThread +{ + Q_OBJECT + +public: + AbstractThread(void); + ~AbstractThread(void); + + bool getException(void) { return m_exception; } + int getSuccess(void) { return m_success; } + +protected: + volatile int m_success; + volatile bool m_exception; + + //Entry point + virtual void run(void); + + //Error handling + static void runChecked1(AbstractThread *const thread, volatile int &success, volatile bool *exception); + static void runChecked2(AbstractThread *const thread, volatile int &success, volatile bool *exception); + + //Thread main + virtual int threadMain(void) = 0; +}; diff --git a/src/thread_encode.cpp b/src/thread_encode.cpp index 4a5a167..730cd37 100644 --- a/src/thread_encode.cpp +++ b/src/thread_encode.cpp @@ -86,13 +86,13 @@ private: log("\nPROCESS ABORTED BY USER !!!"); \ setStatus(JobStatus_Aborted); \ if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \ - return; \ + return 0; \ } \ else if(!(OK_FLAG)) \ { \ setStatus(JobStatus_Failed); \ if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \ - return; \ + return 0; \ } \ } \ while(0) @@ -168,18 +168,16 @@ EncodeThread::~EncodeThread(void) void EncodeThread::run(void) { -#if !defined(_DEBUG) - __try + m_progress = 0; + m_status = JobStatus_Starting; + + AbstractThread::run(); + + if (m_exception) { - checkedRun(); + log(tr("UNHANDLED EXCEPTION ERROR IN THREAD !!!")); + setStatus(JobStatus_Failed); } - __except(1) - { - qWarning("STRUCTURED EXCEPTION ERROR IN ENCODE THREAD !!!"); - } -#else - checkedRun(); -#endif if(m_jobObject) { @@ -188,40 +186,6 @@ void EncodeThread::run(void) } } -void EncodeThread::checkedRun(void) -{ - m_progress = 0; - m_status = JobStatus_Starting; - - try - { - try - { - ExecutionStateHandler executionStateHandler; - encode(); - } - catch(const std::exception &e) - { - log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(e.what()))); - setStatus(JobStatus_Failed); - } - catch(char *msg) - { - log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(msg))); - setStatus(JobStatus_Failed); - } - catch(...) - { - log(tr("UNHANDLED EXCEPTION ERROR IN THREAD !!!")); - setStatus(JobStatus_Failed); - } - } - catch(...) - { - MUtils::OS::fatal_exit(L"Unhandeled exception error in encode thread!"); - } -} - void EncodeThread::start(Priority priority) { qDebug("Thread starting..."); @@ -230,14 +194,14 @@ void EncodeThread::start(Priority priority) m_pause = false; while(m_semaphorePaused.tryAcquire(1, 0)); - QThread::start(priority); + AbstractThread::start(priority); } /////////////////////////////////////////////////////////////////////////////// // Encode functions /////////////////////////////////////////////////////////////////////////////// -void EncodeThread::encode(void) +int EncodeThread::threadMain(void) { QDateTime startTime = QDateTime::currentDateTime(); @@ -355,6 +319,8 @@ void EncodeThread::encode(void) int timePassed = startTime.secsTo(QDateTime::currentDateTime()); log(tr("Job finished at %1, %2. Process took %3 minutes, %4 seconds.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString(Qt::ISODate), QString::number(timePassed / 60), QString::number(timePassed % 60))); setStatus(JobStatus_Completed); + + return 1; /*completed*/ } /////////////////////////////////////////////////////////////////////////////// diff --git a/src/thread_encode.h b/src/thread_encode.h index 1a86b2f..469ab23 100644 --- a/src/thread_encode.h +++ b/src/thread_encode.h @@ -21,6 +21,7 @@ #pragma once +#include "thread_abstract.h" #include "model_status.h" #include @@ -37,7 +38,7 @@ class JobObject; class AbstractEncoder; class AbstractSource; -class EncodeThread : public QThread +class EncodeThread : public AbstractThread { Q_OBJECT @@ -100,10 +101,9 @@ protected: //Entry point virtual void run(void); - virtual void checkedRun(void); - //Main encoding functions - void encode(void); + //Thread main + virtual int threadMain(void); //Static functions static QString getPasslogFile(const QString &outputFile); diff --git a/src/thread_ipc_recv.cpp b/src/thread_ipc_recv.cpp index b9b2d84..3ca10ee 100644 --- a/src/thread_ipc_recv.cpp +++ b/src/thread_ipc_recv.cpp @@ -60,9 +60,14 @@ IPCThread_Recv::~IPCThread_Recv(void) // Thread Main //////////////////////////////////////////////////////////// -void IPCThread_Recv::run() +void IPCThread_Recv::run(void) { setTerminationEnabled(true); + AbstractThread::run(); +} + +int IPCThread_Recv::threadMain(void) +{ QStringList params; quint32 command, flags; @@ -81,6 +86,8 @@ void IPCThread_Recv::run() break; } } + + return 1; } //////////////////////////////////////////////////////////// diff --git a/src/thread_ipc_recv.h b/src/thread_ipc_recv.h index f21a487..a743eaa 100644 --- a/src/thread_ipc_recv.h +++ b/src/thread_ipc_recv.h @@ -22,14 +22,14 @@ #pragma once -#include +#include "thread_abstract.h" namespace MUtils { class IPCChannel; } -class IPCThread_Recv: public QThread +class IPCThread_Recv: public AbstractThread { Q_OBJECT @@ -46,5 +46,9 @@ protected: volatile bool m_stopFlag; MUtils::IPCChannel *const m_ipcChannel; - void run(); + //Entry point + virtual void run(void); + + //Thread main + virtual int threadMain(void); }; diff --git a/src/thread_ipc_send.cpp b/src/thread_ipc_send.cpp index 1764700..069a999 100644 --- a/src/thread_ipc_send.cpp +++ b/src/thread_ipc_send.cpp @@ -55,9 +55,18 @@ IPCThread_Send::~IPCThread_Send(void) { } -void IPCThread_Send::run() +//////////////////////////////////////////////////////////// +// Thread Main +//////////////////////////////////////////////////////////// + +void IPCThread_Send::run(void) { setTerminationEnabled(true); + AbstractThread::run(); +} + +int IPCThread_Send::threadMain(void) +{ bool bSentFiles = false; const MUtils::OS::ArgumentMap &args = MUtils::OS::arguments(); @@ -114,6 +123,8 @@ void IPCThread_Send::run() qWarning("Failed to send IPC message!"); } } + + return 1; } //////////////////////////////////////////////////////////// diff --git a/src/thread_ipc_send.h b/src/thread_ipc_send.h index 6f6bcb5..7c727fc 100644 --- a/src/thread_ipc_send.h +++ b/src/thread_ipc_send.h @@ -22,14 +22,14 @@ #pragma once -#include +#include "thread_abstract.h" namespace MUtils { class IPCChannel; } -class IPCThread_Send: public QThread +class IPCThread_Send: public AbstractThread { Q_OBJECT @@ -37,7 +37,11 @@ public: IPCThread_Send(MUtils::IPCChannel *const ipcChannel); ~IPCThread_Send(void); - void run(); + //Entry point + virtual void run(void); + + //Thread main + virtual int threadMain(void); protected: MUtils::IPCChannel *const m_ipcChannel; diff --git a/src/thread_startup.cpp b/src/thread_startup.cpp index 33887f3..d26af3a 100644 --- a/src/thread_startup.cpp +++ b/src/thread_startup.cpp @@ -35,51 +35,12 @@ StarupThread::StarupThread(void) { - m_exception = false; - m_success = 0; } StarupThread::~StarupThread(void) { } -//------------------------------------- -// Thread entry point -//------------------------------------- - -void StarupThread::run(void) -{ - m_exception = false; - m_success = 0; - runChecked1(this, m_success, &m_exception); -} - -void StarupThread::runChecked1(StarupThread *const thread, volatile int &success, volatile bool *exception) -{ - __try - { - return runChecked2(thread, success, exception); - } - __except(1) - { - *exception = true; - qWarning("Unhandled exception error in startup thread !!!"); - } -} - -void StarupThread::runChecked2(StarupThread *const thread, volatile int &success, volatile bool *exception) -{ - try - { - success = thread->threadMain(); - } - catch(...) - { - *exception = true; - qWarning("Startup thread raised an C++ exception!"); - } -} - //------------------------------------- // Utility functions //------------------------------------- diff --git a/src/thread_startup.h b/src/thread_startup.h index ab9bb78..836862f 100644 --- a/src/thread_startup.h +++ b/src/thread_startup.h @@ -21,11 +21,12 @@ #pragma once +#include "thread_abstract.h" + //Qt -#include #include -class StarupThread : public QThread +class StarupThread : public AbstractThread { Q_OBJECT @@ -33,26 +34,9 @@ public: StarupThread(void); ~StarupThread(void); - bool getException(void) { return m_exception; } - int getSuccess(void) { return m_success; } - protected slots: void start(Priority priority = InheritPriority) { QThread::start(priority); } protected: - volatile int m_success; - volatile bool m_exception; - - //Entry point - virtual void run(void); - - //Error handling - static void runChecked1(StarupThread *const thread, volatile int &success, volatile bool *exception); - static void runChecked2(StarupThread *const thread, volatile int &success, volatile bool *exception); - - //Thread main - virtual int threadMain(void) = 0; - - //Utility functions static QStringList runProcess(const QString &exePath, const QStringList &args, const QStringList *const extraPaths = NULL); }; diff --git a/src/version.h b/src/version.h index 02acb1e..3b25635 100644 --- a/src/version.h +++ b/src/version.h @@ -26,7 +26,7 @@ #define VER_X264_MAJOR 2 #define VER_X264_MINOR 9 #define VER_X264_PATCH 0 -#define VER_X264_BUILD 1156 +#define VER_X264_BUILD 1157 #define VER_X264_PORTABLE_EDITION (0) diff --git a/x264_launcher_MSVC2013.vcxproj b/x264_launcher_MSVC2013.vcxproj index ef4074c..2ba34a9 100644 --- a/x264_launcher_MSVC2013.vcxproj +++ b/x264_launcher_MSVC2013.vcxproj @@ -312,6 +312,14 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im $(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp;%(Outputs) $(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp;%(Outputs) + + "$(QTDIR)\bin\moc.exe" -o "$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp" "%(FullPath)" + "$(QTDIR)\bin\moc.exe" -o "$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp" "%(FullPath)" + MOC "$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp" + MOC "$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp" + $(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp;%(Outputs) + $(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp;%(Outputs) + @@ -471,6 +479,7 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im + @@ -492,6 +501,7 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im + diff --git a/x264_launcher_MSVC2013.vcxproj.filters b/x264_launcher_MSVC2013.vcxproj.filters index ebcb88f..369a941 100644 --- a/x264_launcher_MSVC2013.vcxproj.filters +++ b/x264_launcher_MSVC2013.vcxproj.filters @@ -119,9 +119,6 @@ Header Files - - Header Files - @@ -292,6 +289,12 @@ Generated Files + + Source Files + + + Generated Files + @@ -369,6 +372,12 @@ Header Files + + Header Files + + + Header Files + diff --git a/x264_launcher_MSVC2015.vcxproj b/x264_launcher_MSVC2015.vcxproj index c45195a..66cceb5 100644 --- a/x264_launcher_MSVC2015.vcxproj +++ b/x264_launcher_MSVC2015.vcxproj @@ -312,6 +312,14 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im $(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp;%(Outputs) $(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp;%(Outputs) + + "$(QTDIR)\bin\moc.exe" -o "$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp" "%(FullPath)" + "$(QTDIR)\bin\moc.exe" -o "$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp" "%(FullPath)" + MOC "$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp" + MOC "$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp" + $(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp;%(Outputs) + $(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp;%(Outputs) + @@ -471,6 +479,7 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im + @@ -492,6 +501,7 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im + diff --git a/x264_launcher_MSVC2015.vcxproj.filters b/x264_launcher_MSVC2015.vcxproj.filters index ebcb88f..369a941 100644 --- a/x264_launcher_MSVC2015.vcxproj.filters +++ b/x264_launcher_MSVC2015.vcxproj.filters @@ -119,9 +119,6 @@ Header Files - - Header Files - @@ -292,6 +289,12 @@ Generated Files + + Source Files + + + Generated Files + @@ -369,6 +372,12 @@ Header Files + + Header Files + + + Header Files + diff --git a/x264_launcher_MSVC2017.vcxproj b/x264_launcher_MSVC2017.vcxproj index 1d16863..42f63de 100644 --- a/x264_launcher_MSVC2017.vcxproj +++ b/x264_launcher_MSVC2017.vcxproj @@ -313,6 +313,14 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im $(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp;%(Outputs) $(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp;%(Outputs) + + "$(QTDIR)\bin\moc.exe" -o "$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp" "%(FullPath)" + "$(QTDIR)\bin\moc.exe" -o "$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp" "%(FullPath)" + MOC "$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp" + MOC "$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp" + $(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp;%(Outputs) + $(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp;%(Outputs) + @@ -472,6 +480,7 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im + @@ -493,6 +502,7 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im + diff --git a/x264_launcher_MSVC2017.vcxproj.filters b/x264_launcher_MSVC2017.vcxproj.filters index ebcb88f..369a941 100644 --- a/x264_launcher_MSVC2017.vcxproj.filters +++ b/x264_launcher_MSVC2017.vcxproj.filters @@ -119,9 +119,6 @@ Header Files - - Header Files - @@ -292,6 +289,12 @@ Generated Files + + Source Files + + + Generated Files + @@ -369,6 +372,12 @@ Header Files + + Header Files + + + Header Files +