semaphore.cpp

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