2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// When in MSVC, the debugger provides "thread naming" by catching special exceptions. Interface here:
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
//
|
|
|
|
// Usage: SetThreadName (-1, "MainThread");
|
|
|
|
//
|
2014-04-11 07:09:19 -04:00
|
|
|
|
|
|
|
// Code adapted from MSDN: http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
|
|
|
|
|
|
|
|
const DWORD MS_VC_EXCEPTION = 0x406D1388;
|
|
|
|
|
|
|
|
#pragma pack(push, 8)
|
2012-06-14 09:06:06 -04:00
|
|
|
typedef struct tagTHREADNAME_INFO
|
|
|
|
{
|
2014-04-11 07:09:19 -04:00
|
|
|
DWORD dwType; // Must be 0x1000.
|
|
|
|
LPCSTR szName; // Pointer to name (in user addr space).
|
|
|
|
DWORD dwThreadID; // Thread ID (-1 = caller thread).
|
|
|
|
DWORD dwFlags; // Reserved for future use, must be zero.
|
2012-06-14 09:06:06 -04:00
|
|
|
} THREADNAME_INFO;
|
2014-04-11 07:09:19 -04:00
|
|
|
#pragma pack(pop)
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-04-11 07:09:19 -04:00
|
|
|
static void SetThreadName(DWORD dwThreadID, const char * threadName)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
THREADNAME_INFO info;
|
|
|
|
info.dwType = 0x1000;
|
2014-04-11 07:09:19 -04:00
|
|
|
info.szName = threadName;
|
2012-06-14 09:06:06 -04:00
|
|
|
info.dwThreadID = dwThreadID;
|
|
|
|
info.dwFlags = 0;
|
|
|
|
|
|
|
|
__try
|
|
|
|
{
|
2014-04-11 07:09:19 -04:00
|
|
|
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR *)&info);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2014-04-11 07:09:19 -04:00
|
|
|
__except (EXCEPTION_EXECUTE_HANDLER)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // _MSC_VER
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cThread::cThread( ThreadFunc a_ThreadFunction, void* a_Param, const char* a_ThreadName /* = 0 */ )
|
|
|
|
: m_ThreadFunction( a_ThreadFunction )
|
|
|
|
, m_Param( a_Param )
|
|
|
|
, m_Event( new cEvent() )
|
|
|
|
, m_StopEvent( 0 )
|
|
|
|
{
|
|
|
|
if( a_ThreadName )
|
|
|
|
{
|
2012-07-15 16:32:50 -04:00
|
|
|
m_ThreadName.assign(a_ThreadName);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-15 16:32:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
cThread::~cThread()
|
|
|
|
{
|
|
|
|
delete m_Event;
|
2014-06-19 04:49:56 -04:00
|
|
|
m_Event = NULL;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
if( m_StopEvent )
|
|
|
|
{
|
|
|
|
m_StopEvent->Wait();
|
|
|
|
delete m_StopEvent;
|
2014-06-19 04:49:56 -04:00
|
|
|
m_StopEvent = NULL;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-15 16:32:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
void cThread::Start( bool a_bWaitOnDelete /* = true */ )
|
|
|
|
{
|
|
|
|
if( a_bWaitOnDelete )
|
|
|
|
m_StopEvent = new cEvent();
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
pthread_t SndThread;
|
|
|
|
if( pthread_create( &SndThread, NULL, MyThread, this) )
|
|
|
|
LOGERROR("ERROR: Could not create thread!");
|
|
|
|
#else
|
|
|
|
DWORD ThreadID = 0;
|
2014-07-17 17:15:53 -04:00
|
|
|
HANDLE hThread = CreateThread(NULL // security
|
2014-07-19 08:53:41 -04:00
|
|
|
, 0 // stack size
|
2014-07-17 16:15:34 -04:00
|
|
|
, (LPTHREAD_START_ROUTINE) MyThread // function name
|
2014-07-19 08:53:41 -04:00
|
|
|
, this // parameters
|
|
|
|
, 0 // flags
|
|
|
|
, &ThreadID ); // thread id
|
2012-06-14 09:06:06 -04:00
|
|
|
CloseHandle( hThread );
|
|
|
|
|
2012-07-15 16:32:50 -04:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
if (!m_ThreadName.empty())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-07-15 16:32:50 -04:00
|
|
|
SetThreadName(ThreadID, m_ThreadName.c_str());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2012-07-15 16:32:50 -04:00
|
|
|
#endif // _MSC_VER
|
2012-06-14 09:06:06 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Wait until thread has actually been created
|
|
|
|
m_Event->Wait();
|
|
|
|
}
|
|
|
|
|
2012-07-15 16:32:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
unsigned long cThread::MyThread(void* a_Param )
|
|
|
|
#else
|
|
|
|
void *cThread::MyThread( void *a_Param )
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
cThread* self = (cThread*)a_Param;
|
|
|
|
cEvent* StopEvent = self->m_StopEvent;
|
|
|
|
|
|
|
|
ThreadFunc* ThreadFunction = self->m_ThreadFunction;
|
|
|
|
void* ThreadParam = self->m_Param;
|
|
|
|
|
|
|
|
// Set event to let other thread know this thread has been created and it's safe to delete the cThread object
|
|
|
|
self->m_Event->Set();
|
|
|
|
|
|
|
|
ThreadFunction( ThreadParam );
|
|
|
|
|
|
|
|
if( StopEvent ) StopEvent->Set();
|
|
|
|
return 0;
|
|
|
|
}
|