00001 00034 #include "stdafx.h" 00035 00036 namespace AxPipe { 00038 CSync::CSync() { 00039 m_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); 00040 ASSAPI(m_hEvent != NULL); 00041 } 00042 00044 CSync::~CSync() { 00045 ASSAPI(CloseHandle(m_hEvent)); 00046 } 00047 00051 bool 00052 CSync::Wait(int iMs) { 00053 DWORD dwStatus = WaitForSingleObject(m_hEvent, iMs == -1 ? INFINITE : (DWORD)iMs); 00054 ASSAPI(dwStatus != WAIT_FAILED); 00055 return dwStatus == WAIT_OBJECT_0; 00056 } 00057 00061 bool 00062 CSync::Signal() { 00063 ASSAPI(SetEvent(m_hEvent)); 00064 return true; 00065 } 00066 00067 CThreadSync::CThreadSync() { 00068 ASSAPI((m_hSemaphore = CreateSemaphore(NULL, 1, 1, NULL)) != NULL); 00069 } 00070 00071 CThreadSync::~CThreadSync() { 00072 ASSAPI(CloseHandle(m_hSemaphore)); 00073 } 00075 void 00076 CThreadSync::WorkStart() { 00077 WaitForSingleObject(m_hSemaphore, INFINITE); 00078 } 00079 00081 void 00082 CThreadSync::WorkSignal() { 00083 m_Work.Signal(); m_Accepted.Wait(); 00084 } 00085 00087 void 00088 CThreadSync::WorkWait() { 00089 m_Work.Wait(); m_Accepted.Signal(); 00090 } 00091 00093 void 00094 CThreadSync::WorkEnd() { 00095 ReleaseSemaphore(m_hSemaphore, 1, NULL); 00096 } 00097 };