Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cSysEvent.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cSysEvent_H
7 #define _INC_cSysEvent_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "../GrayLibBase.h"
15 
16 #ifdef __linux__
17 // from sample http://www.llnl.gov/computing/tutorials/pthreads/#Routines
18 #include <pthread.h> // mutex
19 #include <errno.h>
20 struct _SECURITY_ATTRIBUTES;
21 #endif // __linux__
22 
23 namespace GrayLib
24 {
26 
28  {
32 
33  private:
34 #ifdef _WIN32
35  cOSHandle m_h;
36 #else // __linux__
37  static const ::pthread_mutex_t k_mutexInit;
38  static const ::pthread_cond_t k_condInit;
39  ::pthread_mutex_t m_mutex;
40  ::pthread_cond_t m_cond;
41  bool m_bManualReset;
42  bool m_bSignaled;
43 #endif
44 
45  public:
46  cSysEvent() noexcept
47  {
48 #ifdef __linux__
49  m_mutex = k_mutexInit;
50  m_cond = k_condInit;
51  m_bManualReset = false;
52  m_bSignaled = false;
53 #endif
54  }
56  {
57 #ifdef __linux__
58  if (isValidEvent())
59  {
60  ::pthread_cond_destroy(&m_cond);
61  ::pthread_mutex_destroy(&m_mutex);
62  }
63 #endif
64  }
65 
66 #ifdef _WIN32
67  HANDLE get_EventHandle() const noexcept
68  {
69  return m_h;
70  }
71 #elif defined(__linux__)
72  ::pthread_cond_t get_CondHandle() const noexcept
73  {
74  return m_cond;
75  }
76 #else
77 #error NOOS
78 #endif
79 
80  bool isValidEvent() const noexcept
81  {
82 #ifdef _WIN32
83  return m_h.isValidHandle();
84 #else // __linux__
85  return true; // use m_cond ??? or ( m_mutex.__size[0] != 0 )
86 #endif
87  }
88 
89  bool CreateEventX(_SECURITY_ATTRIBUTES* lpEventAttributes = nullptr,
90  bool bManualReset = false, // true = requires use of the ResetEvent function set the state to nonsignaled. eles auto cleared.
91  bool bInitialState = false, // false = wait.
92  const FILECHAR_t* lpName = nullptr)
93  {
94  // Cant use the name "CreateEvent" since it is #defined.
95 
96  ASSERT(!isValidEvent());
97 #ifdef _WIN32
98  m_h.AttachHandle(_FNF(::CreateEvent)(lpEventAttributes, bManualReset, bInitialState, lpName));
99 #else // __linux__
100  m_mutex = k_mutexInit; // NOT recursive!
101  if (::pthread_cond_init(&m_cond, nullptr) != 0)
102  {
103  return false;
104  }
105  m_bManualReset = bManualReset; // must track this manually.
106  m_bSignaled = bInitialState;
107 #endif
108  return isValidEvent();
109  }
110 
111 #if 0
112  bool OpenEvent(const FILECHAR_t* lpName)
113  {
116  m_h.AttachHandle(_FNF(::OpenEvent)(sdf, false, lpName));
117  return isValidEvent() ;
118  }
119 #endif
120 
121  bool SetEvent()
122  {
124  ASSERT(isValidEvent());
125 #ifdef _WIN32
126  return(::SetEvent(m_h) != false);
127 #else // __linux__
128  ::pthread_mutex_lock(&m_mutex);
129  m_bSignaled = m_bManualReset; // only needed to be set if m_bManualReset
130  int iRet = ::pthread_cond_broadcast(&m_cond); // like pthread_cond_signal()
131  ::pthread_mutex_unlock(&m_mutex);
132  return iRet == 0 ;
133 #endif
134  }
135  bool ResetEvent()
136  {
140  ASSERT(isValidEvent());
141 #ifdef _WIN32
142  return(::ResetEvent(m_h) != false);
143 #else // __linux__
144  m_bSignaled = false;
145  return true;
146 #endif
147  }
148  bool PulseEvent()
149  {
151  ASSERT(isValidEvent());
152 #ifdef _WIN32
153  return ::PulseEvent(m_h) != false ;
154 #else // __linux__
155  bool bManualResetOld = m_bManualReset;
156  m_bManualReset = false;
157  SetEvent();
158  m_bManualReset = bManualResetOld;
159  return true;
160 #endif
161  }
162 
163  HRESULT WaitForSingleObject(TIMESYSD_t dwMilliseconds);
164 
166  };
167 }
168 #endif // _INC_cSysEvent_H
#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
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
Definition: cSysEvent.h:28
UNITTEST2_PREDEF(cSysEvent)
bool CreateEventX(_SECURITY_ATTRIBUTES *lpEventAttributes=nullptr, bool bManualReset=false, bool bInitialState=false, const FILECHAR_t *lpName=nullptr)
Definition: cSysEvent.h:89
~cSysEvent()
Definition: cSysEvent.h:55
bool SetEvent()
Definition: cSysEvent.h:121
bool isValidEvent() const noexcept
Definition: cSysEvent.h:80
bool PulseEvent()
Definition: cSysEvent.h:148
bool ResetEvent()
Definition: cSysEvent.h:135
cSysEvent() noexcept
Definition: cSysEvent.h:46
Definition: cOSHandle.h:59
void AttachHandle(HANDLE h) noexcept
Definition: cOSHandle.h:163
bool isValidHandle() const noexcept
Definition: cOSHandle.h:125
Definition: cMesh.h:22
UNITTEST2_PREDEF(cQuadtree)
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