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 
32 using namespace OpenZWave;
33 
34 //-----------------------------------------------------------------------------
35 //	<MutexImpl::MutexImpl>
36 //	Constructor
37 //-----------------------------------------------------------------------------
MutexImpl()38 MutexImpl::MutexImpl
39 (
40 ):
41 	m_lockCount( 0 )
42 {
43 	InitializeCriticalSectionEx( &m_criticalSection, 0, 0 );
44 }
45 
46 //-----------------------------------------------------------------------------
47 //	<MutexImpl::~MutexImpl>
48 //	Destructor
49 //-----------------------------------------------------------------------------
~MutexImpl()50 MutexImpl::~MutexImpl
51 (
52 )
53 {
54 	DeleteCriticalSection( &m_criticalSection );
55 }
56 
57 //-----------------------------------------------------------------------------
58 //	<MutexImpl::Lock>
59 //	Lock the mutex
60 //-----------------------------------------------------------------------------
Lock(bool const _bWait)61 bool MutexImpl::Lock
62 (
63 	bool const _bWait // = true;
64 )
65 {
66 	if( _bWait )
67 	{
68 		// We will wait for the lock
69 		EnterCriticalSection( &m_criticalSection );
70 		++m_lockCount;
71 		return true;
72 	}
73 
74 	// Returns immediately, even if the lock was not available.
75 	if( TryEnterCriticalSection( &m_criticalSection ) )
76 	{
77 		++m_lockCount;
78 		return true;
79 	}
80 
81 	return false;
82 }
83 
84 //-----------------------------------------------------------------------------
85 //	<MutexImpl::Unlock>
86 //	Release our lock on the mutex
87 //-----------------------------------------------------------------------------
Unlock()88 void MutexImpl::Unlock
89 (
90 )
91 {
92 	if( !m_lockCount )
93 	{
94 		// No locks - we have a mismatched lock/release pair
95 		assert(0);
96 	}
97 	else
98 	{
99 		--m_lockCount;
100 		LeaveCriticalSection( &m_criticalSection );
101 	}
102 }
103 
104 //-----------------------------------------------------------------------------
105 //	<MutexImpl::IsSignalled>
106 //	Test whether the mutex is free
107 //-----------------------------------------------------------------------------
IsSignalled()108 bool MutexImpl::IsSignalled
109 (
110 )
111 {
112 	return( 0 == m_lockCount );
113 }
114 
115