2013-12-09 10:59:57 +00:00
|
|
|
#include "ParallelWork.h"
|
|
|
|
|
|
|
|
#include "Thread.h"
|
|
|
|
#include "System.h"
|
2014-08-18 13:20:04 +00:00
|
|
|
#include "Semaphore.h"
|
|
|
|
#include "Mutex.h"
|
2014-08-18 10:17:16 +00:00
|
|
|
#include "ParallelWorker.h"
|
2013-12-09 10:59:57 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
2014-08-18 10:17:16 +00:00
|
|
|
/**
|
|
|
|
* Compatibility class for code that uses ParallelUnitFunction.
|
|
|
|
*/
|
2015-11-09 21:30:46 +00:00
|
|
|
class ParallelWorkerCompat : public ParallelWorker {
|
|
|
|
public:
|
|
|
|
ParallelWorkerCompat(ParallelWork *work, ParallelWork::ParallelUnitFunction func, void *data)
|
|
|
|
: ParallelWorker(), work(work), func(func), data(data) {
|
2014-08-18 10:17:16 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
virtual void processParallelUnit(int unit) {
|
2014-08-19 09:44:54 +00:00
|
|
|
func(work, unit, data);
|
2014-08-18 10:17:16 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
private:
|
|
|
|
ParallelWork *work;
|
2014-08-18 10:17:16 +00:00
|
|
|
ParallelWork::ParallelUnitFunction func;
|
2015-11-09 21:30:46 +00:00
|
|
|
void *data;
|
2014-08-18 10:17:16 +00:00
|
|
|
};
|
|
|
|
|
2014-08-18 13:20:04 +00:00
|
|
|
/**
|
|
|
|
* Thread sub-class to perform units of work
|
|
|
|
*/
|
2015-11-09 21:30:46 +00:00
|
|
|
class ParallelWork::ParallelThread : public Thread {
|
|
|
|
public:
|
|
|
|
ParallelThread(ParallelWork *work) : Thread(), work(work), sem(0) {
|
2014-08-18 13:20:04 +00:00
|
|
|
interrupted = false;
|
|
|
|
unit = -1;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void feedUnit(int unit) {
|
2014-08-18 13:20:04 +00:00
|
|
|
this->unit = unit;
|
|
|
|
sem.release();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void interrupt() {
|
2014-08-18 13:20:04 +00:00
|
|
|
interrupted = true;
|
|
|
|
sem.release();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
protected:
|
|
|
|
virtual void run() override {
|
|
|
|
while (unit >= 0 or not interrupted) {
|
2014-08-18 13:20:04 +00:00
|
|
|
// Wait for a unit (or interrupt)
|
|
|
|
sem.acquire();
|
|
|
|
|
|
|
|
// Process the unit
|
2015-11-09 21:30:46 +00:00
|
|
|
if (unit >= 0) {
|
2014-08-18 13:20:04 +00:00
|
|
|
work->worker->processParallelUnit(unit);
|
|
|
|
unit = -1;
|
2014-08-18 14:25:44 +00:00
|
|
|
work->returnThread(this);
|
2014-08-18 13:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
private:
|
|
|
|
ParallelWork *work;
|
2014-08-18 13:20:04 +00:00
|
|
|
Semaphore sem;
|
|
|
|
int unit;
|
|
|
|
bool interrupted;
|
|
|
|
};
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
ParallelWork::ParallelWork(ParallelWorker *worker, int units) {
|
2014-08-18 10:17:16 +00:00
|
|
|
this->units = units;
|
|
|
|
this->running = 0;
|
|
|
|
this->worker = worker;
|
|
|
|
this->worker_compat = false;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
ParallelWork::ParallelWork(ParallelUnitFunction func, int units, void *data) {
|
2013-12-09 10:59:57 +00:00
|
|
|
this->units = units;
|
|
|
|
this->running = 0;
|
2014-08-18 10:17:16 +00:00
|
|
|
this->worker = new ParallelWorkerCompat(this, func, data);
|
|
|
|
this->worker_compat = true;
|
2013-12-09 10:59:57 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
ParallelWork::~ParallelWork() {
|
2013-12-09 10:59:57 +00:00
|
|
|
assert(not running);
|
2015-11-09 21:30:46 +00:00
|
|
|
if (worker_compat) {
|
2014-08-18 10:17:16 +00:00
|
|
|
delete worker;
|
|
|
|
}
|
2013-12-09 10:59:57 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
int ParallelWork::perform(int thread_count) {
|
2014-08-18 13:20:04 +00:00
|
|
|
int i, done;
|
2013-12-09 10:59:57 +00:00
|
|
|
assert(not running);
|
|
|
|
|
2014-08-18 13:20:04 +00:00
|
|
|
// Get thread count
|
2015-11-09 21:30:46 +00:00
|
|
|
if (thread_count <= 0) {
|
2014-08-18 10:17:16 +00:00
|
|
|
thread_count = System::getCoreCount();
|
2013-12-09 10:59:57 +00:00
|
|
|
}
|
2015-11-09 21:30:46 +00:00
|
|
|
if (thread_count > PARALLEL_MAX_THREADS) {
|
2014-08-18 10:17:16 +00:00
|
|
|
thread_count = PARALLEL_MAX_THREADS;
|
2013-12-09 10:59:57 +00:00
|
|
|
}
|
2014-08-18 13:20:04 +00:00
|
|
|
this->thread_count = thread_count;
|
2013-12-09 10:59:57 +00:00
|
|
|
running = 1;
|
|
|
|
|
2014-08-18 13:20:04 +00:00
|
|
|
// Init threads
|
|
|
|
semaphore = new Semaphore(thread_count);
|
|
|
|
mutex = new Mutex();
|
2015-11-09 21:30:46 +00:00
|
|
|
threads = new ParallelThread *[thread_count];
|
|
|
|
available = new ParallelThread *[thread_count];
|
2014-08-18 13:20:04 +00:00
|
|
|
available_offset = 0;
|
|
|
|
available_length = thread_count;
|
2015-11-09 21:30:46 +00:00
|
|
|
for (i = 0; i < thread_count; i++) {
|
2014-08-18 13:20:04 +00:00
|
|
|
threads[i] = available[i] = new ParallelThread(this);
|
|
|
|
threads[i]->start();
|
2013-12-09 10:59:57 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 13:20:04 +00:00
|
|
|
// Perform all units
|
2015-11-09 21:30:46 +00:00
|
|
|
for (done = 0; done < units; done++) {
|
2014-08-18 13:20:04 +00:00
|
|
|
// Take first available thread
|
|
|
|
semaphore->acquire();
|
|
|
|
mutex->acquire();
|
|
|
|
ParallelThread *thread = available[available_offset];
|
|
|
|
available_offset++;
|
2015-11-09 21:30:46 +00:00
|
|
|
if (available_offset >= thread_count) {
|
2014-08-18 13:20:04 +00:00
|
|
|
available_offset = 0;
|
2013-12-09 10:59:57 +00:00
|
|
|
}
|
2014-08-18 13:20:04 +00:00
|
|
|
available_length--;
|
|
|
|
mutex->release();
|
|
|
|
|
|
|
|
// Feed the unit to it
|
|
|
|
thread->feedUnit(done);
|
2013-12-09 10:59:57 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 13:20:04 +00:00
|
|
|
// Wait for all threads to end, then cleanup
|
2015-11-09 21:30:46 +00:00
|
|
|
for (i = 0; i < thread_count; i++) {
|
2014-08-18 13:20:04 +00:00
|
|
|
threads[i]->interrupt();
|
2013-12-09 10:59:57 +00:00
|
|
|
}
|
2015-11-09 21:30:46 +00:00
|
|
|
for (i = 0; i < thread_count; i++) {
|
2014-08-18 13:20:04 +00:00
|
|
|
threads[i]->join();
|
|
|
|
delete threads[i];
|
|
|
|
}
|
|
|
|
delete[] threads;
|
|
|
|
delete[] available;
|
|
|
|
delete semaphore;
|
|
|
|
delete mutex;
|
2013-12-09 10:59:57 +00:00
|
|
|
|
|
|
|
running = 0;
|
2014-08-18 13:20:04 +00:00
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void ParallelWork::interrupt() {
|
2014-08-19 07:18:55 +00:00
|
|
|
worker->interrupt();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void ParallelWork::returnThread(ParallelWork::ParallelThread *thread) {
|
2014-08-18 13:20:04 +00:00
|
|
|
mutex->acquire();
|
|
|
|
available[(available_offset + available_length) % thread_count] = thread;
|
|
|
|
available_length++;
|
|
|
|
semaphore->release();
|
|
|
|
mutex->release();
|
2013-12-09 10:59:57 +00:00
|
|
|
}
|