Handles where a given tasklet is run. More...

#include <tasklets.hh>

Public Member Functions

 TaskletRunner (const TaskletRunner &)=delete
 
 TaskletRunner (unsigned numWorkers)
 Creates a tasklet runner with numWorkers underling threads for doing work. More...
 
 ~TaskletRunner ()
 Destructor. More...
 
bool failure () const
 
int workerThreadIndex () const
 Returns the index of the current worker thread. More...
 
int numWorkerThreads () const
 Returns the number of worker threads for the tasklet runner. More...
 
void dispatch (std::shared_ptr< TaskletInterface > tasklet)
 Add a new tasklet. More...
 
template<class Fn >
std::shared_ptr< FunctionRunnerTasklet< Fn > > dispatchFunction (Fn &fn, int numInvocations=1)
 Convenience method to construct a new function runner tasklet and dispatch it immediately. More...
 
void barrier ()
 Make sure that all tasklets have been completed after this method has been called. More...
 

Protected Member Functions

void run_ ()
 do the work until the queue received an end tasklet More...
 

Static Protected Member Functions

static void startWorkerThread_ (TaskletRunner *taskletRunner, int workerThreadIndex)
 

Protected Attributes

std::vector< std::unique_ptr< std::thread > > threads_
 
std::queue< std::shared_ptr< TaskletInterface > > taskletQueue_
 
std::mutex taskletQueueMutex_
 
std::condition_variable workAvailableCondition_
 

Detailed Description

Handles where a given tasklet is run.

Depending on the number of worker threads, a tasklet can either be run in a separate worker thread or by the main thread.

Constructor & Destructor Documentation

◆ TaskletRunner() [1/2]

Opm::TaskletRunner::TaskletRunner ( const TaskletRunner )
delete

◆ TaskletRunner() [2/2]

Opm::TaskletRunner::TaskletRunner ( unsigned  numWorkers)
inline

Creates a tasklet runner with numWorkers underling threads for doing work.

The number of worker threads may be 0. In this case, all work is done by the main thread (synchronous mode).

References Opm::Parameters::reset(), startWorkerThread_(), and threads_.

◆ ~TaskletRunner()

Opm::TaskletRunner::~TaskletRunner ( )
inline

Destructor.

If worker threads were created to run the tasklets, this method waits until all worker threads have been terminated, i.e. all scheduled tasklets are guaranteed to be completed.

References dispatch(), and threads_.

Member Function Documentation

◆ barrier()

void Opm::TaskletRunner::barrier ( )
inline

Make sure that all tasklets have been completed after this method has been called.

References dispatch(), and threads_.

Referenced by Opm::VtkMultiWriter< GridView, vtkFormat >::beginWrite(), and Opm::VtkMultiWriter< GridView, vtkFormat >::~VtkMultiWriter().

◆ dispatch()

void Opm::TaskletRunner::dispatch ( std::shared_ptr< TaskletInterface tasklet)
inline

Add a new tasklet.

The tasklet is either run immediately or deferred to a separate thread.

References taskletQueue_, taskletQueueMutex_, threads_, and workAvailableCondition_.

Referenced by barrier(), dispatchFunction(), Opm::VtkMultiWriter< GridView, vtkFormat >::endWrite(), and ~TaskletRunner().

◆ dispatchFunction()

template<class Fn >
std::shared_ptr< FunctionRunnerTasklet< Fn > > Opm::TaskletRunner::dispatchFunction ( Fn &  fn,
int  numInvocations = 1 
)
inline

Convenience method to construct a new function runner tasklet and dispatch it immediately.

References dispatch().

◆ failure()

bool Opm::TaskletRunner::failure ( ) const
inline

◆ numWorkerThreads()

int Opm::TaskletRunner::numWorkerThreads ( ) const
inline

Returns the number of worker threads for the tasklet runner.

References threads_.

◆ run_()

void Opm::TaskletRunner::run_ ( )
inlineprotected

do the work until the queue received an end tasklet

References taskletQueue_, taskletQueueMutex_, and workAvailableCondition_.

Referenced by startWorkerThread_().

◆ startWorkerThread_()

static void Opm::TaskletRunner::startWorkerThread_ ( TaskletRunner taskletRunner,
int  workerThreadIndex 
)
inlinestaticprotected

References run_(), and workerThreadIndex().

Referenced by TaskletRunner().

◆ workerThreadIndex()

int Opm::TaskletRunner::workerThreadIndex ( ) const
inline

Returns the index of the current worker thread.

If the current thread is not a worker thread, -1 is returned.

Referenced by startWorkerThread_().

Member Data Documentation

◆ taskletQueue_

std::queue<std::shared_ptr<TaskletInterface> > Opm::TaskletRunner::taskletQueue_
protected

Referenced by dispatch(), and run_().

◆ taskletQueueMutex_

std::mutex Opm::TaskletRunner::taskletQueueMutex_
protected

Referenced by dispatch(), and run_().

◆ threads_

std::vector<std::unique_ptr<std::thread> > Opm::TaskletRunner::threads_
protected

◆ workAvailableCondition_

std::condition_variable Opm::TaskletRunner::workAvailableCondition_
protected

Referenced by dispatch(), and run_().


The documentation for this class was generated from the following file: