Initial commit
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
#include <windows.h>
|
||||
//#include <process.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include "SimpleMutex.h"
|
||||
//#include "GlobalUnit.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
//CSema
|
||||
CSema::CSema(const char *sName, unsigned int nInit, bool bCreate)
|
||||
{
|
||||
m_bCreate = bCreate;
|
||||
if (!m_bCreate)
|
||||
return;
|
||||
|
||||
m_hSema = CreateSemaphore(NULL,nInit,0xffff,NULL);
|
||||
if (!m_hSema){
|
||||
char txt[256];
|
||||
sprintf(txt, "Create semaphore error, errno=%d\n", errno);
|
||||
OutputDebugStringA( txt );
|
||||
}
|
||||
}
|
||||
|
||||
CSema::~CSema()
|
||||
{
|
||||
if (!m_bCreate)
|
||||
return;
|
||||
|
||||
if( !CloseHandle(m_hSema) ){
|
||||
char txt[256];
|
||||
sprintf(txt, "Close semaphore error, errno=%d\n", errno);
|
||||
OutputDebugStringA( txt );
|
||||
}
|
||||
}
|
||||
|
||||
int CSema::ActP(DWORD dwTimeout)
|
||||
{
|
||||
if (!m_bCreate)
|
||||
return -1;
|
||||
|
||||
int ret = WaitForSingleObject(m_hSema, dwTimeout);
|
||||
if( ret==WAIT_TIMEOUT ){
|
||||
OutputDebugStringA("WaitforSingleObject timeout!\n");
|
||||
return 1;
|
||||
}
|
||||
else if( ret!=0 ){
|
||||
char txt[256];
|
||||
sprintf(txt, "sema wait error, errno=%d\n", errno);
|
||||
OutputDebugStringA( txt );
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void CSema::ActV()
|
||||
{
|
||||
if (!m_bCreate)
|
||||
return;
|
||||
|
||||
if (!ReleaseSemaphore(m_hSema,1,NULL)){
|
||||
char txt[256];
|
||||
sprintf(txt, "sema release error, errno=%d\n", errno);
|
||||
OutputDebugStringA( txt );
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
//CMutextLock
|
||||
CMutexLock::CMutexLock(bool bCreate)
|
||||
{
|
||||
m_psmMut = new CSema( NULL, 1, bCreate );
|
||||
|
||||
m_psmMut->ActP();
|
||||
}
|
||||
|
||||
CMutexLock::CMutexLock(CSema * pSema, DWORD dwTimeOut)
|
||||
{
|
||||
m_psmMut = pSema;
|
||||
|
||||
m_psmMut->ActP(dwTimeOut);
|
||||
}
|
||||
|
||||
CMutexLock::~CMutexLock()
|
||||
{
|
||||
if( m_psmMut!=NULL )
|
||||
m_psmMut->ActV();
|
||||
}
|
||||
////////////////////////////////////////////////////////////////////
|
||||
Reference in New Issue
Block a user