libtasks Documentation  1.6
tasks::dispatcher Class Reference

#include <dispatcher.h>

Collaboration diagram for tasks::dispatcher:

Public Types

enum  mode { mode::SINGLE_LOOP, mode::MULTI_LOOP }
 

Public Member Functions

 dispatcher (uint8_t num_workers)
 
std::shared_ptr< workerfree_worker ()
 Get a free worker to promote it to the leader. More...
 
std::shared_ptr< executorfree_executor ()
 Find a free executor. If non is found a new executor gets created. More...
 
void add_free_worker (uint8_t id)
 When a worker finishes his work he returns to the free worker queue. More...
 
workerlast_worker ()
 
workerget_worker_by_task (event_task *task)
 
void add_task (task *task)
 
void add_event_task (event_task *task)
 Add an event task to the system. More...
 
void add_exec_task (exec_task *task)
 Add an exec task to the system. More...
 
void remove_task (task *task)
 Remove a task from the system. More...
 
void remove_event_task (event_task *task)
 
void remove_exec_task (exec_task *task)
 
void run (int num,...)
 This method starts the system and blocks until terminate() gets called. More...
 
void run (std::vector< tasks::task * > &tasks)
 This method starts the system and blocks until terminate() gets called. More...
 
void start ()
 Start the event loop. Do not block. More...
 
void join ()
 Wait for the dispatcher to finish. More...
 
void terminate ()
 Terminate the workers and die. More...
 
void print_worker_stats () const
 

Static Public Member Functions

static void init_workers (uint8_t num_workers)
 
static void init_run_mode (mode m)
 
static mode run_mode ()
 
static std::shared_ptr
< dispatcher
instance ()
 
static void destroy ()
 
static void add_signal_handler (int sig, signal_func_t func)
 

Private Attributes

std::atomic< bool > m_term
 
std::vector< std::shared_ptr
< worker > > 
m_workers
 All worker threads. More...
 
uint8_t m_num_workers = 0
 
std::list< std::shared_ptr
< executor > > 
m_executors
 All executor threads. More...
 
std::mutex m_executor_mutex
 
tools::bitset m_workers_busy
 State of the workers used for maintaining the leader/followers. More...
 
tools::bitset::int_type m_last_worker_id = 0
 
std::atomic< uint8_t > m_rr_worker_id
 
std::condition_variable m_finish_cond
 Condition variable/mutex used to wait for finishing up. More...
 
std::mutex m_finish_mutex
 
bool m_started = false
 

Static Private Attributes

static std::shared_ptr
< dispatcher
m_instance = nullptr
 
static mode m_run_mode = mode::SINGLE_LOOP
 

Friends

class test_exec
 

Detailed Description

Definition at line 36 of file dispatcher.h.

Member Enumeration Documentation

Enumerator
SINGLE_LOOP 
MULTI_LOOP 

Definition at line 40 of file dispatcher.h.

Constructor & Destructor Documentation

tasks::dispatcher::dispatcher ( uint8_t  num_workers)

Definition at line 26 of file dispatcher.cpp.

Member Function Documentation

void tasks::dispatcher::add_event_task ( event_task task)

Add an event task to the system.

Definition at line 207 of file dispatcher.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void tasks::dispatcher::add_exec_task ( exec_task task)

Add an exec task to the system.

Definition at line 215 of file dispatcher.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void tasks::dispatcher::add_free_worker ( uint8_t  id)

When a worker finishes his work he returns to the free worker queue.

Definition at line 146 of file dispatcher.cpp.

Here is the call graph for this function:

void tasks::dispatcher::add_signal_handler ( int  sig,
signal_func_t  func 
)
static

Add a signal handler. You have to call this before calling start() or run().

Definition at line 287 of file dispatcher.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void tasks::dispatcher::add_task ( task task)

Add a task to the system. This method will find out if the task is an event or exec task and handles it accordingly.

Definition at line 177 of file dispatcher.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

static void tasks::dispatcher::destroy ( )
inlinestatic

Definition at line 83 of file dispatcher.h.

Here is the caller graph for this function:

std::shared_ptr< executor > tasks::dispatcher::free_executor ( )

Find a free executor. If non is found a new executor gets created.

Definition at line 122 of file dispatcher.cpp.

Here is the caller graph for this function:

std::shared_ptr< worker > tasks::dispatcher::free_worker ( )

Get a free worker to promote it to the leader.

Definition at line 109 of file dispatcher.cpp.

Here is the call graph for this function:

worker * tasks::dispatcher::get_worker_by_task ( event_task task)

Return the worker assigned to a task. If no worker is assigned to the given task, a worker is picked depending on the run mode.

Definition at line 151 of file dispatcher.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

static void tasks::dispatcher::init_run_mode ( mode  m)
inlinestatic

Set the run mode. The default is to run a leader/followers system (MODE_SINGLE_LOOP) in which only one event loop exists that is passed from worker to worker. An alternative is to run an event loop in each worker (MODE_MULTI_LOOP). This can improve the responsiveness and throughput in some situations.

Note: This method has to be called before creating the dispatcher singleton. It will fail when called later.

Available Modes: SINGLE_LOOP (Default) MULTI_LOOP

Definition at line 65 of file dispatcher.h.

Here is the caller graph for this function:

static void tasks::dispatcher::init_workers ( uint8_t  num_workers)
inlinestatic

Use this method to override the number of worker threads. The default is the number of CPU's. This method needs to be called before the first call to instance().

Definition at line 47 of file dispatcher.h.

Here is the caller graph for this function:

static std::shared_ptr<dispatcher> tasks::dispatcher::instance ( )
inlinestatic

Definition at line 75 of file dispatcher.h.

Here is the caller graph for this function:

void tasks::dispatcher::join ( )

Wait for the dispatcher to finish.

Definition at line 98 of file dispatcher.cpp.

Here is the caller graph for this function:

worker* tasks::dispatcher::last_worker ( )
inline

Returns the last promoted worker from the workers vector. This can be useful to add tasks in situations where a worker handle is not available.

Definition at line 109 of file dispatcher.h.

void tasks::dispatcher::print_worker_stats ( ) const

Definition at line 171 of file dispatcher.cpp.

void tasks::dispatcher::remove_event_task ( event_task task)

Definition at line 246 of file dispatcher.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void tasks::dispatcher::remove_exec_task ( exec_task task)

Definition at line 274 of file dispatcher.cpp.

Here is the caller graph for this function:

void tasks::dispatcher::remove_task ( task task)

Remove a task from the system.

Definition at line 221 of file dispatcher.cpp.

Here is the call graph for this function:

void tasks::dispatcher::run ( int  num,
  ... 
)

This method starts the system and blocks until terminate() gets called.

Definition at line 32 of file dispatcher.cpp.

Here is the call graph for this function:

void tasks::dispatcher::run ( std::vector< tasks::task * > &  tasks)

This method starts the system and blocks until terminate() gets called.

Definition at line 51 of file dispatcher.cpp.

Here is the call graph for this function:

static mode tasks::dispatcher::run_mode ( )
inlinestatic

Definition at line 73 of file dispatcher.h.

Here is the caller graph for this function:

void tasks::dispatcher::start ( )

Start the event loop. Do not block.

Definition at line 63 of file dispatcher.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void tasks::dispatcher::terminate ( )
inline

Terminate the workers and die.

Definition at line 143 of file dispatcher.h.

Friends And Related Function Documentation

friend class test_exec
friend

Definition at line 37 of file dispatcher.h.

Member Data Documentation

std::mutex tasks::dispatcher::m_executor_mutex
private

Definition at line 160 of file dispatcher.h.

std::list<std::shared_ptr<executor> > tasks::dispatcher::m_executors
private

All executor threads.

Definition at line 159 of file dispatcher.h.

std::condition_variable tasks::dispatcher::m_finish_cond
private

Condition variable/mutex used to wait for finishing up.

Definition at line 174 of file dispatcher.h.

std::mutex tasks::dispatcher::m_finish_mutex
private

Definition at line 175 of file dispatcher.h.

std::shared_ptr< dispatcher > tasks::dispatcher::m_instance = nullptr
staticprivate

Definition at line 151 of file dispatcher.h.

tools::bitset::int_type tasks::dispatcher::m_last_worker_id = 0
private

Definition at line 166 of file dispatcher.h.

uint8_t tasks::dispatcher::m_num_workers = 0
private

Definition at line 156 of file dispatcher.h.

std::atomic<uint8_t> tasks::dispatcher::m_rr_worker_id
private

A round robin counter to add tasks to the system. In case each worker runs it's own event loop this is useful to distribute tasks across the workers.

Definition at line 171 of file dispatcher.h.

dispatcher::mode tasks::dispatcher::m_run_mode = mode::SINGLE_LOOP
staticprivate

Definition at line 162 of file dispatcher.h.

bool tasks::dispatcher::m_started = false
private

Definition at line 177 of file dispatcher.h.

std::atomic<bool> tasks::dispatcher::m_term
private

Definition at line 152 of file dispatcher.h.

std::vector<std::shared_ptr<worker> > tasks::dispatcher::m_workers
private

All worker threads.

Definition at line 155 of file dispatcher.h.

tools::bitset tasks::dispatcher::m_workers_busy
private

State of the workers used for maintaining the leader/followers.

Definition at line 165 of file dispatcher.h.


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