Path: blob/master/3rdparty/openexr/IlmThread/IlmThreadSemaphorePosix.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 platforms37// that support Posix threads and Posix semaphores38//39//-----------------------------------------------------------------------------4041#include "IlmBaseConfig.h"4243#if HAVE_PTHREAD && HAVE_POSIX_SEMAPHORES4445#include "IlmThreadSemaphore.h"46#include "Iex.h"47#include <assert.h>4849namespace IlmThread {505152Semaphore::Semaphore (unsigned int value)53{54if (::sem_init (&_semaphore, 0, value))55Iex::throwErrnoExc ("Cannot initialize semaphore (%T).");56}575859Semaphore::~Semaphore ()60{61int error = ::sem_destroy (&_semaphore);62assert (error == 0);63}646566void67Semaphore::wait ()68{69::sem_wait (&_semaphore);70}717273bool74Semaphore::tryWait ()75{76return sem_trywait (&_semaphore) == 0;77}787980void81Semaphore::post ()82{83if (::sem_post (&_semaphore))84Iex::throwErrnoExc ("Post operation on semaphore failed (%T).");85}868788int89Semaphore::value () const90{91int value;9293if (::sem_getvalue (&_semaphore, &value))94Iex::throwErrnoExc ("Cannot read semaphore value (%T).");9596return value;97}9899100} // namespace IlmThread101102#endif103104105