6 #ifndef _INC_cSemaphore_H
7 #define _INC_cSemaphore_H
12 #include "../GrayLibBase.h"
17 #elif defined(__linux__)
18 #include <semaphore.h>
48 m_sem.
AttachHandle(
_FNF(::CreateSemaphore)(
nullptr, iUnlockedCount, iMaxCount, pszName));
50 if (pszName !=
nullptr)
52 m_pSem = ::sem_open(pszName, 0);
56 ::sem_init(&m_sem, iUnlockedCount, iMaxCount);
67 #if defined(_WIN32) && ! defined(UNDER_CE)
68 cSemaphore(DWORD dwDesiredAccess,
bool bInheritHandle,
const FILECHAR_t* pszName)
71 OpenSemaphore(dwDesiredAccess, bInheritHandle, pszName);
86 return m_pSem !=
nullptr;
89 #if defined(_WIN32) && ! defined(UNDER_CE)
90 void OpenSemaphore(DWORD dwDesiredAccess,
bool bInheritHandle,
const FILECHAR_t* pszName)
93 m_sem.
AttachHandle(
_FNF(::OpenSemaphore)(dwDesiredAccess, bInheritHandle, pszName));
101 DWORD dwRet = ::WaitForSingleObject(m_sem, tWait);
102 HRESULT hRes = HResult::FromWaitRet(dwRet);
105 cTimeSpec tWaitSpec(tWait);
106 int iRet = ::sem_timedwait(m_pSem, &tWaitSpec);
110 bool Unlock(LONG nReleaseCount = 1, LONG* plPreviousCount =
nullptr)
114 return ::ReleaseSemaphore(m_sem, nReleaseCount, plPreviousCount);
#define _FNF(c)
_WIN32 name has a A or W for UTF8 or UNICODE
Definition: FileName.h:24
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define SUCCEEDED(x)
Definition: HResult.h:29
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
Definition: cSemaphore.h:123
cSemaphoreLock(cSemaphore &Sem)
Definition: cSemaphore.h:131
bool isLocked() const
Definition: cSemaphore.h:150
~cSemaphoreLock()
Definition: cSemaphore.h:142
bool LockWait(TIMESYSD_t tWait=cTimeSys::k_INF)
Definition: cSemaphore.h:154
cSemaphoreLock(cSemaphore &Sem, TIMESYSD_t tWait)
Definition: cSemaphore.h:136
Definition: cSemaphore.h:26
~cSemaphore()
Definition: cSemaphore.h:75
bool isValidSemaphore() const
Definition: cSemaphore.h:81
bool Unlock(LONG nReleaseCount=1, LONG *plPreviousCount=nullptr)
Definition: cSemaphore.h:110
cSemaphore(int iUnlockedCount, int iMaxCount, const FILECHAR_t *pszName=nullptr)
Definition: cSemaphore.h:42
bool LockWait(TIMESYSD_t tWait=cTimeSys::k_INF)
Definition: cSemaphore.h:96
sem_t m_sem
Unnamed semaphore.
Definition: cSemaphore.h:38
sem_t * m_pSem
pthread semaphore.
Definition: cSemaphore.h:39
Definition: cOSHandle.h:59
void AttachHandle(HANDLE h) noexcept
Definition: cOSHandle.h:163
bool isValidHandle() const noexcept
Definition: cOSHandle.h:125
static const TIMESYS_t k_INF
INFINITE in _WIN32. MAILSLOT_WAIT_FOREVER.
Definition: cTimeSys.h:101
INT32 TIMESYSD_t
Time delta. signed milli-Seconds Span. cTimeSys::k_DMAX, cTimeSys::k_INF = MAILSLOT_WAIT_FOREVER.
Definition: cTimeSys.h:28
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22