Some clean-up and simplification for JobObject class.
[MUtilities.git] / src / CriticalSection_Win32.h
blob82e9f9b696d60b00a64d546cf32deb58160d4660
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2016 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_locked(false),
82 m_criticalSection(criticalSection)
84 m_criticalSection.enter();
85 m_locked = true;
88 inline ~CSLocker(void)
90 forceUnlock();
93 inline void forceUnlock(void)
95 if(m_locked)
97 m_criticalSection.leave();
98 m_locked = false;
101 protected:
102 volatile bool m_locked;
103 CriticalSection &m_criticalSection;