Path: blob/master/3rdparty/openexr/IlmThread/IlmThreadSemaphore.h
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#ifndef INCLUDED_ILM_THREAD_SEMAPHORE_H35#define INCLUDED_ILM_THREAD_SEMAPHORE_H3637//-----------------------------------------------------------------------------38//39// class Semaphore -- a wrapper class for40// system-dependent counting semaphores41//42//-----------------------------------------------------------------------------4344#include "IlmBaseConfig.h"4546#if defined _WIN32 || defined _WIN6447#ifdef NOMINMAX48#undef NOMINMAX49#endif50#define NOMINMAX51#include <windows.h>52#elif HAVE_PTHREAD && !HAVE_POSIX_SEMAPHORES53#include <pthread.h>54#elif HAVE_PTHREAD && HAVE_POSIX_SEMAPHORES55#include <semaphore.h>56#endif5758namespace IlmThread {596061class Semaphore62{63public:6465Semaphore (unsigned int value = 0);66virtual ~Semaphore();6768void wait();69bool tryWait();70void post();71int value() const;7273private:7475#if defined _WIN32 || defined _WIN647677mutable HANDLE _semaphore;7879#elif HAVE_PTHREAD && !HAVE_POSIX_SEMAPHORES8081//82// If the platform has Posix threads but no semapohores,83// then we implement them ourselves using condition variables84//8586struct sema_t87{88unsigned int count;89unsigned long numWaiting;90pthread_mutex_t mutex;91pthread_cond_t nonZero;92};9394mutable sema_t _semaphore;9596#elif HAVE_PTHREAD && HAVE_POSIX_SEMAPHORES9798mutable sem_t _semaphore;99100#endif101102void operator = (const Semaphore& s); // not implemented103Semaphore (const Semaphore& s); // not implemented104};105106107} // namespace IlmThread108109#endif110111112