This isn't used by the SMXManager thread for now. This also separates out SMX::Event for thread event handling.master
parent
a7cb6ad51d
commit
d576545266
@ -0,0 +1,49 @@ |
||||
#include "SMXThread.h" |
||||
|
||||
using namespace std; |
||||
using namespace SMX; |
||||
|
||||
SMXThread::SMXThread(Mutex &lock): |
||||
m_Lock(lock), |
||||
m_Event(lock) |
||||
{ |
||||
} |
||||
|
||||
void SMX::SMXThread::SetHighPriority(bool bHighPriority) |
||||
{ |
||||
if(m_hThread == INVALID_HANDLE_VALUE) |
||||
throw exception("SetHighPriority called while the thread isn't running"); |
||||
|
||||
SetThreadPriority(m_hThread, THREAD_PRIORITY_HIGHEST); |
||||
} |
||||
|
||||
bool SMX::SMXThread::IsCurrentThread() const |
||||
{ |
||||
return GetCurrentThreadId() == m_iThreadId; |
||||
} |
||||
|
||||
void SMXThread::Start(string name) |
||||
{ |
||||
// Start the thread.
|
||||
m_hThread = CreateThread(NULL, 0, ThreadMainStart, this, 0, &m_iThreadId); |
||||
SMX::SetThreadName(m_iThreadId, name); |
||||
} |
||||
|
||||
void SMXThread::Shutdown() |
||||
{ |
||||
m_Lock.AssertNotLockedByCurrentThread(); |
||||
|
||||
// Shut down the thread and wait for it to exit.
|
||||
m_bShutdown = true; |
||||
m_Event.Set(); |
||||
|
||||
WaitForSingleObject(m_hThread, INFINITE); |
||||
m_hThread = INVALID_HANDLE_VALUE; |
||||
} |
||||
|
||||
DWORD WINAPI SMXThread::ThreadMainStart(void *self_) |
||||
{ |
||||
SMXThread *self = (SMXThread *) self_; |
||||
self->ThreadMain(); |
||||
return 0; |
||||
} |
@ -0,0 +1,45 @@ |
||||
#ifndef SMXThread_h |
||||
#define SMXThread_h |
||||
|
||||
// A base class for a thread.
|
||||
#include "Helpers.h" |
||||
#include <string> |
||||
|
||||
namespace SMX |
||||
{ |
||||
|
||||
class SMXThread |
||||
{ |
||||
public: |
||||
SMXThread(SMX::Mutex &lock); |
||||
|
||||
// Raise the priority of the thread.
|
||||
void SetHighPriority(bool bHighPriority); |
||||
|
||||
// Start the thread, giving it a name for debugging.
|
||||
void Start(std::string name); |
||||
|
||||
// Shut down the thread. This function won't return until the thread
|
||||
// has been stopped.
|
||||
void Shutdown(); |
||||
|
||||
// Return true if this is the calling thread.
|
||||
bool IsCurrentThread() const; |
||||
|
||||
// The derived class implements this.
|
||||
virtual void ThreadMain() = 0; |
||||
|
||||
protected: |
||||
static DWORD WINAPI ThreadMainStart(void *self); |
||||
|
||||
SMX::Mutex &m_Lock; |
||||
SMX::Event m_Event; |
||||
bool m_bShutdown = false; |
||||
|
||||
private: |
||||
HANDLE m_hThread = INVALID_HANDLE_VALUE; |
||||
DWORD m_iThreadId = 0; |
||||
}; |
||||
} |
||||
|
||||
#endif |
Loading…
Reference in new issue