OSDN Git Service

Bump version.
[mutilities/MUtilities.git] / src / CriticalSection_Win32.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2019 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 //
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
21
22 #pragma once
23
24 //Win32 API
25 #ifndef _INC_WINDOWS
26 #define WIN32_LEAN_AND_MEAN 1
27 #include <Windows.h>
28 #endif //_INC_WINDOWS
29
30 ///////////////////////////////////////////////////////////////////////////////
31 // CRITICAL SECTION
32 ///////////////////////////////////////////////////////////////////////////////
33
34 namespace MUtils
35 {
36         namespace Internal
37         {
38                 /*
39                  * wrapper for native Win32 critical sections
40                  */
41                 class CriticalSection
42                 {
43                 public:
44                         inline CriticalSection(void)
45                         {
46                                 InitializeCriticalSection(&m_win32criticalSection);
47                         }
48
49                         inline ~CriticalSection(void)
50                         {
51                                 DeleteCriticalSection(&m_win32criticalSection);
52                         }
53
54                         inline void enter(void)
55                         {
56                                 EnterCriticalSection(&m_win32criticalSection);
57                         }
58
59                         inline bool tryEnter(void)
60                         {
61                                 return TryEnterCriticalSection(&m_win32criticalSection);
62                         }
63
64                         inline void leave(void)
65                         {
66                                 LeaveCriticalSection(&m_win32criticalSection);
67                         }
68
69                 protected:
70                         CRITICAL_SECTION m_win32criticalSection;
71                 };
72
73                 /*
74                  * RAII-style critical section locker
75                  */
76                 class CSLocker
77                 {
78                 public:
79                         inline CSLocker(CriticalSection &criticalSection)
80                         :
81                                 m_criticalSection(criticalSection)
82                         {
83                                 m_criticalSection.enter();
84                                 m_locked.ref();
85                         }
86
87                         inline ~CSLocker(void)
88                         {
89                                 forceUnlock();
90                         }
91
92                         inline void forceUnlock(void)
93                         {
94                                 if (m_locked.fetchAndStoreOrdered(0) > 0)
95                                 {
96                                         m_criticalSection.leave();
97                                 }
98                         }
99                 protected:
100                         QAtomicInt m_locked;
101                         CriticalSection &m_criticalSection;
102                 };
103         }
104 }