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 {
00056
00057 m_hSemaphore=CreateSemaphore(NULL,
00058 lInitialCount,
00059 lMaxCount,
00060 pSemaphoreName);
00061
00062
00063 if (!m_hSemaphore)
00064 throw std::string("Failed to create semaphore!");
00065
00066
00067 if (pSemaphoreName &&
00068 GetLastError()!=ERROR_ALREADY_EXISTS)
00069 SetFirst();
00070 }
00071
00072 CWin32Semaphore::CWin32Semaphore(long lInitialCount,
00073 long lMaxCount,
00074 const char* pSemaphoreName,
00075 bool bLater) : CGenericSemaphore(lInitialCount,
00076 lMaxCount,
00077 pSemaphoreName),
00078 m_hSemaphore(NULL)
00079
00080 {
00081 }
00082
00083 CWin32Semaphore::~CWin32Semaphore()
00084 {
00085 if (m_hSemaphore)
00086
00087 CloseHandle(m_hSemaphore);
00088 }
00089
00090 void CWin32Semaphore::SetHandle(HANDLE hSemaphore)
00091 {
00092 m_hSemaphore=hSemaphore;
00093 }
00094
00095 BOOL CWin32Semaphore::Aquire(unsigned long ulMSTimeout)
00096 {
00097 DWORD dwWaitResult;
00098
00099
00100 dwWaitResult=WaitForSingleObject(m_hSemaphore,
00101 ulMSTimeout);
00102
00103
00104 return dwWaitResult==WAIT_TIMEOUT;
00105 }
00106
00107 long CWin32Semaphore::Release(long lAmount)
00108 {
00109 long lOld;
00110 BOOL bResult;
00111
00112
00113 bResult=ReleaseSemaphore(m_hSemaphore,
00114 lAmount,
00115 &lOld);
00116
00117
00118 if (bResult)
00119 return lOld;
00120 else
00121 return -1;
00122 }
00123
00124 HANDLE CWin32Semaphore::GetHandle()const
00125 {
00126 return m_hSemaphore;
00127 }
00128
00129 void CWin32Semaphore::ReleaseObject()
00130 {
00131 Release();
00132 }
00133
00134 KOMODIA_NAMESPACE_END