semaphore.cpp
Engine/source/platformX86UNIX/threads/semaphore.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 "platformX86UNIX/platformX86UNIX.h" 25#include "platform/threads/semaphore.h" 26// Instead of that mess that was here before, lets use the SDL lib to deal 27// with the semaphores. 28 29#include <SDL.h> 30#include <SDL_thread.h> 31 32struct PlatformSemaphore 33{ 34 SDL_sem *semaphore; 35 36 PlatformSemaphore(S32 initialCount) 37 { 38 semaphore = SDL_CreateSemaphore(initialCount); 39 AssertFatal(semaphore, "PlatformSemaphore constructor - Failed to create SDL Semaphore."); 40 } 41 42 ~PlatformSemaphore() 43 { 44 SDL_DestroySemaphore(semaphore); 45 } 46}; 47 48Semaphore::Semaphore(S32 initialCount) 49{ 50 mData = new PlatformSemaphore(initialCount); 51} 52 53Semaphore::~Semaphore() 54{ 55 AssertFatal(mData, "Semaphore destructor - Invalid semaphore."); 56 delete mData; 57} 58 59bool Semaphore::acquire(bool block, S32 timeoutMS) 60{ 61 AssertFatal(mData && mData->semaphore, "Semaphore::acquire - Invalid semaphore."); 62 if (block) 63 { 64 // Semaphore acquiring is different from the MacOS/Win realization because SDL_SemWaitTimeout() with "infinite" timeout can be too heavy on some platforms. 65 // (see "man SDL_SemWaitTimeout(3)" for more info) 66 // "man" states to avoid the use of SDL_SemWaitTimeout at all, but at current stage this looks like a valid and working solution, so keeping it this way. 67 // [bank / Feb-2010] 68 if (timeoutMS == -1) 69 { 70 if (SDL_SemWait(mData->semaphore) < 0) 71 AssertFatal(false, "Semaphore::acquie - Wait failed."); 72 } 73 else 74 { 75 if (SDL_SemWaitTimeout(mData->semaphore, timeoutMS) < 0) 76 AssertFatal(false, "Semaphore::acquie - Wait with timeout failed."); 77 } 78 return (true); 79 } 80 else 81 { 82 int res = SDL_SemTryWait(mData->semaphore); 83 return (res == 0); 84 } 85} 86 87void Semaphore::release() 88{ 89 AssertFatal(mData, "Semaphore::releaseSemaphore - Invalid semaphore."); 90 SDL_SemPost(mData->semaphore); 91} 92