49 lines
1.0 KiB
C++
49 lines
1.0 KiB
C++
#include <thread/mutex.hpp>
|
|
#include <thread/postfix.hpp>
|
|
|
|
Mutex::Mutex(void)
|
|
: mhMutex(0), mMutexName("Mutex")
|
|
{
|
|
PostFix postFix;
|
|
postFix.postFix(mMutexName);
|
|
mhMutex=::CreateMutex((LPSECURITY_ATTRIBUTES)0,FALSE,mMutexName);
|
|
}
|
|
|
|
Mutex::Mutex(const String &mutexName,BOOL postFixup)
|
|
: mhMutex(0)
|
|
{
|
|
PostFix postFix;
|
|
if(mutexName.isNull())return;
|
|
mMutexName=mutexName;
|
|
if(postFixup)postFix.postFix(mMutexName);
|
|
mhMutex=::CreateMutex((LPSECURITY_ATTRIBUTES)0,FALSE,mMutexName);
|
|
}
|
|
|
|
WORD Mutex::requestMutex(DWORD waitTime,WORD alertable)const
|
|
{
|
|
DWORD waitResult;
|
|
|
|
if(!isOkay())return FALSE;
|
|
waitResult=::WaitForSingleObjectEx(mhMutex,waitTime,alertable);
|
|
if(WAIT_ABANDONED==waitResult)return FALSE;
|
|
else if(WAIT_TIMEOUT==waitResult)return FALSE;
|
|
else if(WAIT_OBJECT_0==waitResult)return TRUE;
|
|
else return TRUE;
|
|
return TRUE;
|
|
}
|
|
|
|
WORD Mutex::releaseMutex(void)const
|
|
{
|
|
if(!isOkay())return FALSE;
|
|
return ::ReleaseMutex(mhMutex);
|
|
}
|
|
|
|
WORD Mutex::closeMutex(void)
|
|
{
|
|
if(!isOkay())return FALSE;
|
|
::CloseHandle(mhMutex);
|
|
mhMutex=0;
|
|
return TRUE;
|
|
}
|
|
|