Blame IlmThread/IlmThread.h

Packit 8dc392
///////////////////////////////////////////////////////////////////////////
Packit 8dc392
//
Packit 8dc392
// Copyright (c) 2005-2012, Industrial Light & Magic, a division of Lucas
Packit 8dc392
// Digital Ltd. LLC
Packit 8dc392
// 
Packit 8dc392
// All rights reserved.
Packit 8dc392
// 
Packit 8dc392
// Redistribution and use in source and binary forms, with or without
Packit 8dc392
// modification, are permitted provided that the following conditions are
Packit 8dc392
// met:
Packit 8dc392
// *       Redistributions of source code must retain the above copyright
Packit 8dc392
// notice, this list of conditions and the following disclaimer.
Packit 8dc392
// *       Redistributions in binary form must reproduce the above
Packit 8dc392
// copyright notice, this list of conditions and the following disclaimer
Packit 8dc392
// in the documentation and/or other materials provided with the
Packit 8dc392
// distribution.
Packit 8dc392
// *       Neither the name of Industrial Light & Magic nor the names of
Packit 8dc392
// its contributors may be used to endorse or promote products derived
Packit 8dc392
// from this software without specific prior written permission. 
Packit 8dc392
// 
Packit 8dc392
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 8dc392
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 8dc392
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 8dc392
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 8dc392
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 8dc392
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 8dc392
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 8dc392
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 8dc392
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 8dc392
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 8dc392
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 8dc392
//
Packit 8dc392
///////////////////////////////////////////////////////////////////////////
Packit 8dc392
Packit 8dc392
#ifndef INCLUDED_ILM_THREAD_H
Packit 8dc392
#define INCLUDED_ILM_THREAD_H
Packit 8dc392
Packit 8dc392
//-----------------------------------------------------------------------------
Packit 8dc392
//
Packit 8dc392
//	class Thread
Packit 8dc392
//
Packit 8dc392
//	Class Thread is a portable interface to a system-dependent thread
Packit 8dc392
//	primitive.  In order to make a thread actually do something useful,
Packit 8dc392
//	you must derive a subclass from class Thread and implement the
Packit 8dc392
//	run() function.  If the operating system supports threading then
Packit 8dc392
//	the run() function will be executed int a new thread.
Packit 8dc392
//
Packit 8dc392
//	The actual creation of the thread is done by the start() routine
Packit 8dc392
//	which then calls the run() function.  In general the start()
Packit 8dc392
//	routine should be called from the constructor of the derived class.
Packit 8dc392
//
Packit 8dc392
//	The base-class thread destructor will join/destroy the thread.
Packit 8dc392
//
Packit 8dc392
//	IMPORTANT: Due to the mechanisms that encapsulate the low-level
Packit 8dc392
//	threading primitives in a C++ class there is a race condition
Packit 8dc392
//	with code resembling the following:
Packit 8dc392
//
Packit 8dc392
//	    {
Packit 8dc392
//		WorkerThread myThread;
Packit 8dc392
//	    } // myThread goes out of scope, is destroyed
Packit 8dc392
//	      // and the thread is joined
Packit 8dc392
//
Packit 8dc392
//	The race is between the parent thread joining the child thread
Packit 8dc392
//	in the destructor of myThread, and the run() function in the
Packit 8dc392
//	child thread.  If the destructor gets executed first then run()
Packit 8dc392
//	will be called with an invalid "this" pointer.
Packit 8dc392
//
Packit 8dc392
//	This issue can be fixed by using a Semaphore to keep track of
Packit 8dc392
//	whether the run() function has already been called.  You can
Packit 8dc392
//	include a Semaphore member variable within your derived class
Packit 8dc392
//	which you post() on in the run() function, and wait() on in the
Packit 8dc392
//	destructor before the thread is joined.  Alternatively you could
Packit 8dc392
//	do something like this:
Packit 8dc392
//
Packit 8dc392
//	    Semaphore runStarted;
Packit 8dc392
//
Packit 8dc392
//	    void WorkerThread::run ()
Packit 8dc392
//	    {
Packit 8dc392
//		runStarted.post()
Packit 8dc392
//		// do some work
Packit 8dc392
//		...
Packit 8dc392
//	    }
Packit 8dc392
//
Packit 8dc392
//	    {
Packit 8dc392
//		WorkerThread myThread;
Packit 8dc392
//		runStarted.wait ();    // ensure that we have started
Packit 8dc392
//				       // the run function
Packit 8dc392
//	    } // myThread goes out of scope, is destroyed
Packit 8dc392
//	      // and the thread is joined
Packit 8dc392
//
Packit 8dc392
//-----------------------------------------------------------------------------
Packit 8dc392
Packit 8dc392
#include "IlmBaseConfig.h"
Packit 8dc392
#include "IlmThreadExport.h"
Packit 8dc392
#include "IlmThreadNamespace.h"
Packit 8dc392
Packit 8dc392
#if defined _WIN32 || defined _WIN64
Packit 8dc392
    #ifdef NOMINMAX
Packit 8dc392
        #undef NOMINMAX
Packit 8dc392
    #endif
Packit 8dc392
    #define NOMINMAX
Packit 8dc392
    #include <windows.h>
Packit 8dc392
    #include <process.h>
Packit 8dc392
#elif HAVE_PTHREAD
Packit 8dc392
    #include <pthread.h>
Packit 8dc392
#endif
Packit 8dc392
Packit 8dc392
ILMTHREAD_INTERNAL_NAMESPACE_HEADER_ENTER
Packit 8dc392
Packit 8dc392
//
Packit 8dc392
// Query function to determine if the current platform supports
Packit 8dc392
// threads AND this library was compiled with threading enabled.
Packit 8dc392
//
Packit 8dc392
Packit 8dc392
ILMTHREAD_EXPORT bool supportsThreads ();
Packit 8dc392
Packit 8dc392
Packit 8dc392
class ILMTHREAD_EXPORT Thread
Packit 8dc392
{
Packit 8dc392
  public:
Packit 8dc392
Packit 8dc392
    Thread ();
Packit 8dc392
    virtual ~Thread ();
Packit 8dc392
Packit 8dc392
    void		start ();
Packit 8dc392
    virtual void	run () = 0;
Packit 8dc392
    
Packit 8dc392
  private:
Packit 8dc392
Packit 8dc392
    #if defined _WIN32 || defined _WIN64
Packit 8dc392
	HANDLE _thread;
Packit 8dc392
    #elif HAVE_PTHREAD
Packit 8dc392
	pthread_t _thread;
Packit 8dc392
    #endif
Packit 8dc392
Packit 8dc392
    void operator = (const Thread& t);	// not implemented
Packit 8dc392
    Thread (const Thread& t);		// not implemented
Packit 8dc392
};
Packit 8dc392
Packit 8dc392
Packit 8dc392
ILMTHREAD_INTERNAL_NAMESPACE_HEADER_EXIT
Packit 8dc392
Packit 8dc392
#endif // INCLUDED_ILM_THREAD_H