|
TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
|
A Worker that owns and runs an Actor in a dedicated thread.
More...
#include <tec_actor_worker.hpp>
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 ¶ms, 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 ¶ms) | |
| Constructs a worker. | |
| virtual | ~Worker () |
| Destructor that ensures proper thread termination. | |
| id_t | id () const |
| Retrieves the worker thread's ID. | |
| constexpr const Params & | params () const |
| Retrieves the worker's configuration parameters. | |
| const Signal & | sig_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< TActor > | actor_ |
| Owned actor instance. | |
Protected Attributes inherited from tec::Worker< TParams > | |
| Params | params_ |
| Configuration parameters for the worker. | |
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:
on_init().Daemon::Payload messages to actor_->process_request().on_exit().| TParams | Configuration parameters (passed to both worker and actor). |
| TActor | Concrete actor type derived from tec::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.
| params | Configuration for the worker and actor. |
| actor | Unique pointer to the concrete actor instance. |
TActor must derive from tec::Actor.
|
delete |
Deleted copy constructor.
ActorWorker manages unique resources (thread, actor) and cannot be copied.
|
delete |
Deleted move constructor.
Move semantics are disabled to prevent thread/actor ownership issues.
|
inlineoverride |
Destructor.
Ensures the actor thread is joined if still running.
on_exit() should be called explicitly for graceful shutdown.
|
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.
Status::Ok() (shutdown is best-effort).Reimplemented from tec::Worker< TParams >.
|
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).
Status::Ok() on successError::Kind::RuntimeErr if already runningReimplemented from tec::Worker< TParams >.
|
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.
| msg | The incoming message (contains Daemon::Payload*). |
mtx_request_ to ensure only one request is processed at a time.Signal::OnExit to auto-set payload->ready on exit.