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