libtasks Documentation  1.6
tasks::worker Class Reference

#include <worker.h>

Collaboration diagram for tasks::worker:

Public Member Functions

 worker (uint8_t id, std::unique_ptr< loop_t > &loop)
 
virtual ~worker ()
 
uint8_t id () const
 Return the worker id. More...
 
std::string get_string () const
 
struct ev_loop * loop_ptr () const
 Return the event loop pointer for this worker. More...
 
bool exec_in_worker_ctx (task_func_t f)
 
bool signal_call (task_func_t f)
 Same as exec_in_ctx(task_func_t f) More...
 
void async_call (task_func_t f)
 Put a functor into the async work queue of a worker and notify it. More...
 
void set_event_loop (std::unique_ptr< loop_t > &loop)
 Pass the event loop to the worker. More...
 
void terminate ()
 Terminate the worker and wait for it to finish. More...
 
void add_event (event e)
 Add an event to the workers queue. More...
 
void handle_io_event (ev_io *watcher, int revents)
 Handle an I/O event. More...
 
void handle_timer_event (ev_timer *watcher)
 Handle a timer event. More...
 
uint64_t events_count () const
 Return the number of events the worker has handled until now. More...
 

Static Public Member Functions

static worker * get ()
 Provide access to the executing worker thread object in the current thread context. More...
 
static void add_async_event (event e)
 Add an event to the workers queue from a different thread context. More...
 

Private Member Functions

void promote_leader ()
 Find a free worker and promote it to become the next leader. More...
 
void mark_free ()
 Let the dispatcher know that this thread is free to become a leader and take on some work. More...
 
void run ()
 Main method of the thread. More...
 

Private Attributes

uint8_t m_id
 A thread local pointer to the worker thread. More...
 
uint64_t m_events_count = 0
 
std::unique_ptr< loop_t > m_loop
 
std::atomic< bool > m_term
 
std::atomic< bool > m_leader
 
std::mutex m_work_mutex
 
std::condition_variable m_work_cond
 
std::queue< event > m_events_queue
 
ev_async m_signal_watcher
 
std::unique_ptr< std::thread > m_thread
 

Static Private Attributes

static thread_local worker * m_worker_ptr = nullptr
 

Detailed Description

Definition at line 55 of file worker.h.

Constructor & Destructor Documentation

tasks::worker::worker ( uint8_t  id,
std::unique_ptr< loop_t > &  loop 
)

Definition at line 19 of file worker.cpp.

Here is the call graph for this function:

tasks::worker::~worker ( )
virtual

Definition at line 38 of file worker.cpp.

Here is the call graph for this function:

Member Function Documentation

static void tasks::worker::add_async_event ( event  e)
inlinestatic

Add an event to the workers queue from a different thread context.

Definition at line 141 of file worker.h.

Here is the call graph for this function:

Here is the caller graph for this function:

void tasks::worker::add_event ( event  e)
inline

Add an event to the workers queue.

Definition at line 138 of file worker.h.

Here is the caller graph for this function:

void tasks::worker::async_call ( task_func_t  f)
inline

Put a functor into the async work queue of a worker and notify it.

Definition at line 107 of file worker.h.

Here is the caller graph for this function:

uint64_t tasks::worker::events_count ( ) const
inline

Return the number of events the worker has handled until now.

Definition at line 158 of file worker.h.

bool tasks::worker::exec_in_worker_ctx ( task_func_t  f)
inline

Executes task_func_t directly if called in leader thread context or delegates it. Returns true when task_func_t has been executed. If some work needs to be executed in the context of a worker thread (eg to modify a watcher) this method needs to be used.

Definition at line 89 of file worker.h.

Here is the call graph for this function:

Here is the caller graph for this function:

static worker* tasks::worker::get ( )
inlinestatic

Provide access to the executing worker thread object in the current thread context.

Definition at line 64 of file worker.h.

Here is the caller graph for this function:

std::string tasks::worker::get_string ( ) const
inline

Definition at line 66 of file worker.h.

Here is the caller graph for this function:

void tasks::worker::handle_io_event ( ev_io *  watcher,
int  revents 
)

Handle an I/O event.

void tasks::worker::handle_timer_event ( ev_timer *  watcher)

Handle a timer event.

uint8_t tasks::worker::id ( ) const
inline

Return the worker id.

Definition at line 61 of file worker.h.

struct ev_loop* tasks::worker::loop_ptr ( ) const
inline

Return the event loop pointer for this worker.

Definition at line 73 of file worker.h.

Here is the call graph for this function:

void tasks::worker::mark_free ( )
inlineprivate

Let the dispatcher know that this thread is free to become a leader and take on some work.

Definition at line 227 of file worker.h.

Here is the call graph for this function:

Here is the caller graph for this function:

void tasks::worker::promote_leader ( )
inlineprivate

Find a free worker and promote it to become the next leader.

Definition at line 214 of file worker.h.

Here is the call graph for this function:

Here is the caller graph for this function:

void tasks::worker::run ( )
private

Main method of the thread.

Definition at line 44 of file worker.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void tasks::worker::set_event_loop ( std::unique_ptr< loop_t > &  loop)
inline

Pass the event loop to the worker.

Definition at line 117 of file worker.h.

bool tasks::worker::signal_call ( task_func_t  f)
inline

Same as exec_in_ctx(task_func_t f)

Definition at line 102 of file worker.h.

Here is the call graph for this function:

void tasks::worker::terminate ( )
inline

Terminate the worker and wait for it to finish.

Definition at line 125 of file worker.h.

Member Data Documentation

uint64_t tasks::worker::m_events_count = 0
private

Definition at line 194 of file worker.h.

std::queue<event> tasks::worker::m_events_queue
private

Definition at line 200 of file worker.h.

uint8_t tasks::worker::m_id
private

A thread local pointer to the worker thread.

Definition at line 193 of file worker.h.

std::atomic<bool> tasks::worker::m_leader
private

Definition at line 197 of file worker.h.

std::unique_ptr<loop_t> tasks::worker::m_loop
private

Definition at line 195 of file worker.h.

ev_async tasks::worker::m_signal_watcher
private

Every worker has an async watcher to be able to call into the leader thread context.

Definition at line 209 of file worker.h.

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

Definition at line 196 of file worker.h.

std::unique_ptr<std::thread> tasks::worker::m_thread
private

Definition at line 211 of file worker.h.

std::condition_variable tasks::worker::m_work_cond
private

Definition at line 199 of file worker.h.

std::mutex tasks::worker::m_work_mutex
private

Definition at line 198 of file worker.h.

thread_local worker * tasks::worker::m_worker_ptr = nullptr
staticprivate

Definition at line 189 of file worker.h.


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