mutex.cpp

Engine/source/platformSDL/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 "console/console.h"
25#include "platform/threads/mutex.h"
26#include "core/util/safeDelete.h"
27
28#include <SDL.h>
29#include <SDL_thread.h>
30
31struct PlatformMutexData
32{
33   SDL_mutex *mutex;
34};
35
36Mutex::Mutex()
37{
38   mData = new PlatformMutexData;
39   mData->mutex = SDL_CreateMutex();
40}
41
42Mutex::~Mutex()
43{
44   AssertFatal(mData, "Mutex::destroyMutex: invalid mutex");
45   SDL_DestroyMutex(mData->mutex);
46   SAFE_DELETE(mData);
47}
48
49bool Mutex::lock(bool block)
50{
51   if(mData == NULL)
52      return false;
53   if(block)
54   {
55      return SDL_LockMutex(mData->mutex) == 0;
56   }
57   else
58   {
59      return SDL_TryLockMutex(mData->mutex) == 0;
60   }
61}
62
63void Mutex::unlock()
64{
65   if(mData == NULL)
66      return;
67   SDL_UnlockMutex(mData->mutex);
68}
69