ReactOS 0.4.15-dev-7931-gfd331f1
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
15namespace ATL
16{
17
19{
21 {
23 }
24
26 {
28 }
29
30 void Enter()
31 {
33 }
34
35 void Leave()
36 {
38 }
39
41 {
42 return ::TryEnterCriticalSection(this);
43 }
44};
45
46class 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);
75 return hEvent != NULL;
76 }
77
79 {
80 HANDLE hEvent = ::OpenEvent(dwAccess, bInheritHandle, pszName);
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
105class 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);
133 Attach(hMutex);
134 return hMutex != NULL;
135 }
136
138 {
139 HANDLE hMutex = ::OpenMutex(dwAccess, bInheritHandle, pszName);
141 Attach(hMutex);
142 return hMutex != NULL;
143 }
144
146 {
147 ATLASSERT(*this);
148 return ::ReleaseMutex(*this);
149 }
150};
151
152class CSemaphore : public CHandle
153{
155 {
156 }
157
158 CSemaphore(CSemaphore& hSemaphore) : CHandle(hSemaphore)
159 {
160 }
161
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
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 ATLASSERT(x)
Definition: CComVariant.cpp:10
@ Create
Definition: registry.c:563
BOOL Set()
Definition: atlsync.h:86
CEvent(CEvent &hEvent)
Definition: atlsync.h:52
CEvent(BOOL bManualReset, BOOL bInitialState)
Definition: atlsync.h:56
BOOL Create(LPSECURITY_ATTRIBUTES pSecurity, BOOL bManualReset, BOOL bInitialState, LPCTSTR pszName)
Definition: atlsync.h:70
CEvent(LPSECURITY_ATTRIBUTES pSecurity, BOOL bManualReset, BOOL bInitialState, LPCTSTR pszName)
Definition: atlsync.h:61
BOOL Open(DWORD dwAccess, BOOL bInheritHandle, LPCTSTR pszName)
Definition: atlsync.h:78
BOOL Reset()
Definition: atlsync.h:92
BOOL Pulse()
Definition: atlsync.h:98
CEvent(HANDLE hEvent)
Definition: atlsync.h:66
void Attach(_In_ HANDLE handle)
Definition: atlbase.h:350
BOOL Create(LPSECURITY_ATTRIBUTES pSecurity, BOOL bInitialOwner, LPCTSTR pszName)
Definition: atlsync.h:129
CMutex(LPSECURITY_ATTRIBUTES pSecurity, BOOL bInitialOwner, LPCTSTR pszName)
Definition: atlsync.h:120
CMutex(CMutex &hMutex)
Definition: atlsync.h:111
CMutex(HANDLE hMutex)
Definition: atlsync.h:125
CMutex(BOOL bInitialOwner)
Definition: atlsync.h:115
BOOL Release()
Definition: atlsync.h:145
BOOL Open(DWORD dwAccess, BOOL bInheritHandle, LPCTSTR pszName)
Definition: atlsync.h:137
BOOL Open(DWORD dwAccess, BOOL bInheritHandle, LPCTSTR pszName)
Definition: atlsync.h:184
CSemaphore(LPSECURITY_ATTRIBUTES pSecurity, LONG nInitialCount, LONG nMaxCount, LPCTSTR pszName)
Definition: atlsync.h:167
CSemaphore(LONG nInitialCount, LONG nMaxCount)
Definition: atlsync.h:162
CSemaphore(HANDLE hSemaphore)
Definition: atlsync.h:172
BOOL Create(LPSECURITY_ATTRIBUTES pSecurity, LONG nInitialCount, LONG nMaxCount, LPCTSTR pszName)
Definition: atlsync.h:176
BOOL Release(LONG nReleaseCount=1, LPLONG pnOldCount=NULL)
Definition: atlsync.h:192
CSemaphore(CSemaphore &hSemaphore)
Definition: atlsync.h:158
#define NULL
Definition: types.h:112
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
HANDLE hMutex
Definition: mutex.c:11
static HANDLE hEvent
Definition: comm.c:54
static BOOL bInheritHandle
Definition: pipe.c:82
Definition: rosdlgs.h:6
long LONG
Definition: pedump.c:60
VOID WINAPI InitializeCriticalSection(OUT LPCRITICAL_SECTION lpCriticalSection)
Definition: synch.c:751
int32_t * LPLONG
Definition: typedefs.h:58
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
#define CreateSemaphore
Definition: winbase.h:3760
#define OpenMutex
Definition: winbase.h:3888
#define CreateEvent
Definition: winbase.h:3748
#define CreateMutex
Definition: winbase.h:3756
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
#define OpenSemaphore
Definition: winbase.h:3889
void WINAPI DeleteCriticalSection(PCRITICAL_SECTION)
#define OpenEvent
Definition: winbase.h:3885
_In_ int nMaxCount
Definition: winuser.h:4877
const CHAR * LPCTSTR
Definition: xmlstorage.h:193