Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cSemaphore.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cSemaphore_H
7 #define _INC_cSemaphore_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "../GrayLibBase.h"
15 #ifdef _WIN32
17 #elif defined(__linux__)
18 #include <semaphore.h>
19 #else
20 #error NOOS
21 #endif
22 
23 namespace GrayLib
24 {
26  {
34  public:
35 #ifdef _WIN32
36  cOSHandle m_sem; // OpenSemaphore
37 #else
38  sem_t m_sem;
39  sem_t* m_pSem;
40 #endif
41  public:
42  cSemaphore(int iUnlockedCount, int iMaxCount, const FILECHAR_t* pszName = nullptr)
43  {
47 #ifdef _WIN32
48  m_sem.AttachHandle(_FNF(::CreateSemaphore)(nullptr, iUnlockedCount, iMaxCount, pszName));
49 #else
50  if (pszName != nullptr)
51  {
52  m_pSem = ::sem_open(pszName, 0); // flags
53  }
54  else
55  {
56  ::sem_init(&m_sem, iUnlockedCount, iMaxCount);
57  m_pSem = &m_sem;
58  }
59 #endif
60  }
61 #ifdef _WIN32
62  cSemaphore()
63  {
65  }
66 #endif
67 #if defined(_WIN32) && ! defined(UNDER_CE)
68  cSemaphore(DWORD dwDesiredAccess, bool bInheritHandle, const FILECHAR_t* pszName)
69  {
71  OpenSemaphore(dwDesiredAccess, bInheritHandle, pszName);
72  }
73 #endif
74 
76  {
77 #ifdef __linux__
78  ::sem_close(m_pSem); // sem_destroy()
79 #endif
80  }
81  bool isValidSemaphore() const
82  {
83 #ifdef _WIN32
84  return m_sem.isValidHandle();
85 #else
86  return m_pSem != nullptr;
87 #endif
88  }
89 #if defined(_WIN32) && ! defined(UNDER_CE)
90  void OpenSemaphore(DWORD dwDesiredAccess, bool bInheritHandle, const FILECHAR_t* pszName)
91  {
93  m_sem.AttachHandle(_FNF(::OpenSemaphore)(dwDesiredAccess, bInheritHandle, pszName));
94  }
95 #endif
97  {
100 #ifdef _WIN32
101  DWORD dwRet = ::WaitForSingleObject(m_sem, tWait);
102  HRESULT hRes = HResult::FromWaitRet(dwRet);
103  return SUCCEEDED(hRes);
104 #else
105  cTimeSpec tWaitSpec(tWait);
106  int iRet = ::sem_timedwait(m_pSem, &tWaitSpec); // errno = ETIMEDOUT
107  return (iRet == 0);
108 #endif
109  }
110  bool Unlock(LONG nReleaseCount = 1, LONG* plPreviousCount = nullptr)
111  {
113 #ifdef _WIN32
114  return ::ReleaseSemaphore(m_sem, nReleaseCount, plPreviousCount);
115 #else
116  ::sem_post(m_pSem);
117  return false;
118 #endif
119  }
120  };
121 
122  class GRAYLIB_LINK cSemaphoreLock // : public cLockerT<>
123  {
126  private:
127  cSemaphore& m_Sem;
128  bool m_bLocked;
129 
130  public:
132  : m_Sem(Sem)
133  , m_bLocked(false)
134  {
135  }
137  : m_Sem(Sem)
138  , m_bLocked(false)
139  {
140  LockWait(tWait);
141  }
143  {
145  if (m_bLocked)
146  {
147  m_Sem.Unlock(1);
148  }
149  }
150  bool isLocked() const
151  {
152  return m_bLocked;
153  }
155  {
157  if (m_bLocked)
158  return true;
159  m_bLocked = m_Sem.LockWait(tWait);
160  return m_bLocked;
161  }
162  };
163 };
164 #endif
#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
Definition: cMesh.h:22
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