1 //-----------------------------------------------------------------------------
2 //
3 //	MutexImpl.cpp
4 //
5 //	WinRT implementation of the cross-platform mutex
6 //
7 //	Copyright (c) 2015 Microsoft Corporation
8 //	All rights reserved.
9 //
10 //	SOFTWARE NOTICE AND LICENSE
11 //
12 //	This file is part of OpenZWave.
13 //
14 //	OpenZWave is free software: you can redistribute it and/or modify
15 //	it under the terms of the GNU Lesser General Public License as published
16 //	by the Free Software Foundation, either version 3 of the License,
17 //	or (at your option) any later version.
18 //
19 //	OpenZWave is distributed in the hope that it will be useful,
20 //	but WITHOUT ANY WARRANTY; without even the implied warranty of
21 //	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 //	GNU Lesser General Public License for more details.
23 //
24 //	You should have received a copy of the GNU Lesser General Public License
25 //	along with OpenZWave.  If not, see <http://www.gnu.org/licenses/>.
26 //
27 //-----------------------------------------------------------------------------
28 #include "Defs.h"
29 #include "MutexImpl.h"
30 
31 namespace OpenZWave
32 {
33 	namespace Internal
34 	{
35 		namespace Platform
36 		{
37 
38 //-----------------------------------------------------------------------------
39 //	<MutexImpl::MutexImpl>
40 //	Constructor
41 //-----------------------------------------------------------------------------
MutexImpl()42 			MutexImpl::MutexImpl() :
43 					m_lockCount(0)
44 			{
45 				InitializeCriticalSectionEx(&m_criticalSection, 0, 0);
46 			}
47 
48 //-----------------------------------------------------------------------------
49 //	<MutexImpl::~MutexImpl>
50 //	Destructor
51 //-----------------------------------------------------------------------------
~MutexImpl()52 			MutexImpl::~MutexImpl()
53 			{
54 				DeleteCriticalSection(&m_criticalSection);
55 			}
56 
57 //-----------------------------------------------------------------------------
58 //	<MutexImpl::Lock>
59 //	Lock the mutex
60 //-----------------------------------------------------------------------------
Lock(bool const _bWait)61 			bool MutexImpl::Lock(bool const _bWait // = true;
62 					)
63 			{
64 				if (_bWait)
65 				{
66 					// We will wait for the lock
67 					EnterCriticalSection(&m_criticalSection);
68 					++m_lockCount;
69 					return true;
70 				}
71 
72 				// Returns immediately, even if the lock was not available.
73 				if (TryEnterCriticalSection(&m_criticalSection))
74 				{
75 					++m_lockCount;
76 					return true;
77 				}
78 
79 				return false;
80 			}
81 
82 //-----------------------------------------------------------------------------
83 //	<MutexImpl::Unlock>
84 //	Release our lock on the mutex
85 //-----------------------------------------------------------------------------
Unlock()86 			void MutexImpl::Unlock()
87 			{
88 				if (!m_lockCount)
89 				{
90 					// No locks - we have a mismatched lock/release pair
91 					assert(0);
92 				}
93 				else
94 				{
95 					--m_lockCount;
96 					LeaveCriticalSection(&m_criticalSection);
97 				}
98 			}
99 
100 //-----------------------------------------------------------------------------
101 //	<MutexImpl::IsSignalled>
102 //	Test whether the mutex is free
103 //-----------------------------------------------------------------------------
IsSignalled()104 			bool MutexImpl::IsSignalled()
105 			{
106 				return (0 == m_lockCount);
107 			}
108 		} // namespace Platform
109 	} // namespace Internal
110 } // namespace OpenZWave
111