2013-12-09 10:59:57 +00:00
|
|
|
#ifndef PARALLELWORK_H
|
|
|
|
#define PARALLELWORK_H
|
|
|
|
|
|
|
|
#include "system_global.h"
|
|
|
|
|
|
|
|
#define PARALLEL_MAX_THREADS 20
|
|
|
|
|
|
|
|
namespace paysages {
|
|
|
|
namespace system {
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
class SYSTEMSHARED_EXPORT ParallelWork {
|
|
|
|
public:
|
|
|
|
typedef int (*ParallelUnitFunction)(ParallelWork *work, int unit, void *data);
|
2013-12-09 10:59:57 +00:00
|
|
|
|
2014-08-18 13:20:04 +00:00
|
|
|
/**
|
|
|
|
* Obscure thread class.
|
|
|
|
*/
|
|
|
|
class ParallelThread;
|
|
|
|
friend class ParallelThread;
|
2013-12-09 10:59:57 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
public:
|
2013-12-09 10:59:57 +00:00
|
|
|
/**
|
|
|
|
* Create a parallel work handler.
|
|
|
|
*
|
2014-08-18 10:17:16 +00:00
|
|
|
* This will spawn a number of threads.
|
|
|
|
*/
|
|
|
|
ParallelWork(ParallelWorker *worker, int units);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a parallel work handler.
|
2013-12-09 10:59:57 +00:00
|
|
|
*
|
2014-08-18 10:17:16 +00:00
|
|
|
* This is a compatibility constructor for older code, use the constructor with ParallelWorker instead.
|
2013-12-09 10:59:57 +00:00
|
|
|
*/
|
2015-11-09 21:30:46 +00:00
|
|
|
ParallelWork(ParallelUnitFunction func, int units, void *data);
|
2013-12-09 10:59:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a parallel work handler.
|
|
|
|
*
|
|
|
|
* The work must be terminated or fully interrupted before calling this.
|
|
|
|
*/
|
|
|
|
~ParallelWork();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start working on the units.
|
|
|
|
*
|
2014-08-18 10:17:16 +00:00
|
|
|
* @param threads Number of threads to spaws, -1 for an optimal number.
|
2013-12-09 10:59:57 +00:00
|
|
|
*/
|
2015-11-09 21:30:46 +00:00
|
|
|
int perform(int thread_count = -1);
|
2013-12-09 10:59:57 +00:00
|
|
|
|
2014-08-19 07:18:55 +00:00
|
|
|
/**
|
|
|
|
* Tell the threads to interrupt what they are doing.
|
|
|
|
*
|
|
|
|
* This will also call interrupt() on the worker.
|
|
|
|
*/
|
|
|
|
void interrupt();
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
private:
|
2014-08-18 13:20:04 +00:00
|
|
|
void returnThread(ParallelThread *thread);
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
private:
|
2013-12-09 10:59:57 +00:00
|
|
|
int units;
|
|
|
|
int running;
|
2014-08-18 10:17:16 +00:00
|
|
|
ParallelWorker *worker;
|
|
|
|
bool worker_compat;
|
2014-08-18 13:20:04 +00:00
|
|
|
|
|
|
|
int thread_count;
|
2015-11-09 21:30:46 +00:00
|
|
|
Mutex *mutex;
|
|
|
|
Semaphore *semaphore;
|
|
|
|
ParallelThread **threads;
|
|
|
|
ParallelThread **available;
|
2014-08-18 13:20:04 +00:00
|
|
|
int available_offset;
|
|
|
|
int available_length;
|
2013-12-09 10:59:57 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // PARALLELWORK_H
|