Path: blob/master/3rdparty/openexr/IlmThread/IlmThread.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_H35#define INCLUDED_ILM_THREAD_H3637//-----------------------------------------------------------------------------38//39// class Thread40//41// Class Thread is a portable interface to a system-dependent thread42// primitive. In order to make a thread actually do something useful,43// you must derive a subclass from class Thread and implement the44// run() function. If the operating system supports threading then45// the run() function will be executed int a new thread.46//47// The actual creation of the thread is done by the start() routine48// which then calls the run() function. In general the start()49// routine should be called from the constructor of the derived class.50//51// The base-class thread destructor will join/destroy the thread.52//53// IMPORTANT: Due to the mechanisms that encapsulate the low-level54// threading primitives in a C++ class there is a race condition55// with code resembling the following:56//57// {58// WorkerThread myThread;59// } // myThread goes out of scope, is destroyed60// // and the thread is joined61//62// The race is between the parent thread joining the child thread63// in the destructor of myThread, and the run() function in the64// child thread. If the destructor gets executed first then run()65// will be called with an invalid "this" pointer.66//67// This issue can be fixed by using a Semaphore to keep track of68// whether the run() function has already been called. You can69// include a Semaphore member variable within your derived class70// which you post() on in the run() function, and wait() on in the71// destructor before the thread is joined. Alternatively you could72// do something like this:73//74// Semaphore runStarted;75//76// void WorkerThread::run ()77// {78// runStarted.post()79// // do some work80// ...81// }82//83// {84// WorkerThread myThread;85// runStarted.wait (); // ensure that we have started86// // the run function87// } // myThread goes out of scope, is destroyed88// // and the thread is joined89//90//-----------------------------------------------------------------------------9192#include "IlmBaseConfig.h"9394#if defined _WIN32 || defined _WIN6495#ifdef NOMINMAX96#undef NOMINMAX97#endif98#define NOMINMAX99#include <windows.h>100#include <process.h>101#elif HAVE_PTHREAD102#include <pthread.h>103#endif104105#if defined(OPENEXR_DLL) && !defined(ZENO_STATIC)106#ifdef ILMTHREAD_EXPORTS107#define ILMTHREAD_EXPORT __declspec(dllexport)108#else109#define ILMTHREAD_EXPORT __declspec(dllimport)110#endif111#else112#define ILMTHREAD_EXPORT113#endif114115namespace IlmThread {116117//118// Query function to determine if the current platform supports119// threads AND this library was compiled with threading enabled.120//121122ILMTHREAD_EXPORT bool supportsThreads ();123124125class ILMTHREAD_EXPORT Thread126{127public:128129Thread ();130virtual ~Thread ();131132void start ();133virtual void run () = 0;134135private:136137#if defined _WIN32 || defined _WIN64138HANDLE _thread;139#elif HAVE_PTHREAD140pthread_t _thread;141#endif142143void operator = (const Thread& t); // not implemented144Thread (const Thread& t); // not implemented145};146147148} // namespace IlmThread149150#endif151152153