Jobrocket
jobrocket::Worker Class Reference

#include <Worker.hpp>

Overview

A representation of a worker thread in a scheduler. Each Worker owns a thread and its own work-stealing queue. The workers main process will keep popping jobs off its own queue until there are none left, in which case it will start stealing jobs from other workers to help with the overall work-load of the owning scheduler. Once no jobs are queued, the worker will go to sleep until signalled by its scheduler - for instance when a new job is scheduled.

Public Types

enum  State { State::ready, State::running, State::waiting, State::terminated }
 Represents the running state of a Worker More...
 

Public Member Functions

 Worker ()=default
 
 Worker (const uint32_t id, std::vector< Worker > *workers, const uint32_t max_jobs, std::mutex *parent_mutex, std::condition_variable *parent_cv, detail::AtomicCounter *parent_counter)
 
 ~Worker ()
 
 Worker (const Worker &other)=delete
 
Workeroperator= (const Worker &)=delete
 
 Worker (Worker &&other) noexcept
 
Workeroperator= (Worker &&other) noexcept
 
void start ()
 Starts the workers main procedure and forks a new thread to run jobs on. More...
 
void terminate ()
 Terminates the thread but doesn't join it to the parent thread. More...
 
void join ()
 Joins the worker, waiting until its completely terminated before destruction. More...
 
void schedule_job (Job *job)
 Schedules a new job to the worker and notifies it to wake up. More...
 
bool owns_this_thread ()
 Checks if the current thread is owned by this worker, returns true if so. More...
 
State state ()
 Gets workers current running state. More...
 
uint32_t id ()
 Gets the workers id. More...
 
void try_run_job ()
 Attempts to pop or steal a job and execute it once. More...
 

Member Enumeration Documentation

◆ State

Represents the running state of a Worker

Enumerator
ready 
running 
waiting 
terminated 

Constructor & Destructor Documentation

◆ Worker() [1/4]

jobrocket::Worker::Worker ( )
default

◆ Worker() [2/4]

jobrocket::Worker::Worker ( const uint32_t  id,
std::vector< Worker > *  workers,
const uint32_t  max_jobs,
std::mutex *  parent_mutex,
std::condition_variable *  parent_cv,
detail::AtomicCounter parent_counter 
)
inline

◆ ~Worker()

jobrocket::Worker::~Worker ( )
inline

◆ Worker() [3/4]

jobrocket::Worker::Worker ( const Worker other)
delete

◆ Worker() [4/4]

jobrocket::Worker::Worker ( Worker &&  other)
inlinenoexcept

Member Function Documentation

◆ id()

uint32_t jobrocket::Worker::id ( )
inline

Gets the workers id.

◆ join()

void jobrocket::Worker::join ( )

Joins the worker, waiting until its completely terminated before destruction.

◆ operator=() [1/2]

Worker& jobrocket::Worker::operator= ( const Worker )
delete

◆ operator=() [2/2]

Worker& jobrocket::Worker::operator= ( Worker &&  other)
inlinenoexcept

◆ owns_this_thread()

bool jobrocket::Worker::owns_this_thread ( )
inline

Checks if the current thread is owned by this worker, returns true if so.

◆ schedule_job()

void jobrocket::Worker::schedule_job ( Job job)

Schedules a new job to the worker and notifies it to wake up.

◆ start()

void jobrocket::Worker::start ( )

Starts the workers main procedure and forks a new thread to run jobs on.

◆ state()

State jobrocket::Worker::state ( )
inline

Gets workers current running state.

◆ terminate()

void jobrocket::Worker::terminate ( )

Terminates the thread but doesn't join it to the parent thread.

◆ try_run_job()

void jobrocket::Worker::try_run_job ( )

Attempts to pop or steal a job and execute it once.