CIRCT 24.0.0git
Loading...
Searching...
No Matches
esi::AcceleratorServiceThread Class Reference

Background thread which services various requests. More...

#include <Accelerator.h>

Collaboration diagram for esi::AcceleratorServiceThread:

Classes

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

Public Member Functions

 AcceleratorServiceThread (Context *ctxt=nullptr)
 Exceptions thrown by listener callbacks and tasks are reported to ctxt's logger if given (otherwise to stderr) rather than stopping the thread.
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, Context *ctxt=nullptr)
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.

Private Member Functions

void runReportingErrors (const std::function< void()> &f)
 Run f, reporting any exception it throws.

Private Attributes

Context * ctxt
 Looked up through the context each time, since it may be replaced.

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 241 of file Accelerator.h.

Constructor & Destructor Documentation

◆ AcceleratorServiceThread() [1/2]

esi::AcceleratorServiceThread::AcceleratorServiceThread ( Context * ctxt = nullptr)

Exceptions thrown by listener callbacks and tasks are reported to ctxt's logger if given (otherwise to stderr) rather than stopping the thread.

Definition at line 379 of file Accelerator.cpp.

References ctxt, and start().

◆ ~AcceleratorServiceThread()

esi::AcceleratorServiceThread::~AcceleratorServiceThread ( )
virtual

Definition at line 384 of file Accelerator.cpp.

References stop().

◆ AcceleratorServiceThread() [2/2]

esi::AcceleratorServiceThread::AcceleratorServiceThread ( DeferStart ,
Context * ctxt = nullptr )
explicitprotected

Definition at line 382 of file Accelerator.cpp.

References ctxt.

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 462 of file Accelerator.cpp.

References listeners, and m.

◆ addPoll()

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

Poll this module.

Definition at line 478 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 473 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 396 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 407 of file Accelerator.cpp.

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

Referenced by loop().

◆ runReportingErrors()

void esi::AcceleratorServiceThread::runReportingErrors ( const std::function< void()> & f)
private

Run f, reporting any exception it throws.

Definition at line 441 of file Accelerator.cpp.

References ctxt.

Referenced by poll().

◆ 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 386 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 390 of file Accelerator.cpp.

References me, and shutdown.

Referenced by ~AcceleratorServiceThread().

Member Data Documentation

◆ ctxt

Context* esi::AcceleratorServiceThread::ctxt
private

Looked up through the context each time, since it may be replaced.

Definition at line 308 of file Accelerator.h.

Referenced by AcceleratorServiceThread(), AcceleratorServiceThread(), esiaccel.accelerator.AcceleratorConnection::manifest(), and runReportingErrors().

◆ 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 298 of file Accelerator.h.

Referenced by addListener(), and poll().

◆ m

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

Protects listeners and taskList.

Definition at line 292 of file Accelerator.h.

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

◆ me

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

The thread running loop().

Definition at line 289 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 287 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 301 of file Accelerator.h.

Referenced by addTask(), and poll().


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