mutex.cpp
Engine/source/platformX86UNIX/threads/mutex.cpp
Classes:
class
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 "platformX86UNIX/platformX86UNIX.h" 26#include "platform/threads/mutex.h" 27#include "core/util/safeDelete.h" 28 29#include <pthread.h> 30#include <sys/stat.h> 31#include <unistd.h> 32#include <fcntl.h> 33#include <errno.h> 34 35struct PlatformMutexData 36{ 37 pthread_mutex_t mutex; 38}; 39 40Mutex::Mutex() 41{ 42 mData = new PlatformMutexData; 43 pthread_mutexattr_t attr; 44 45 pthread_mutexattr_init(&attr); 46 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE); 47 48 pthread_mutex_init(&mData->mutex, &attr); 49} 50 51Mutex::~Mutex() 52{ 53 AssertFatal(mData, "Mutex::destroyMutex: invalid mutex"); 54 pthread_mutex_destroy(&mData->mutex); 55 SAFE_DELETE(mData); 56} 57 58bool Mutex::lock(bool block) 59{ 60 if(mData == NULL) 61 return false; 62 if(block) 63 { 64 return pthread_mutex_lock(&mData->mutex) == 0; 65 } 66 else 67 { 68 return pthread_mutex_trylock(&mData->mutex) == 0; 69 } 70} 71 72void Mutex::unlock() 73{ 74 if(mData == NULL) 75 return; 76 pthread_mutex_unlock(&mData->mutex); 77} 78