8 #ifndef META_OCEAN_BASE_WORKER_H
9 #define META_OCEAN_BASE_WORKER_H
105 explicit WorkerThread(
const unsigned int workerSeedValue,
const unsigned int workerThreadId);
116 inline unsigned int id();
129 inline void setThreadFunction(
const Function&
function);
135 inline void setThreadFunction(
Function&&
function);
142 inline void setThreadFunction(
const AbortableFunction& abortableFunction,
const unsigned int abortIndex);
149 inline void setThreadFunction(
AbortableFunction&& abortableFunction,
const unsigned int abortIndex);
180 unsigned int id_ = (
unsigned int)(-1);
183 unsigned int abortIndex_ = (
unsigned int)(-1);
202 explicit Worker(
const LoadType loadType = TYPE_ALL_CORES,
const unsigned int maximalNumberCores = 16u);
280 bool executeFunction(
const Function&
function,
const unsigned int first,
const unsigned int size,
const unsigned int firstIndex = (
unsigned int)(-1),
const unsigned int sizeIndex = (
unsigned int)(-1),
const unsigned int minimalIterations = 1u,
const unsigned int threadIndex = (
unsigned int)(-1));
356 bool executeSeparableAndAbortableFunction(
const AbortableFunction& separableAbortableFunction,
const unsigned int first,
const unsigned int size,
const unsigned int firstIndex,
const unsigned int sizeIndex,
const unsigned int abortIndex,
const unsigned int minimalIterations = 1);
371 explicit inline operator bool()
const;
377 inline operator Worker*()
const;
413 ocean_assert(externalSignal_);
415 ocean_assert(!function_);
416 ocean_assert(!abortableFunction_);
418 function_ =
function;
420 internalSignal_.
pulse();
425 ocean_assert(externalSignal_);
427 ocean_assert(!function_);
428 ocean_assert(!abortableFunction_);
430 function_ = std::move(
function);
432 internalSignal_.
pulse();
437 ocean_assert(externalSignal_);
439 ocean_assert(!function_);
440 ocean_assert(!abortableFunction_);
442 abortableFunction_ = abortableFunction;
443 abortIndex_ = abortIndex;
445 internalSignal_.
pulse();
450 ocean_assert(externalSignal_);
452 ocean_assert(!function_);
453 ocean_assert(!abortableFunction_);
455 abortableFunction_ = std::move(abortableFunction);
456 abortIndex_ = abortIndex;
458 internalSignal_.
pulse();
466 inline Worker::operator bool()
const
473 ocean_assert(
false &&
"Use the explicit address operator instead!");
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a signal.
Definition: Signal.h:31
bool pulse()
Pulses this signal.
This class implements a signal array.
Definition: Signal.h:132
This class implements a thread.
Definition: Thread.h:115
This class implements a thread with an explicit external thread function.
Definition: Worker.h:82
unsigned int id_
Id of the worker thread.
Definition: Worker.h:180
WorkerState workerState_
Worker thread result.
Definition: Worker.h:186
virtual ~WorkerThread()
Destructs a worker thread object.
Function function_
Function using the internal thread to execute.
Definition: Worker.h:168
Signal internalSignal_
Internal signal handling the internal thread execution.
Definition: Worker.h:174
WorkerState state() const
Returns the current worker thread state.
Definition: Worker.h:461
WorkerThread(const unsigned int workerSeedValue, const unsigned int workerThreadId)
Creates a new worker thread object.
AbortableFunction abortableFunction_
Abortable function using the internal thread to execute.
Definition: Worker.h:171
virtual void threadRun()
This thread function has to be overloaded by derived classes.
bool start(Signal &signal)
Starts the worker thread.
void setThreadFunction(const Function &function)
Sets a new thread function which will be executed immediately using the internal thread.
Definition: Worker.h:411
WorkerState
Definition of different worker thread states.
Definition: Worker.h:89
@ WS_POSITIVE_RESULT
The worker thread is in a state with positive function result.
Definition: Worker.h:93
@ WS_UNKNOWN_RESULT
The worker thread is in a state with unknown result.
Definition: Worker.h:91
unsigned int id()
Returns the id of this worker thread.
Definition: Worker.h:406
This class implements a worker able to distribute function calls over different threads.
Definition: Worker.h:33
Lock lock_
Worker lock.
Definition: Worker.h:403
Worker & operator=(const Worker &worker)=delete
Disabled copy operator.
Caller< bool > AbortableFunction
Definition of a worker function caller object for abort functions.
Definition: Worker.h:45
StartIndices separation(const unsigned int first, const unsigned int size, const unsigned int minimalIterations=1u)
Returns a separation this worker would apply to execute a distributable function.
unsigned int threads() const
Returns the number of threads this worker uses.
WorkerThreads workerThreads_
Worker threads.
Definition: Worker.h:397
bool executeFunctions(const Functions &functions)
Executes several callback functions concurrently.
std::vector< WorkerThread * > WorkerThreads
Definition of a vector holding worker threads.
Definition: Worker.h:192
Caller< void > Function
Definition of a worker function caller object for standard functions to be distributed.
Definition: Worker.h:40
Signals signals_
Worker signals determining whether all thread finished their work.
Definition: Worker.h:400
~Worker()
Destructs a worker object.
bool executeSeparableAndAbortableFunction(const AbortableFunction &separableAbortableFunction, const unsigned int first, const unsigned int size, const unsigned int firstIndex, const unsigned int sizeIndex, const unsigned int abortIndex, const unsigned int minimalIterations=1)
Executes an abortable and separable function on several CPU cores.
Worker(const LoadType loadType=TYPE_ALL_CORES, const unsigned int maximalNumberCores=16u)
Creates a new worker object.
LoadType
Definition of CPU load types.
Definition: Worker.h:56
@ TYPE_DOUBLE_CORES
For each CPU core two thread are used.
Definition: Worker.h:66
@ TYPE_ALL_CORES
All CPU cores are used.
Definition: Worker.h:64
@ TYPE_ONE_CORE
One CPU core is used.
Definition: Worker.h:58
@ TYPE_ALL_BUT_ONE_CORE
All CPU cores but one, minimum is one CPU core.
Definition: Worker.h:62
@ TYPE_HALF_CORES
Half of the CPU cores are used, minimum is one CPU core.
Definition: Worker.h:60
std::vector< unsigned int > StartIndices
Definition of a vector holding indices.
Definition: Worker.h:74
bool executeFunction(const Function &function, const unsigned int first, const unsigned int size, const unsigned int firstIndex=(unsigned int)(-1), const unsigned int sizeIndex=(unsigned int)(-1), const unsigned int minimalIterations=1u, const unsigned int threadIndex=(unsigned int)(-1))
Executes a callback function separable by two function parameters.
std::vector< Function > Functions
Definition of a vector holding worker functions.
Definition: Worker.h:50
Worker(const Worker &worker)=delete
Disabled copy constructor.
Worker(const unsigned int numberCores, const LoadType loadType)
Creates a new worker object with a custom amount of worker threads.
bool executeAbortableFunction(const AbortableFunction &abortableFunction, const unsigned int abortIndex, const unsigned int maximalExecutions=0u)
Executes an abortable function on several CPU cores.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15