[Common] Add thread class

This commit is contained in:
zilmar 2016-04-18 05:34:47 +10:00
parent 1daa43a6d8
commit bddd3841cc
10 changed files with 147 additions and 1 deletions

View File

@ -193,6 +193,10 @@
RelativePath=".\SyncEvent.cpp"
>
</File>
<File
RelativePath=".\Thread.cpp"
>
</File>
<File
RelativePath=".\Trace.cpp"
>
@ -266,6 +270,10 @@
RelativePath=".\SyncEvent.h"
>
</File>
<File
RelativePath=".\Thread.h"
>
</File>
<File
RelativePath=".\Trace.h"
>

View File

@ -47,6 +47,7 @@
<ClCompile Include="StdString.cpp" />
<ClCompile Include="SyncEvent.cpp" />
<ClCompile Include="DateTimeClass.cpp" />
<ClCompile Include="Thread.cpp" />
<ClCompile Include="Trace.cpp" />
<ClCompile Include="Util.cpp" />
</ItemGroup>
@ -66,6 +67,7 @@
<ClInclude Include="stdtypes.h" />
<ClInclude Include="SyncEvent.h" />
<ClInclude Include="DateTimeClass.h" />
<ClInclude Include="Thread.h" />
<ClInclude Include="Trace.h" />
<ClInclude Include="TraceModulesCommon.h" />
<ClInclude Include="Util.h" />

View File

@ -8,7 +8,6 @@
#define _strnicmp strncasecmp
#define _snprintf snprintf
#define _isnan isnan
#define GetCurrentThreadId pthread_self
int _vscprintf (const char * format, va_list pargs);

92
Source/Common/Thread.cpp Normal file
View File

@ -0,0 +1,92 @@
#include "stdafx.h"
#include "Thread.h"
#ifndef _WIN32
#include <unistd.h>
#include <pthread.h>
#endif
CThread::CThread(CTHREAD_START_ROUTINE lpStartAddress) :
m_StartAddress(lpStartAddress),
m_thread(NULL)
{
WriteTrace(TraceThread, TraceDebug, "Start");
WriteTrace(TraceThread, TraceDebug, "Done");
}
CThread::~CThread()
{
WriteTrace(TraceThread, TraceDebug, "Start");
if (isRunning())
{
Terminate();
}
#ifdef _WIN32
CloseHandle(m_thread);
#endif
WriteTrace(TraceThread, TraceDebug, "Done");
}
bool CThread::Start(void * lpThreadParameter)
{
WriteTrace(TraceThread, TraceDebug, "Start");
m_lpThreadParameter = lpThreadParameter;
m_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadWrapper, this, 0, (LPDWORD)&m_threadID);
WriteTrace(TraceThread, TraceDebug, "Done");
return true;
}
void * CThread::ThreadWrapper (CThread * _this)
{
WriteTrace(TraceThread, TraceDebug, "Start");
void * res = NULL;
try
{
res = (void *)_this->m_StartAddress(_this->m_lpThreadParameter);
}
catch (...)
{
//WriteTrace(TraceUserInterface, TraceError, "Unhandled Exception ");
}
CloseHandle(_this->m_thread);
_this->m_thread = NULL;
WriteTrace(TraceThread, TraceDebug, "Done");
return res;
}
bool CThread::isRunning(void) const
{
WriteTrace(TraceThread, TraceDebug, "Start");
if (m_thread == NULL)
{
WriteTrace(TraceThread, TraceDebug, "Done (res: false), m_thread is null");
return false;
}
DWORD ExitCode;
if (GetExitCodeThread(m_thread, &ExitCode))
{
if (ExitCode == STILL_ACTIVE)
{
WriteTrace(TraceThread, TraceDebug, "Done (res: true)");
return true;
}
}
WriteTrace(TraceThread, TraceDebug, "Done (res: false)");
return false;
}
void CThread::Terminate(void)
{
WriteTrace(TraceThread, TraceDebug, "Start");
if (isRunning())
{
WriteTrace(TraceThread, TraceDebug, "Terminating thread");
TerminateThread(m_thread, 0);
}
WriteTrace(TraceThread, TraceDebug, "Done");
}
uint32_t CThread::GetCurrentThreadId(void)
{
return ::GetCurrentThreadId();
}

29
Source/Common/Thread.h Normal file
View File

@ -0,0 +1,29 @@
#pragma once
class CThread
{
public:
typedef uint32_t(__stdcall * CTHREAD_START_ROUTINE)(void * lpThreadParameter);
CThread(CTHREAD_START_ROUTINE lpStartAddress);
~CThread();
bool Start(void * lpThreadParameter);
inline uint32_t ThreadID(void) const { return m_threadID; }
bool isRunning(void) const;
void Terminate(void);
static uint32_t GetCurrentThreadId(void);
private:
CThread(void); // Disable default constructor
CThread(const CThread&); // Disable copy constructor
CThread& operator=(const CThread&); // Disable assignment
static void * ThreadWrapper (CThread * _this);
CTHREAD_START_ROUTINE m_StartAddress;
void * m_lpThreadParameter;
void * m_thread;
uint32_t m_threadID;
};

View File

@ -3,5 +3,7 @@
enum TraceModuleCommon
{
TraceMD5,
TraceThread,
TracePath,
MaxTraceModuleCommon,
};

View File

@ -53,6 +53,8 @@ void SetupTrace(void)
TraceSetModuleName(TraceMD5, "MD5");
TraceSetModuleName(TraceThread, "Thread");
TraceSetModuleName(TracePath, "Path");
TraceSetModuleName(TraceSettings, "Settings");
TraceSetModuleName(TraceUnknown, "Unknown");
TraceSetModuleName(TraceGlide64, "Glide64");

View File

@ -51,6 +51,8 @@ void AddLogModule(void)
void SetTraceModuleNames(void)
{
TraceSetModuleName(TraceMD5, "MD5");
TraceSetModuleName(TraceThread, "Thread");
TraceSetModuleName(TracePath, "Path");
TraceSetModuleName(TraceSettings, "Settings");
TraceSetModuleName(TraceUnknown, "Unknown");
TraceSetModuleName(TraceAppInit, "App Init");
@ -74,6 +76,8 @@ void SetTraceModuleNames(void)
void UpdateTraceLevel(void * /*NotUsed*/)
{
g_ModuleLogLevel[TraceMD5] = (uint8_t)g_Settings->LoadDword(Debugger_TraceMD5);
g_ModuleLogLevel[TraceThread] = (uint8_t)g_Settings->LoadDword(Debugger_TraceThread);
g_ModuleLogLevel[TracePath] = (uint8_t)g_Settings->LoadDword(Debugger_TracePath);
g_ModuleLogLevel[TraceSettings] = (uint8_t)g_Settings->LoadDword(Debugger_TraceSettings);
g_ModuleLogLevel[TraceUnknown] = (uint8_t)g_Settings->LoadDword(Debugger_TraceUnknown);
g_ModuleLogLevel[TraceAppInit] = (uint8_t)g_Settings->LoadDword(Debugger_TraceAppInit);
@ -99,6 +103,8 @@ void SetupTrace(void)
AddLogModule();
g_Settings->RegisterChangeCB(Debugger_TraceMD5, NULL, (CSettings::SettingChangedFunc)UpdateTraceLevel);
g_Settings->RegisterChangeCB(Debugger_TraceThread, NULL, (CSettings::SettingChangedFunc)UpdateTraceLevel);
g_Settings->RegisterChangeCB(Debugger_TracePath, NULL, (CSettings::SettingChangedFunc)UpdateTraceLevel);
g_Settings->RegisterChangeCB(Debugger_TraceSettings, NULL, (CSettings::SettingChangedFunc)UpdateTraceLevel);
g_Settings->RegisterChangeCB(Debugger_TraceUnknown, NULL, (CSettings::SettingChangedFunc)UpdateTraceLevel);
g_Settings->RegisterChangeCB(Debugger_TraceAppInit, NULL, (CSettings::SettingChangedFunc)UpdateTraceLevel);
@ -128,6 +134,8 @@ void CleanupTrace(void)
WriteTrace(TraceAppCleanup, TraceDebug, "Done");
g_Settings->UnregisterChangeCB(Debugger_TraceMD5, NULL, (CSettings::SettingChangedFunc)UpdateTraceLevel);
g_Settings->UnregisterChangeCB(Debugger_TraceThread, NULL, (CSettings::SettingChangedFunc)UpdateTraceLevel);
g_Settings->UnregisterChangeCB(Debugger_TracePath, NULL, (CSettings::SettingChangedFunc)UpdateTraceLevel);
g_Settings->UnregisterChangeCB(Debugger_TraceSettings, NULL, (CSettings::SettingChangedFunc)UpdateTraceLevel);
g_Settings->UnregisterChangeCB(Debugger_TraceUnknown, NULL, (CSettings::SettingChangedFunc)UpdateTraceLevel);
g_Settings->UnregisterChangeCB(Debugger_TraceAppInit, NULL, (CSettings::SettingChangedFunc)UpdateTraceLevel);

View File

@ -217,6 +217,8 @@ enum SettingID
//Trace
Debugger_TraceMD5,
Debugger_TraceThread,
Debugger_TracePath,
Debugger_TraceSettings,
Debugger_TraceUnknown,
Debugger_TraceAppInit,

View File

@ -293,6 +293,8 @@ void CSettings::AddHowToHandleSetting(const char * BaseDirectory)
//Logging
AddHandler(Debugger_TraceMD5, new CSettingTypeApplication("Logging", "MD5", (uint32_t)g_ModuleLogLevel[TraceMD5]));
AddHandler(Debugger_TraceThread, new CSettingTypeApplication("Logging", "Thread", (uint32_t)g_ModuleLogLevel[TraceThread]));
AddHandler(Debugger_TracePath, new CSettingTypeApplication("Logging", "Path", (uint32_t)g_ModuleLogLevel[TracePath]));
AddHandler(Debugger_TraceSettings, new CSettingTypeApplication("Logging", "Settings", (uint32_t)g_ModuleLogLevel[TraceSettings]));
AddHandler(Debugger_TraceUnknown, new CSettingTypeApplication("Logging", "Unknown", (uint32_t)g_ModuleLogLevel[TraceUnknown]));
AddHandler(Debugger_TraceAppInit, new CSettingTypeApplication("Logging", "App Init", (uint32_t)g_ModuleLogLevel[TraceAppInit]));