mutex.cpp

Engine/source/platformWin32/threads/mutex.cpp

More...

Classes:

Detailed Description

 1
 2//-----------------------------------------------------------------------------
 3// Copyright (c) 2012 GarageGames, LLC
 4//
 5// Permission is hereby granted, free of charge, to any person obtaining a copy
 6// of this software and associated documentation files (the "Software"), to
 7// deal in the Software without restriction, including without limitation the
 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 9// sell copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21// IN THE SOFTWARE.
22//-----------------------------------------------------------------------------
23
24#include "platform/threads/mutex.h"
25#include "platformWin32/platformWin32.h"
26#include "core/util/safeDelete.h"
27
28//-----------------------------------------------------------------------------
29// Mutex Data
30//-----------------------------------------------------------------------------
31
32struct PlatformMutexData
33{
34   CRITICAL_SECTION mCriticalSection;
35};
36
37//-----------------------------------------------------------------------------
38// Constructor/Destructor
39//-----------------------------------------------------------------------------
40
41Mutex::Mutex()
42{
43   mData = new PlatformMutexData;
44   InitializeCriticalSection( &mData->mCriticalSection );
45}
46
47Mutex::~Mutex()
48{
49   AssertFatal( TryEnterCriticalSection( &mData->mCriticalSection ), "Mutex::~Mutex - Critical section is locked!" );
50   DeleteCriticalSection( &mData->mCriticalSection );
51   SAFE_DELETE( mData );
52}
53
54//-----------------------------------------------------------------------------
55// Public Methods
56//-----------------------------------------------------------------------------
57
58bool Mutex::lock( bool block )
59{
60   AssertFatal( mData, "Mutex::lock - No data!" );
61
62   if( !block )
63      return TryEnterCriticalSection( &mData->mCriticalSection );
64   else
65   {
66      EnterCriticalSection( &mData->mCriticalSection );
67      return true;
68   }
69}
70
71void Mutex::unlock()
72{
73   AssertFatal( mData, "Mutex::unlock - No data!" );
74   LeaveCriticalSection( &mData->mCriticalSection );
75}
76