Updated VS2013 project file.
[MUtilities.git] / src / CriticalSection_Win32.h
blobf0ae0a029d848ab2773445ec37a5a0b1ac724b68
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2017 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.
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
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
22 #pragma once
24 //Win32 API
25 #ifndef _INC_WINDOWS
26 #define WIN32_LEAN_AND_MEAN 1
27 #include <Windows.h>
28 #endif //_INC_WINDOWS
30 ///////////////////////////////////////////////////////////////////////////////
31 // CRITICAL SECTION
32 ///////////////////////////////////////////////////////////////////////////////
34 namespace MUtils
36 namespace Internal
39 * wrapper for native Win32 critical sections
41 class CriticalSection
43 public:
44 inline CriticalSection(void)
46 InitializeCriticalSection(&m_win32criticalSection);
49 inline ~CriticalSection(void)
51 DeleteCriticalSection(&m_win32criticalSection);
54 inline void enter(void)
56 EnterCriticalSection(&m_win32criticalSection);
59 inline bool tryEnter(void)
61 return TryEnterCriticalSection(&m_win32criticalSection);
64 inline void leave(void)
66 LeaveCriticalSection(&m_win32criticalSection);
69 protected:
70 CRITICAL_SECTION m_win32criticalSection;
74 * RAII-style critical section locker
76 class CSLocker
78 public:
79 inline CSLocker(CriticalSection &criticalSection)
81 m_criticalSection(criticalSection)
83 m_criticalSection.enter();
84 m_locked.ref();
87 inline ~CSLocker(void)
89 forceUnlock();
92 inline void forceUnlock(void)
94 if (m_locked.fetchAndStoreOrdered(0) > 0)
96 m_criticalSection.leave();
99 protected:
100 QAtomicInt m_locked;
101 CriticalSection &m_criticalSection;