CIRCT 23.0.0git
Loading...
Searching...
No Matches
Classes | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
esi::AcceleratorServiceThread Class Reference

Background thread which services various requests. More...

#include <Accelerator.h>

Collaboration diagram for esi::AcceleratorServiceThread:
Collaboration graph
[legend]

Classes

struct  DeferStart
 Tag type used to construct the base class without starting the service thread. More...
 

Public Member Functions

 AcceleratorServiceThread ()
 
virtual ~AcceleratorServiceThread ()
 
virtual void addListener (std::initializer_list< ReadChannelPort * > listenPorts, std::function< void(ReadChannelPort *, MessageData)> callback)
 When there's data on any of the listenPorts, call the callback.
 
virtual void addPoll (HWModule &module)
 Poll this module.
 
virtual void addTask (std::function< void(void)> task)
 Add a task to be invoked on every service iteration.
 
virtual void stop ()
 Instruct the service thread to stop running.
 

Protected Member Functions

 AcceleratorServiceThread (DeferStart)
 
virtual void start ()
 Start the service thread.
 
virtual void loop ()
 The main service loop.
 
virtual void poll ()
 Perform a single iteration of servicing: drain any ready listener futures, invoke their callbacks, and run all registered tasks.
 

Protected Attributes

std::atomic< bool > shutdown {false}
 Set to true to request the service loop to exit.
 
std::thread me
 The thread running loop().
 
std::mutex m
 Protects listeners and taskList.
 
std::map< ReadChannelPort *, std::pair< std::function< void(ReadChannelPort *, MessageData)>, std::future< MessageData > > > listeners
 Map of read ports to callbacks and their in-flight async reads.
 
std::vector< std::function< void(void)> > taskList
 Tasks which should be called on every loop iteration.
 

Detailed Description

Background thread which services various requests.

Currently, it listens on ports and calls callbacks for incoming messages on said ports.

All methods are virtual so that users may provide their own service thread implementation. Overriders can reuse the default implementation piecemeal by invoking the base-class methods – typically overriding poll() (to change what a single iteration does) or loop() (to change how iterations are scheduled). Subclasses that need to run their own loop() from the outset should use the protected DeferStart constructor to prevent the base class from starting the thread, then call start() themselves.

Definition at line 240 of file Accelerator.h.

Constructor & Destructor Documentation

◆ AcceleratorServiceThread() [1/2]

esi::AcceleratorServiceThread::AcceleratorServiceThread ( )

Definition at line 379 of file Accelerator.cpp.

References start().

◆ ~AcceleratorServiceThread()

esi::AcceleratorServiceThread::~AcceleratorServiceThread ( )
virtual

Definition at line 381 of file Accelerator.cpp.

References stop().

◆ AcceleratorServiceThread() [2/2]

esi::AcceleratorServiceThread::AcceleratorServiceThread ( DeferStart  )
explicitprotected

Definition at line 380 of file Accelerator.cpp.

Member Function Documentation

◆ addListener()

void esi::AcceleratorServiceThread::addListener ( std::initializer_list< ReadChannelPort * >  listenPorts,
std::function< void(ReadChannelPort *, MessageData)>  callback 
)
virtual

When there's data on any of the listenPorts, call the callback.

Callable from any thread.

Definition at line 441 of file Accelerator.cpp.

References listeners, and m.

◆ addPoll()

void esi::AcceleratorServiceThread::addPoll ( HWModule module)
virtual

Poll this module.

Definition at line 457 of file Accelerator.cpp.

References addTask().

◆ addTask()

void esi::AcceleratorServiceThread::addTask ( std::function< void(void)>  task)
virtual

Add a task to be invoked on every service iteration.

Callable from any thread.

Definition at line 452 of file Accelerator.cpp.

References m, and taskList.

Referenced by addPoll().

◆ loop()

void esi::AcceleratorServiceThread::loop ( )
protectedvirtual

The main service loop.

Runs until shutdown is set to true. The default implementation yields and then calls poll() on every iteration.

Definition at line 393 of file Accelerator.cpp.

References poll(), and shutdown.

Referenced by start().

◆ poll()

void esi::AcceleratorServiceThread::poll ( )
protectedvirtual

Perform a single iteration of servicing: drain any ready listener futures, invoke their callbacks, and run all registered tasks.

Overriders can invoke this from a custom loop to reuse the default servicing behavior.

Definition at line 404 of file Accelerator.cpp.

References assert(), listeners, m, and taskList.

Referenced by loop().

◆ start()

void esi::AcceleratorServiceThread::start ( )
protectedvirtual

Start the service thread.

Called by the default constructor. Spawns a std::thread which invokes loop() via virtual dispatch.

Definition at line 383 of file Accelerator.cpp.

References loop(), and me.

Referenced by AcceleratorServiceThread().

◆ stop()

void esi::AcceleratorServiceThread::stop ( )
virtual

Instruct the service thread to stop running.

Definition at line 387 of file Accelerator.cpp.

References me, and shutdown.

Referenced by ~AcceleratorServiceThread().

Member Data Documentation

◆ listeners

std::map<ReadChannelPort *, std::pair<std::function<void(ReadChannelPort *, MessageData)>, std::future<MessageData> > > esi::AcceleratorServiceThread::listeners
protected

Map of read ports to callbacks and their in-flight async reads.

Definition at line 294 of file Accelerator.h.

Referenced by addListener(), and poll().

◆ m

std::mutex esi::AcceleratorServiceThread::m
protected

Protects listeners and taskList.

Definition at line 288 of file Accelerator.h.

Referenced by addListener(), addTask(), and poll().

◆ me

std::thread esi::AcceleratorServiceThread::me
protected

The thread running loop().

Definition at line 285 of file Accelerator.h.

Referenced by start(), and stop().

◆ shutdown

std::atomic<bool> esi::AcceleratorServiceThread::shutdown {false}
protected

Set to true to request the service loop to exit.

Definition at line 283 of file Accelerator.h.

Referenced by loop(), and stop().

◆ taskList

std::vector<std::function<void(void)> > esi::AcceleratorServiceThread::taskList
protected

Tasks which should be called on every loop iteration.

Definition at line 297 of file Accelerator.h.

Referenced by addTask(), and poll().


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