|
Packit |
8dc392 |
///////////////////////////////////////////////////////////////////////////
|
|
Packit |
8dc392 |
//
|
|
Packit |
8dc392 |
// Copyright (c) 2005-2012, Industrial Light & Magic, a division of Lucas
|
|
Packit |
8dc392 |
// Digital Ltd. LLC
|
|
Packit |
8dc392 |
//
|
|
Packit |
8dc392 |
// All rights reserved.
|
|
Packit |
8dc392 |
//
|
|
Packit |
8dc392 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
8dc392 |
// modification, are permitted provided that the following conditions are
|
|
Packit |
8dc392 |
// met:
|
|
Packit |
8dc392 |
// * Redistributions of source code must retain the above copyright
|
|
Packit |
8dc392 |
// notice, this list of conditions and the following disclaimer.
|
|
Packit |
8dc392 |
// * Redistributions in binary form must reproduce the above
|
|
Packit |
8dc392 |
// copyright notice, this list of conditions and the following disclaimer
|
|
Packit |
8dc392 |
// in the documentation and/or other materials provided with the
|
|
Packit |
8dc392 |
// distribution.
|
|
Packit |
8dc392 |
// * Neither the name of Industrial Light & Magic nor the names of
|
|
Packit |
8dc392 |
// its contributors may be used to endorse or promote products derived
|
|
Packit |
8dc392 |
// from this software without specific prior written permission.
|
|
Packit |
8dc392 |
//
|
|
Packit |
8dc392 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit |
8dc392 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit |
8dc392 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
Packit |
8dc392 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
Packit |
8dc392 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
Packit |
8dc392 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
Packit |
8dc392 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
Packit |
8dc392 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
Packit |
8dc392 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
Packit |
8dc392 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
Packit |
8dc392 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
8dc392 |
//
|
|
Packit |
8dc392 |
///////////////////////////////////////////////////////////////////////////
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
//-----------------------------------------------------------------------------
|
|
Packit |
8dc392 |
//
|
|
Packit |
8dc392 |
// class Semaphore -- implementation for for platforms that do
|
|
Packit |
8dc392 |
// support Posix threads but do not support Posix semaphores,
|
|
Packit |
8dc392 |
// for example, OS X
|
|
Packit |
8dc392 |
//
|
|
Packit |
8dc392 |
//-----------------------------------------------------------------------------
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
#include "IlmBaseConfig.h"
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
#if HAVE_PTHREAD && !HAVE_POSIX_SEMAPHORES
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
#include "IlmThreadSemaphore.h"
|
|
Packit |
8dc392 |
#include "Iex.h"
|
|
Packit |
8dc392 |
#include <assert.h>
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
ILMTHREAD_INTERNAL_NAMESPACE_SOURCE_ENTER
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
Semaphore::Semaphore (unsigned int value)
|
|
Packit |
8dc392 |
{
|
|
Packit |
8dc392 |
if (int error = ::pthread_mutex_init (&_semaphore.mutex, 0))
|
|
Packit |
8dc392 |
IEX_NAMESPACE::throwErrnoExc ("Cannot initialize mutex (%T).", error);
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
if (int error = ::pthread_cond_init (&_semaphore.nonZero, 0))
|
|
Packit |
8dc392 |
IEX_NAMESPACE::throwErrnoExc ("Cannot initialize condition variable (%T).",
|
|
Packit |
8dc392 |
error);
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
_semaphore.count = value;
|
|
Packit |
8dc392 |
_semaphore.numWaiting = 0;
|
|
Packit |
8dc392 |
}
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
Semaphore::~Semaphore ()
|
|
Packit |
8dc392 |
{
|
|
Packit |
8dc392 |
int error = ::pthread_cond_destroy (&_semaphore.nonZero);
|
|
Packit |
8dc392 |
assert (error == 0);
|
|
Packit |
8dc392 |
error = ::pthread_mutex_destroy (&_semaphore.mutex);
|
|
Packit |
8dc392 |
assert (error == 0);
|
|
Packit |
8dc392 |
}
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
void
|
|
Packit |
8dc392 |
Semaphore::wait ()
|
|
Packit |
8dc392 |
{
|
|
Packit |
8dc392 |
::pthread_mutex_lock (&_semaphore.mutex);
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
_semaphore.numWaiting++;
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
while (_semaphore.count == 0)
|
|
Packit |
8dc392 |
{
|
|
Packit |
8dc392 |
if (int error = ::pthread_cond_wait (&_semaphore.nonZero,
|
|
Packit |
8dc392 |
&_semaphore.mutex))
|
|
Packit |
8dc392 |
{
|
|
Packit |
8dc392 |
::pthread_mutex_unlock (&_semaphore.mutex);
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
IEX_NAMESPACE::throwErrnoExc ("Cannot wait on condition variable (%T).",
|
|
Packit |
8dc392 |
error);
|
|
Packit |
8dc392 |
}
|
|
Packit |
8dc392 |
}
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
_semaphore.numWaiting--;
|
|
Packit |
8dc392 |
_semaphore.count--;
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
::pthread_mutex_unlock (&_semaphore.mutex);
|
|
Packit |
8dc392 |
}
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
bool
|
|
Packit |
8dc392 |
Semaphore::tryWait ()
|
|
Packit |
8dc392 |
{
|
|
Packit |
8dc392 |
::pthread_mutex_lock (&_semaphore.mutex);
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
if (_semaphore.count == 0)
|
|
Packit |
8dc392 |
{
|
|
Packit |
8dc392 |
::pthread_mutex_unlock (&_semaphore.mutex);
|
|
Packit |
8dc392 |
return false;
|
|
Packit |
8dc392 |
}
|
|
Packit |
8dc392 |
else
|
|
Packit |
8dc392 |
{
|
|
Packit |
8dc392 |
_semaphore.count--;
|
|
Packit |
8dc392 |
::pthread_mutex_unlock (&_semaphore.mutex);
|
|
Packit |
8dc392 |
return true;
|
|
Packit |
8dc392 |
}
|
|
Packit |
8dc392 |
}
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
void
|
|
Packit |
8dc392 |
Semaphore::post ()
|
|
Packit |
8dc392 |
{
|
|
Packit |
8dc392 |
::pthread_mutex_lock (&_semaphore.mutex);
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
if (_semaphore.numWaiting > 0)
|
|
Packit |
8dc392 |
{
|
|
Packit |
8dc392 |
if (int error = ::pthread_cond_signal (&_semaphore.nonZero))
|
|
Packit |
8dc392 |
{
|
|
Packit |
8dc392 |
::pthread_mutex_unlock (&_semaphore.mutex);
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
IEX_NAMESPACE::throwErrnoExc ("Cannot signal condition variable (%T).",
|
|
Packit |
8dc392 |
error);
|
|
Packit |
8dc392 |
}
|
|
Packit |
8dc392 |
}
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
_semaphore.count++;
|
|
Packit |
8dc392 |
::pthread_mutex_unlock (&_semaphore.mutex);
|
|
Packit |
8dc392 |
}
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
int
|
|
Packit |
8dc392 |
Semaphore::value () const
|
|
Packit |
8dc392 |
{
|
|
Packit |
8dc392 |
::pthread_mutex_lock (&_semaphore.mutex);
|
|
Packit |
8dc392 |
int value = _semaphore.count;
|
|
Packit |
8dc392 |
::pthread_mutex_unlock (&_semaphore.mutex);
|
|
Packit |
8dc392 |
return value;
|
|
Packit |
8dc392 |
}
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
ILMTHREAD_INTERNAL_NAMESPACE_SOURCE_EXIT
|
|
Packit |
8dc392 |
|
|
Packit |
8dc392 |
#endif
|