xref: /reactos/sdk/lib/atl/atlsync.h (revision 1734f297)
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 
18 class CCriticalSection : public CRITICAL_SECTION
19 {
20     CCriticalSection()
21     {
22         ::InitializeCriticalSection(this);
23     }
24 
25     ~CCriticalSection()
26     {
27         ::DeleteCriticalSection(this);
28     }
29 
30     void Enter()
31     {
32         ::EnterCriticalSection(this);
33     }
34 
35     void Leave()
36     {
37         ::LeaveCriticalSection(this);
38     }
39 
40     BOOL TryEnter()
41     {
42         return ::TryEnterCriticalSection(this);
43     }
44 };
45 
46 class CEvent : public CHandle
47 {
48     CEvent()
49     {
50     }
51 
52     CEvent(CEvent& hEvent) : CHandle(hEvent)
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 
66     explicit CEvent(HANDLE hEvent) : CHandle(hEvent)
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 
86     BOOL Set()
87     {
88         ATLASSERT(*this);
89         return ::SetEvent(*this);
90     }
91 
92     BOOL Reset()
93     {
94         ATLASSERT(*this);
95         return ::ResetEvent(*this);
96     }
97 
98     BOOL Pulse()
99     {
100         ATLASSERT(*this);
101         return ::PulseEvent(*this);
102     }
103 };
104 
105 class CMutex : public CHandle
106 {
107     CMutex()
108     {
109     }
110 
111     CMutex(CMutex& hMutex) : CHandle(hMutex)
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 
125     explicit CMutex(HANDLE hMutex) : CHandle(hMutex)
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 
145     BOOL Release()
146     {
147         ATLASSERT(*this);
148         return ::ReleaseMutex(*this);
149     }
150 };
151 
152 class CSemaphore : public CHandle
153 {
154     CSemaphore()
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__
202