ReactOS  0.4.15-dev-4874-g57c84dd
atlsync.h
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS ATL
3  * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE: ATL Synchronization
5  * COPYRIGHT: Copyright 2022 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
6  */
7 
8 #pragma once
9 
10 #ifndef __ATLSYNC_H__
11 #define __ATLSYNC_H__
12 
13 #include "atlbase.h"
14 
15 namespace ATL
16 {
17 
19 {
21  {
23  }
24 
26  {
28  }
29 
30  void Enter()
31  {
33  }
34 
35  void Leave()
36  {
38  }
39 
41  {
43  }
44 };
45 
46 class CEvent : public CHandle
47 {
49  {
50  }
51 
53  {
54  }
55 
56  CEvent(BOOL bManualReset, BOOL bInitialState)
57  {
58  Create(NULL, bManualReset, bInitialState, NULL);
59  }
60 
61  CEvent(LPSECURITY_ATTRIBUTES pSecurity, BOOL bManualReset, BOOL bInitialState, LPCTSTR pszName)
62  {
63  Create(pSecurity, bManualReset, bInitialState, pszName);
64  }
65 
67  {
68  }
69 
70  BOOL Create(LPSECURITY_ATTRIBUTES pSecurity, BOOL bManualReset, BOOL bInitialState, LPCTSTR pszName)
71  {
72  HANDLE hEvent = ::CreateEvent(pSecurity, bManualReset, bInitialState, pszName);
73  ATLASSERT(hEvent != NULL);
74  Attach(hEvent);
75  return hEvent != NULL;
76  }
77 
78  BOOL Open(DWORD dwAccess, BOOL bInheritHandle, LPCTSTR pszName)
79  {
80  HANDLE hEvent = ::OpenEvent(dwAccess, bInheritHandle, pszName);
81  ATLASSERT(hEvent != NULL);
82  Attach(hEvent);
83  return hEvent != NULL;
84  }
85 
87  {
88  ATLASSERT(*this);
89  return ::SetEvent(*this);
90  }
91 
93  {
94  ATLASSERT(*this);
95  return ::ResetEvent(*this);
96  }
97 
99  {
100  ATLASSERT(*this);
101  return ::PulseEvent(*this);
102  }
103 };
104 
105 class CMutex : public CHandle
106 {
108  {
109  }
110 
112  {
113  }
114 
115  explicit CMutex(BOOL bInitialOwner)
116  {
117  Create(NULL, bInitialOwner, NULL);
118  }
119 
120  CMutex(LPSECURITY_ATTRIBUTES pSecurity, BOOL bInitialOwner, LPCTSTR pszName)
121  {
122  Create(pSecurity, bInitialOwner, pszName);
123  }
124 
126  {
127  }
128 
129  BOOL Create(LPSECURITY_ATTRIBUTES pSecurity, BOOL bInitialOwner, LPCTSTR pszName)
130  {
131  HANDLE hMutex = ::CreateMutex(pSecurity, bInitialOwner, pszName);
132  ATLASSERT(hMutex != NULL);
133  Attach(hMutex);
134  return hMutex != NULL;
135  }
136 
137  BOOL Open(DWORD dwAccess, BOOL bInheritHandle, LPCTSTR pszName)
138  {
139  HANDLE hMutex = ::OpenMutex(dwAccess, bInheritHandle, pszName);
140  ATLASSERT(hMutex != NULL);
141  Attach(hMutex);
142  return hMutex != NULL;
143  }
144 
146  {
147  ATLASSERT(*this);
148  return ::ReleaseMutex(*this);
149  }
150 };
151 
152 class CSemaphore : public CHandle
153 {
155  {
156  }
157 
158  CSemaphore(CSemaphore& hSemaphore) : CHandle(hSemaphore)
159  {
160  }
161 
162  CSemaphore(LONG nInitialCount, LONG nMaxCount)
163  {
164  Create(NULL, nInitialCount, nMaxCount, NULL);
165  }
166 
167  CSemaphore(LPSECURITY_ATTRIBUTES pSecurity, LONG nInitialCount, LONG nMaxCount, LPCTSTR pszName)
168  {
169  Create(pSecurity, nInitialCount, nMaxCount, pszName);
170  }
171 
172  explicit CSemaphore(HANDLE hSemaphore) : CHandle(hSemaphore)
173  {
174  }
175 
176  BOOL Create(LPSECURITY_ATTRIBUTES pSecurity, LONG nInitialCount, LONG nMaxCount, LPCTSTR pszName)
177  {
178  HANDLE hSemaphore = ::CreateSemaphore(pSecurity, nInitialCount, nMaxCount, pszName);
179  ATLASSERT(hSemaphore != NULL);
180  Attach(hSemaphore);
181  return hSemaphore != NULL;
182  }
183 
184  BOOL Open(DWORD dwAccess, BOOL bInheritHandle, LPCTSTR pszName)
185  {
186  HANDLE hSemaphore = ::OpenSemaphore(dwAccess, bInheritHandle, pszName);
187  ATLASSERT(hSemaphore != NULL);
188  Attach(hSemaphore);
189  return hSemaphore != NULL;
190  }
191 
192  BOOL Release(LONG nReleaseCount = 1, LPLONG pnOldCount = NULL)
193  {
194  ATLASSERT(*this);
195  return ::ReleaseSemaphore(*this, nReleaseCount, pnOldCount);
196  }
197 };
198 
199 } // namespace ATL
200 
201 #endif // __ATLSYNC_H__
#define CreateEvent
Definition: winbase.h:3609
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
BOOL Reset()
Definition: atlsync.h:92
#define ATLASSERT(x)
Definition: CComVariant.cpp:10
CMutex(CMutex &hMutex)
Definition: atlsync.h:111
BOOL Pulse()
Definition: atlsync.h:98
CEvent(CEvent &hEvent)
Definition: atlsync.h:52
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:733
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
CSemaphore(LPSECURITY_ATTRIBUTES pSecurity, LONG nInitialCount, LONG nMaxCount, LPCTSTR pszName)
Definition: atlsync.h:167
BOOL Open(DWORD dwAccess, BOOL bInheritHandle, LPCTSTR pszName)
Definition: atlsync.h:137
#define OpenEvent
Definition: winbase.h:3746
BOOL Open(DWORD dwAccess, BOOL bInheritHandle, LPCTSTR pszName)
Definition: atlsync.h:184
BOOL Create(LPSECURITY_ATTRIBUTES pSecurity, LONG nInitialCount, LONG nMaxCount, LPCTSTR pszName)
Definition: atlsync.h:176
static HANDLE hEvent
Definition: comm.c:54
CSemaphore(HANDLE hSemaphore)
Definition: atlsync.h:172
CSemaphore(CSemaphore &hSemaphore)
Definition: atlsync.h:158
static BOOL bInheritHandle
Definition: pipe.c:82
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
BOOL WINAPI DECLSPEC_HOTPATCH PulseEvent(IN HANDLE hEvent)
Definition: synch.c:695
VOID WINAPI InitializeCriticalSection(OUT LPCRITICAL_SECTION lpCriticalSection)
Definition: synch.c:751
BOOL Set()
Definition: atlsync.h:86
HANDLE hMutex
Definition: mutex.c:11
CEvent(HANDLE hEvent)
Definition: atlsync.h:66
Definition: rosdlgs.h:5
#define CreateMutex
Definition: winbase.h:3617
void WINAPI DeleteCriticalSection(PCRITICAL_SECTION)
void Attach(_In_ HANDLE handle)
Definition: atlbase.h:301
#define OpenSemaphore
Definition: winbase.h:3750
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL WINAPI DECLSPEC_HOTPATCH ReleaseMutex(IN HANDLE hMutex)
Definition: synch.c:618
BOOL Create(LPSECURITY_ATTRIBUTES pSecurity, BOOL bInitialOwner, LPCTSTR pszName)
Definition: atlsync.h:129
BOOL WINAPI TryEnterCriticalSection(LPCRITICAL_SECTION)
CSemaphore(LONG nInitialCount, LONG nMaxCount)
Definition: atlsync.h:162
CEvent(BOOL bManualReset, BOOL bInitialState)
Definition: atlsync.h:56
int32_t * LPLONG
Definition: typedefs.h:58
BOOL Open(DWORD dwAccess, BOOL bInheritHandle, LPCTSTR pszName)
Definition: atlsync.h:78
BOOL Create(LPSECURITY_ATTRIBUTES pSecurity, BOOL bManualReset, BOOL bInitialState, LPCTSTR pszName)
Definition: atlsync.h:70
CMutex(HANDLE hMutex)
Definition: atlsync.h:125
CMutex(LPSECURITY_ATTRIBUTES pSecurity, BOOL bInitialOwner, LPCTSTR pszName)
Definition: atlsync.h:120
#define NULL
Definition: types.h:112
BOOL WINAPI DECLSPEC_HOTPATCH ResetEvent(IN HANDLE hEvent)
Definition: synch.c:714
BOOL WINAPI DECLSPEC_HOTPATCH ReleaseSemaphore(IN HANDLE hSemaphore, IN LONG lReleaseCount, IN LPLONG lpPreviousCount)
Definition: synch.c:542
#define OpenMutex
Definition: winbase.h:3749
BOOL Release()
Definition: atlsync.h:145
CMutex(BOOL bInitialOwner)
Definition: atlsync.h:115
#define CreateSemaphore
Definition: winbase.h:3621
BOOL Release(LONG nReleaseCount=1, LPLONG pnOldCount=NULL)
Definition: atlsync.h:192
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
_In_ int nMaxCount
Definition: winuser.h:4857
CEvent(LPSECURITY_ATTRIBUTES pSecurity, BOOL bManualReset, BOOL bInitialState, LPCTSTR pszName)
Definition: atlsync.h:61