TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec::ActorWorker< TParams, TActor > Class Template Reference

A Worker that owns and runs an Actor in a dedicated thread. More...

#include <tec_actor_worker.hpp>

Inheritance diagram for tec::ActorWorker< TParams, TActor >:
tec::Worker< TParams > tec::Daemon

Classes

struct  Builder
 Factory for constructing ActorWorker as a Daemon pointer. More...
 

Public Types

using Params = TParams
 Alias for the parameter type.
 
- Public Types inherited from tec::Worker< TParams >
using Params = TParams
 Type alias for worker parameters.
 
using id_t = std::thread::id
 Type alias for thread ID.
 
using Lock = std::lock_guard< std::mutex >
 Type alias for mutex lock guard.
 
using CallbackFunc = std::function< void(Worker< Params > *, const Message &)>
 Type alias for a callback function to process messages.
 

Public Member Functions

 ActorWorker (const Params &params, std::unique_ptr< TActor > actor)
 Constructs an ActorWorker with parameters and actor ownership.
 
 ActorWorker (const ActorWorker &)=delete
 Deleted copy constructor.
 
 ActorWorker (ActorWorker &&)=delete
 Deleted move constructor.
 
 ~ActorWorker () override
 Destructor.
 
- Public Member Functions inherited from tec::Worker< TParams >
 Worker (const Params &params)
 Constructs a worker.
 
virtual ~Worker ()
 Destructor that ensures proper thread termination.
 
id_t id () const
 Retrieves the worker thread's ID.
 
constexpr const Paramsparams () const
 Retrieves the worker's configuration parameters.
 
const Signalsig_terminated () const override
 Retrieves the signal indicating the worker has terminated.
 
void send (Message &&msg) override
 Sends a message to the worker's queue.
 
Status make_request (Request &&req, Reply &&rep) override
 Sends a request and waits for a reply in a daemon thread.
 
template<typename Derived , typename T >
void register_callback (Derived *worker, void(Derived::*callback)(const Message &msg))
 Registers a callback for a specific message type.
 
Status run () override
 Starts the worker thread's message polling.
 
Status terminate () override
 Terminates the worker thread.
 
- Public Member Functions inherited from tec::Daemon
 Daemon ()=default
 Default constructor.
 
virtual ~Daemon ()=default
 Virtual destructor for safe polymorphic deletion.
 
 Daemon (const Daemon &)=delete
 Daemons are non-copyable to ensure unique ownership.
 
 Daemon (Daemon &&)=delete
 Daemons are non-movable to ensure unique ownership.
 
template<typename TRequest , typename TReply >
Status request (const TRequest *req, TReply *rep)
 Helper: Sends a request and waits for a reply in a daemon process.
 
template<typename TRequest >
Status request (const TRequest *req)
 Helper: Sends a notification request – no reply required.
 

Protected Member Functions

Status on_init () override
 Initializes the worker by starting the actor in a background thread.
 
Status on_exit () override
 Shuts down the actor and joins all threads.
 
virtual void on_request (const Message &msg)
 Handles incoming Payload requests synchronously.
 
- Protected Member Functions inherited from tec::Worker< TParams >
virtual void dispatch (const Message &msg)
 Dispatches a message to its registered callback.
 
virtual Status create_thread ()
 Create the Daemon's thread in suspended state.
 

Protected Attributes

std::unique_ptr< TActoractor_
 Owned actor instance.
 
- Protected Attributes inherited from tec::Worker< TParams >
Params params_
 Configuration parameters for the worker.
 

Detailed Description

template<typename TParams, typename TActor>
class tec::ActorWorker< TParams, TActor >

A Worker that owns and runs an Actor in a dedicated thread.

This class integrates the actor model (asynchronous lifecycle) with the worker model (synchronous request handling via Daemon). It:

  • Starts the actor in a background thread during on_init().
  • Routes incoming Daemon::Payload messages to actor_->process_request().
  • Ensures thread-safe, synchronous replies.
  • Gracefully shuts down the actor and joins threads during on_exit().
Template Parameters
TParamsConfiguration parameters (passed to both worker and actor).
TActorConcrete actor type derived from tec::Actor.
Threading Model
  • One dedicated thread runs the actor (actor_thread_).
  • Request processing is serialized via mtx_request_.
  • on_init() and on_exit() are called from the daemon's control thread.

Constructor & Destructor Documentation

◆ ActorWorker() [1/3]

tec::ActorWorker< TParams, TActor >::ActorWorker ( const Params params,
std::unique_ptr< TActor actor 
)
inline

Constructs an ActorWorker with parameters and actor ownership.

Takes ownership of a fully constructed TActor instance. Registers a callback to handle Payload* messages synchronously.

Parameters
paramsConfiguration for the worker and actor.
actorUnique pointer to the concrete actor instance.
Precondition
TActor must derive from tec::Actor.
Example
auto worker = std::make_unique<ActorWorker<Params, GrpcActor>>(
params, std::move(std::make_unique<GrpcActor>(params)));
constexpr const Params & params() const
Retrieves the worker's configuration parameters.
Definition tec_worker.hpp:161

◆ ActorWorker() [2/3]

tec::ActorWorker< TParams, TActor >::ActorWorker ( const ActorWorker< TParams, TActor > &  )
delete

Deleted copy constructor.

ActorWorker manages unique resources (thread, actor) and cannot be copied.

◆ ActorWorker() [3/3]

tec::ActorWorker< TParams, TActor >::ActorWorker ( ActorWorker< TParams, TActor > &&  )
delete

Deleted move constructor.

Move semantics are disabled to prevent thread/actor ownership issues.

◆ ~ActorWorker()

tec::ActorWorker< TParams, TActor >::~ActorWorker ( )
inlineoverride

Destructor.

Ensures the actor thread is joined if still running.

Note
on_exit() should be called explicitly for graceful shutdown.

Member Function Documentation

◆ on_exit()

Status tec::ActorWorker< TParams, TActor >::on_exit ( )
inlineoverrideprotectedvirtual

Shuts down the actor and joins all threads.

Initiates shutdown in a temporary thread to avoid deadlock if the actor is blocked in start(). Waits for sig_stopped_ and joins both threads.

Returns
Status Always returns Status::Ok() (shutdown is best-effort).
Trace Events
  • Logs shutdown initiation and completion.
See also
on_init(), Actor::shutdown()

Reimplemented from tec::Worker< TParams >.

◆ on_init()

Status tec::ActorWorker< TParams, TActor >::on_init ( )
inlineoverrideprotectedvirtual

Initializes the worker by starting the actor in a background thread.

Spawns actor_thread_ and calls actor_->start(). Waits for sig_started_ to be set (with optional timeout in derived classes).

Returns
Status
  • Status::Ok() on success
  • Error::Kind::RuntimeErr if already running
  • Actor-reported error (e.g., timeout, bind failure)
See also
on_exit(), Actor::start()

Reimplemented from tec::Worker< TParams >.

◆ on_request()

virtual void tec::ActorWorker< TParams, TActor >::on_request ( const Message msg)
inlineprotectedvirtual

Handles incoming Payload requests synchronously.

Called by the daemon when a message of type Payload* arrives. Forwards the request to the actor and populates the reply.

Parameters
msgThe incoming message (contains Daemon::Payload*).
Thread Safety
Protected by mtx_request_ to ensure only one request is processed at a time.
RAII Signal
Uses Signal::OnExit to auto-set payload->ready on exit.
See also
Daemon::Payload, Actor::process_request()

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