Path: blob/master/3rdparty/openexr/IlmThread/IlmThreadMutex.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_MUTEX_H35#define INCLUDED_ILM_THREAD_MUTEX_H3637//-----------------------------------------------------------------------------38//39// class Mutex, class Lock40//41// Class Mutex is a wrapper for a system-dependent mutual exclusion42// mechanism. Actual locking and unlocking of a Mutex object must43// be performed using an instance of a Lock (defined below).44//45// Class lock provides safe locking and unlocking of mutexes even in46// the presence of C++ exceptions. Constructing a Lock object locks47// the mutex; destroying the Lock unlocks the mutex.48//49// Lock objects are not themselves thread-safe. You should never50// share a Lock object among multiple threads.51//52// Typical usage:53//54// Mutex mtx; // Create a Mutex object that is visible55// //to multiple threads56//57// ... // create some threads58//59// // Then, within each thread, construct a critical section like so:60//61// {62// Lock lock (mtx); // Lock constructor locks the mutex63// ... // do some computation on shared data64// } // leaving the block unlocks the mutex65//66//-----------------------------------------------------------------------------6768#include "IlmBaseConfig.h"6970#if defined _WIN32 || defined _WIN6471#ifdef NOMINMAX72#undef NOMINMAX73#endif74#define NOMINMAX75#include <windows.h>76#elif HAVE_PTHREAD77#include <pthread.h>78#endif7980namespace IlmThread {8182class Lock;838485class Mutex86{87public:8889Mutex ();90virtual ~Mutex ();9192private:9394void lock () const;95void unlock () const;9697#if defined _WIN32 || defined _WIN6498mutable CRITICAL_SECTION _mutex;99#elif HAVE_PTHREAD100mutable pthread_mutex_t _mutex;101#endif102103void operator = (const Mutex& M); // not implemented104Mutex (const Mutex& M); // not implemented105106friend class Lock;107};108109110class Lock111{112public:113114Lock (const Mutex& m, bool autoLock = true):115_mutex (m),116_locked (false)117{118if (autoLock)119{120_mutex.lock();121_locked = true;122}123}124125~Lock ()126{127if (_locked)128_mutex.unlock();129}130131void acquire ()132{133_mutex.lock();134_locked = true;135}136137void release ()138{139_mutex.unlock();140_locked = false;141}142143bool locked ()144{145return _locked;146}147148private:149150const Mutex & _mutex;151bool _locked;152};153154155} // namespace IlmThread156157#endif158159160