| 1 |
#ifndef _PROTO_THREAD |
| 2 |
#define _PROTO_THREAD |
| 3 |
|
| 4 |
#ifdef WIN32 |
| 5 |
#include <processthreadsapi.h> |
| 6 |
#include <synchapi.h> |
| 7 |
#else |
| 8 |
#include <pthread.h> |
| 9 |
#endif // if/else WIN32/UNIX |
| 10 |
|
| 11 |
class ProtoMutex |
| 12 |
{ |
| 13 |
public: |
| 14 |
ProtoMutex(bool init = true); |
| 15 |
~ProtoMutex(); |
| 16 |
|
| 17 |
void Init(); |
| 18 |
void Destroy(); |
| 19 |
void Lock(); |
| 20 |
void Unlock(); |
| 21 |
// TBD - add TryLock() call? |
| 22 |
|
| 23 |
private: |
| 24 |
#ifdef WIN32 |
| 25 |
CRITICAL_SECTION mutex; |
| 26 |
#else |
| 27 |
pthread_mutex_t mutex; |
| 28 |
#endif // if/else WIN32/UNIX |
| 29 |
}; // end class ProtoMutex |
| 30 |
|
| 31 |
class ProtoThread |
| 32 |
{ |
| 33 |
public: |
| 34 |
#ifdef WIN32 |
| 35 |
typedef DWORD ThreadId; |
| 36 |
static ThreadId GetCurrentThread() |
| 37 |
{return ::GetCurrentThreadId();} |
| 38 |
#else |
| 39 |
typedef pthread_t ThreadId; |
| 40 |
static ThreadId GetCurrentThread() |
| 41 |
{return pthread_self();} |
| 42 |
#endif // if/else WIN32 |
| 43 |
|
| 44 |
virtual ~ProtoThread(); |
| 45 |
// Can use externally created thread but must be within that thread when calling StartThread() with non-NULL threadId |
| 46 |
bool StartThread(ThreadId threadId = (ThreadId)NULL); // creates thread if needed and enters its "Run()" method |
| 47 |
virtual int RunThread() = 0; // required override, must return "exit_code" |
| 48 |
virtual void StopThread(int exitCode = 0) // probably will need to override to break any RunThread() loop |
| 49 |
{ |
| 50 |
CloseThread(exitCode); // any overridden StopThread() method must call CloseThread() to wrap things up properly |
| 51 |
} |
| 52 |
|
| 53 |
bool IsStarted() const |
| 54 |
{return ((ThreadId)NULL != thread_id);} |
| 55 |
|
| 56 |
bool IsRunning() const |
| 57 |
{return thread_running;} |
| 58 |
|
| 59 |
bool IsMyself() |
| 60 |
{return (GetCurrentThread() == thread_id);} |
| 61 |
|
| 62 |
int GetExitCode() const |
| 63 |
{return exit_code;} |
| 64 |
|
| 65 |
protected: |
| 66 |
ProtoThread(); |
| 67 |
void CloseThread(int exitCode); // subclasses should override this and call this method to clean up |
| 68 |
|
| 69 |
private: |
| 70 |
#ifdef WIN32 |
| 71 |
typedef DWORD ExitStatus; |
| 72 |
ExitStatus GetExitStatus() // pthread uses a _pointer_ to the status value location |
| 73 |
{return &exit_status;} |
| 74 |
static unsigned int __stdcall DoThreadStart(void* lpParameter); |
| 75 |
static void DoThreadExit(ExitStatus exitStatus) |
| 76 |
{ExitThread(exitStatus);} |
| 77 |
#else |
| 78 |
typedef void* ExitStatus; |
| 79 |
ExitStatus GetExitStatus() // pthread uses a _pointer_ to the status value location |
| 80 |
{return &exit_status;} |
| 81 |
static void* DoThreadStart(void* arg); |
| 82 |
static void DoThreadExit(ExitStatus exitStatus) |
| 83 |
{pthread_exit(exitStatus);} |
| 84 |
#endif // if/else WIN32 |
| 85 |
|
| 86 |
ThreadId thread_id; |
| 87 |
bool external_thread; |
| 88 |
bool thread_running; |
| 89 |
int exit_code; |
| 90 |
int exit_status; |
| 91 |
|
| 92 |
}; // end class ProtoThread |
| 93 |
|
| 94 |
#endif // !_PROTO_THREAD |