00001 #pragma once
00002 #ifndef AXPIPE_CSYNC_H
00003 #define AXPIPE_CSYNC_H
00004
00038 #include "AxPipe.h"
00039 #include "AxPipeAssert.h"
00040
00041 namespace AxPipe {
00045 class CSync {
00046 private:
00047 HANDLE m_hEvent;
00048 public:
00049 CSync();
00050 ~CSync();
00051 bool Wait(int iMs = -1);
00052 bool Signal();
00053 };
00076 class CThreadSync {
00077 HANDLE m_hSemaphore;
00078 CSync m_Work,
00079 m_Accepted;
00080 public:
00081 CThreadSync();
00082 ~CThreadSync();
00083 void WorkStart();
00084 void WorkSignal();
00085 void WorkWait();
00086 void WorkEnd();
00087 };
00088
00090 template <class T> class CThreadMain : public T {
00091 DWORD m_dwThreadId;
00092 HANDLE m_hThread;
00093 private:
00097 static DWORD WINAPI Main(LPVOID lpParam) {
00098 return ((CThreadMain<T> *)lpParam)->Main();
00099 }
00100
00107 virtual int Main() {
00108 ASSCHK(false, _T("Override of 'virtual int Main()' missing"));
00109 return 0;
00110 }
00111 public:
00116 CThreadMain() {
00117
00118
00119 int iCurPrio = GetThreadPriority(GetCurrentThread());
00120 ASSAPI((m_hThread = CreateThread(NULL, 0, Main, this, CREATE_SUSPENDED, &m_dwThreadId))!= NULL);
00121 SetThreadPriority(m_hThread, iCurPrio);
00122 }
00123
00125 virtual ~CThreadMain() {
00126 ASSAPI(WaitForSingleObject(m_hThread, INFINITE) == WAIT_OBJECT_0);
00127 ASSAPI(CloseHandle(m_hThread));
00128 }
00129
00131 void Run() {
00132 ResumeThread(m_hThread);
00133 }
00134
00136 void Wait() {
00137 WaitForSingleObject(m_hThread, INFINITE);
00138 }
00139 };
00140 };
00141 #endif AXPIPE_CSYNC_H