Path: blob/master/3rdparty/openexr/IlmThread/IlmThreadPool.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_POOL_H35#define INCLUDED_ILM_THREAD_POOL_H3637//-----------------------------------------------------------------------------38//39// class Task, class ThreadPool, class TaskGroup40//41// Class ThreadPool manages a set of worker threads and accepts42// tasks for processing. Tasks added to the thread pool are43// executed concurrently by the worker threads.44//45// Class Thread provides an abstract interface for a task which46// a ThreadPool works on. Derived classes need to implement the47// execute() function which performs the actual task.48//49// Class TaskTroup allows synchronization on the completion of a set50// of tasks. Every task that is added to a ThreadPool belongs to a51// single TaskGroup. The destructor of the TaskGroup waits for all52// tasks in the group to finish.53//54// Note: if you plan to use the ThreadPool interface in your own55// applications note that the implementation of the ThreadPool calls56// operator delete on tasks as they complete. If you define a custom57// operator new for your tasks, for instance to use a custom heap,58// then you must also write an appropriate operator delete.59//60//-----------------------------------------------------------------------------6162namespace IlmThread {6364class TaskGroup;65class Task;666768class ThreadPool69{70public:7172//-------------------------------------------------------73// Constructor -- creates numThreads worker threads which74// wait until a task is available.75//-------------------------------------------------------7677ThreadPool (unsigned numThreads = 0);787980//-----------------------------------------------------------81// Destructor -- waits for all tasks to complete, joins all82// the threads to the calling thread, and then destroys them.83//-----------------------------------------------------------8485virtual ~ThreadPool ();868788//--------------------------------------------------------89// Query and set the number of worker threads in the pool.90//91// Warning: never call setNumThreads from within a worker92// thread as this will almost certainly cause a deadlock93// or crash.94//--------------------------------------------------------9596int numThreads () const;97void setNumThreads (int count);9899100//------------------------------------------------------------101// Add a task for processing. The ThreadPool can handle any102// number of tasks regardless of the number of worker threads.103// The tasks are first added onto a queue, and are executed104// by threads as they become available, in FIFO order.105//------------------------------------------------------------106107void addTask (Task* task);108109110//-------------------------------------------111// Access functions for the global threadpool112//-------------------------------------------113114static ThreadPool& globalThreadPool ();115static void addGlobalTask (Task* task);116117struct Data;118119protected:120121Data * _data;122};123124125class Task126{127public:128129Task (TaskGroup* g);130virtual ~Task ();131132virtual void execute () = 0;133TaskGroup * group();134135protected:136137TaskGroup * _group;138};139140141class TaskGroup142{143public:144145TaskGroup();146~TaskGroup();147148struct Data;149Data* const _data;150};151152153} // namespace IlmThread154155#endif156157158