Path: blob/master/3rdparty/openexr/IlmThread/IlmThreadSemaphorePosixCompat.cpp
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2005, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////3334//-----------------------------------------------------------------------------35//36// class Semaphore -- implementation for for platforms that do37// support Posix threads but do not support Posix semaphores,38// for example, OS X39//40//-----------------------------------------------------------------------------4142#include "IlmBaseConfig.h"4344#if HAVE_PTHREAD && !HAVE_POSIX_SEMAPHORES4546#include "IlmThreadSemaphore.h"47#include "Iex.h"48#include <assert.h>4950namespace IlmThread {515253Semaphore::Semaphore (unsigned int value)54{55if (int error = ::pthread_mutex_init (&_semaphore.mutex, 0))56Iex::throwErrnoExc ("Cannot initialize mutex (%T).", error);5758if (int error = ::pthread_cond_init (&_semaphore.nonZero, 0))59Iex::throwErrnoExc ("Cannot initialize condition variable (%T).",60error);6162_semaphore.count = value;63_semaphore.numWaiting = 0;64}656667Semaphore::~Semaphore ()68{69int error = ::pthread_cond_destroy (&_semaphore.nonZero);70assert (error == 0);71error = ::pthread_mutex_destroy (&_semaphore.mutex);72assert (error == 0);73}747576void77Semaphore::wait ()78{79::pthread_mutex_lock (&_semaphore.mutex);8081_semaphore.numWaiting++;8283while (_semaphore.count == 0)84{85if (int error = ::pthread_cond_wait (&_semaphore.nonZero,86&_semaphore.mutex))87{88::pthread_mutex_unlock (&_semaphore.mutex);8990Iex::throwErrnoExc ("Cannot wait on condition variable (%T).",91error);92}93}9495_semaphore.numWaiting--;96_semaphore.count--;9798::pthread_mutex_unlock (&_semaphore.mutex);99}100101102bool103Semaphore::tryWait ()104{105::pthread_mutex_lock (&_semaphore.mutex);106107if (_semaphore.count == 0)108{109::pthread_mutex_unlock (&_semaphore.mutex);110return false;111}112else113{114_semaphore.count--;115::pthread_mutex_unlock (&_semaphore.mutex);116return true;117}118}119120121void122Semaphore::post ()123{124::pthread_mutex_lock (&_semaphore.mutex);125126if (_semaphore.numWaiting > 0)127{128if (int error = ::pthread_cond_signal (&_semaphore.nonZero))129{130::pthread_mutex_unlock (&_semaphore.mutex);131132Iex::throwErrnoExc ("Cannot signal condition variable (%T).",133error);134}135}136137_semaphore.count++;138::pthread_mutex_unlock (&_semaphore.mutex);139}140141142int143Semaphore::value () const144{145::pthread_mutex_lock (&_semaphore.mutex);146int value = _semaphore.count;147::pthread_mutex_unlock (&_semaphore.mutex);148return value;149}150151152} // namespace IlmThread153154#endif155156157