00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "stdafx.h"
00037 #include "Win32Semaphore.h"
00038
00039 #include <string>
00040
00041 #ifdef _MEMORY_DEBUG
00042 #define new DEBUG_NEW
00043 #define malloc DEBUG_MALLOC
00044 static char THIS_FILE[] = __FILE__;
00045 #endif
00046
00047 KOMODIA_NAMESPACE_START
00048
00049 CWin32Semaphore::CWin32Semaphore(long lInitialCount,
00050 long lMaxCount,
00051 const char* pSemaphoreName) : CGenericSemaphore(lInitialCount,
00052 lMaxCount,
00053 pSemaphoreName),
00054 m_hSemaphore(0),
00055 m_bDontDelete(FALSE)
00056 {
00057
00058 m_hSemaphore=CreateSemaphore(NULL,
00059 lInitialCount,
00060 lMaxCount,
00061 pSemaphoreName);
00062
00063
00064 if (!m_hSemaphore)
00065 throw std::string("Failed to create semaphore!");
00066
00067
00068 if (pSemaphoreName &&
00069 GetLastError()!=ERROR_ALREADY_EXISTS)
00070 SetFirst();
00071 }
00072
00073 CWin32Semaphore::~CWin32Semaphore()
00074 {
00075 if (m_hSemaphore)
00076
00077 CloseHandle(m_hSemaphore);
00078 }
00079
00080 BOOL CWin32Semaphore::Aquire(unsigned long ulMSTimeout)
00081 {
00082 DWORD dwWaitResult;
00083
00084
00085 dwWaitResult=WaitForSingleObject(m_hSemaphore,
00086 ulMSTimeout);
00087
00088
00089 return dwWaitResult==WAIT_TIMEOUT;
00090 }
00091
00092 long CWin32Semaphore::Release(long lAmount)
00093 {
00094 long lOld;
00095 BOOL bResult;
00096
00097
00098 bResult=ReleaseSemaphore(m_hSemaphore,
00099 lAmount,
00100 &lOld);
00101
00102
00103 if (bResult)
00104 return lOld;
00105 else
00106 return -1;
00107 }
00108
00109 HANDLE CWin32Semaphore::GetHandle()const
00110 {
00111 return m_hSemaphore;
00112 }
00113
00114 void CWin32Semaphore::ReleaseObject()
00115 {
00116 Release();
00117 }
00118
00119 KOMODIA_NAMESPACE_END