opencv/3rdparty/openexr/IlmThread/IlmThreadPool.cpp

457 lines
9.0 KiB
C++
Raw Normal View History

2012-08-24 22:31:49 +02:00
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2005, Industrial Light & Magic, a division of Lucas
// Digital Ltd. LLC
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// All rights reserved.
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Industrial Light & Magic nor the names of
// its contributors may be used to endorse or promote products derived
2012-10-17 09:12:04 +02:00
// from this software without specific prior written permission.
//
2012-08-24 22:31:49 +02:00
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
//
// class Task, class ThreadPool, class TaskGroup
//
//-----------------------------------------------------------------------------
#include "IlmThread.h"
#include "IlmThreadMutex.h"
#include "IlmThreadSemaphore.h"
#include "IlmThreadPool.h"
#include "Iex.h"
#include <list>
using namespace std;
namespace IlmThread {
namespace {
class WorkerThread: public Thread
{
public:
WorkerThread (ThreadPool::Data* data);
virtual void run ();
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
private:
ThreadPool::Data * _data;
};
} //namespace
struct TaskGroup::Data
{
Data ();
~Data ();
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
void addTask () ;
void removeTask ();
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
Semaphore isEmpty; // used to signal that the taskgroup is empty
int numPending; // number of pending tasks to still execute
};
struct ThreadPool::Data
{
Data ();
~Data();
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
void finish ();
bool stopped () const;
void stop ();
Semaphore taskSemaphore; // threads wait on this for ready tasks
Mutex taskMutex; // mutual exclusion for the tasks list
list<Task*> tasks; // the list of tasks to execute
size_t numTasks; // fast access to list size
// (list::size() can be O(n))
Semaphore threadSemaphore; // signaled when a thread starts executing
Mutex threadMutex; // mutual exclusion for threads list
list<WorkerThread*> threads; // the list of all threads
size_t numThreads; // fast access to list size
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
bool stopping; // flag indicating whether to stop threads
Mutex stopMutex; // mutual exclusion for stopping flag
};
//
// class WorkerThread
//
WorkerThread::WorkerThread (ThreadPool::Data* data):
_data (data)
{
start();
}
void
WorkerThread::run ()
{
//
// Signal that the thread has started executing
//
_data->threadSemaphore.post();
while (true)
{
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// Wait for a task to become available
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
_data->taskSemaphore.wait();
{
Lock taskLock (_data->taskMutex);
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// If there is a task pending, pop off the next task in the FIFO
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
if (_data->numTasks > 0)
{
Task* task = _data->tasks.front();
2012-10-17 09:12:04 +02:00
TaskGroup* taskGroup = task->group();
2012-08-24 22:31:49 +02:00
_data->tasks.pop_front();
_data->numTasks--;
taskLock.release();
task->execute();
taskLock.acquire();
delete task;
taskGroup->_data->removeTask();
}
else if (_data->stopped())
2012-10-17 09:12:04 +02:00
{
2012-08-24 22:31:49 +02:00
break;
2012-10-17 09:12:04 +02:00
}
2012-08-24 22:31:49 +02:00
}
}
}
//
// struct TaskGroup::Data
//
TaskGroup::Data::Data (): isEmpty (1), numPending (0)
{
// empty
}
TaskGroup::Data::~Data ()
{
//
// A TaskGroup acts like an "inverted" semaphore: if the count
// is above 0 then waiting on the taskgroup will block. This
// destructor waits until the taskgroup is empty before returning.
//
isEmpty.wait ();
}
void
2012-10-17 09:12:04 +02:00
TaskGroup::Data::addTask ()
2012-08-24 22:31:49 +02:00
{
//
// Any access to the taskgroup is protected by a mutex that is
// held by the threadpool. Therefore it is safe to access
// numPending before we wait on the semaphore.
//
if (numPending++ == 0)
2012-10-17 09:12:04 +02:00
isEmpty.wait ();
2012-08-24 22:31:49 +02:00
}
void
TaskGroup::Data::removeTask ()
{
if (--numPending == 0)
2012-10-17 09:12:04 +02:00
isEmpty.post ();
2012-08-24 22:31:49 +02:00
}
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
//
// struct ThreadPool::Data
//
ThreadPool::Data::Data (): numTasks (0), numThreads (0), stopping (false)
{
// empty
}
ThreadPool::Data::~Data()
{
Lock lock (threadMutex);
finish ();
}
void
ThreadPool::Data::finish ()
{
stop();
//
// Signal enough times to allow all threads to stop.
//
// Wait until all threads have started their run functions.
// If we do not wait before we destroy the threads then it's
// possible that the threads have not yet called their run
// functions.
// If this happens then the run function will be called off
// of an invalid object and we will crash, most likely with
// an error like: "pure virtual method called"
//
for (size_t i = 0; i < numThreads; i++)
{
2012-10-17 09:12:04 +02:00
taskSemaphore.post();
threadSemaphore.wait();
2012-08-24 22:31:49 +02:00
}
//
// Join all the threads
//
for (list<WorkerThread*>::iterator i = threads.begin();
2012-10-17 09:12:04 +02:00
i != threads.end();
++i)
2012-08-24 22:31:49 +02:00
{
2012-10-17 09:12:04 +02:00
delete (*i);
2012-08-24 22:31:49 +02:00
}
Lock lock1 (taskMutex);
Lock lock2 (stopMutex);
threads.clear();
tasks.clear();
numThreads = 0;
numTasks = 0;
stopping = false;
}
bool
ThreadPool::Data::stopped () const
{
Lock lock (stopMutex);
return stopping;
}
void
ThreadPool::Data::stop ()
{
Lock lock (stopMutex);
stopping = true;
}
//
// class Task
//
Task::Task (TaskGroup* g): _group(g)
{
// empty
}
Task::~Task()
{
// empty
}
TaskGroup*
Task::group ()
{
return _group;
}
TaskGroup::TaskGroup ():
_data (new Data())
{
// empty
}
TaskGroup::~TaskGroup ()
{
delete _data;
}
//
// class ThreadPool
//
ThreadPool::ThreadPool (unsigned nthreads):
_data (new Data())
{
setNumThreads (nthreads);
}
ThreadPool::~ThreadPool ()
{
delete _data;
}
int
ThreadPool::numThreads () const
{
Lock lock (_data->threadMutex);
return _data->numThreads;
}
void
ThreadPool::setNumThreads (int count)
{
if (count < 0)
throw Iex::ArgExc ("Attempt to set the number of threads "
2012-10-17 09:12:04 +02:00
"in a thread pool to a negative value.");
2012-08-24 22:31:49 +02:00
//
// Lock access to thread list and size
//
Lock lock (_data->threadMutex);
if ((size_t)count > _data->numThreads)
{
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// Add more threads
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
while (_data->numThreads < (size_t)count)
{
_data->threads.push_back (new WorkerThread (_data));
_data->numThreads++;
}
}
else if ((size_t)count < _data->numThreads)
{
2012-10-17 09:12:04 +02:00
//
// Wait until all existing threads are finished processing,
// then delete all threads.
//
2012-08-24 22:31:49 +02:00
_data->finish ();
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// Add in new threads
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
while (_data->numThreads < (size_t)count)
{
_data->threads.push_back (new WorkerThread (_data));
_data->numThreads++;
}
}
}
void
2012-10-17 09:12:04 +02:00
ThreadPool::addTask (Task* task)
2012-08-24 22:31:49 +02:00
{
//
// Lock the threads, needed to access numThreads
//
Lock lock (_data->threadMutex);
if (_data->numThreads == 0)
{
task->execute ();
delete task;
}
else
{
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// Get exclusive access to the tasks queue
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
{
Lock taskLock (_data->taskMutex);
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// Push the new task into the FIFO
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
_data->tasks.push_back (task);
_data->numTasks++;
task->group()->_data->addTask();
}
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// Signal that we have a new task to process
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
_data->taskSemaphore.post ();
}
}
ThreadPool&
ThreadPool::globalThreadPool ()
{
//
// The global thread pool
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
static ThreadPool gThreadPool (0);
return gThreadPool;
}
void
ThreadPool::addGlobalTask (Task* task)
{
globalThreadPool().addTask (task);
}
} // namespace IlmThread