|
TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
|
A class implementing message processing as a daemon. More...
#include <tec_worker.hpp>
Public Types | |
| 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 | |
| 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 | |
| virtual void | dispatch (const Message &msg) |
| Dispatches a message to its registered callback. | |
| virtual Status | on_init () |
| Default callback invoked during worker thread initialization. | |
| virtual Status | on_exit () |
| Default callback invoked when exiting the worker thread. | |
| virtual Status | create_thread () |
| Create the Daemon's thread in suspended state. | |
Protected Attributes | |
| Params | params_ |
| Configuration parameters for the worker. | |
A class implementing message processing as a daemon.
Implements the Daemon interface to manage a worker thread that processes messages from a SafeQueue. It supports registering callbacks for specific message types, provides default initialization and exit callbacks, and controls thread lifecycle with a signal for termination.
| TParams | The type of parameters used to configure the worker. |
TestWorker parameters and messages. TestWorker implementation. Exposes the TestWorker Worker as a Daemon. Type alias for a callback function to process messages.
Defines a function that takes a Worker pointer and a Message to process.
Destructor that ensures proper thread termination.
Calls terminate() if the thread is joinable to ensure clean shutdown.
Dispatches a message to its registered callback.
Looks up the callback for the message's type in the slots table and invokes it if found.
| msg | The message to dispatch. |
|
inline |
Retrieves the worker thread's ID.
|
inlineoverridevirtual |
Sends a request and waits for a reply in a daemon thread.
This method sends an RPC-style request of type Payload to the daemon and waits for a corresponding reply of type Reply. It uses a signal to synchronize the operation and returns the status of the request processing.
| req | The request object to be sent. |
| rep | The reply object where the response will be stored. |
Implements tec::Daemon.
|
inlineprotectedvirtual |
Default callback invoked when exiting the worker thread.
Called after the message loop, only if on_init() succeeded.
Reimplemented in tec::ActorWorker< TParams, TActor >.
|
inlineprotectedvirtual |
Default callback invoked during worker thread initialization.
Called before entering the message loop. If it returns a status other than Error::Kind::Ok, the worker stops and skips the message loop and on_exit().
Reimplemented in tec::ActorWorker< TParams, TActor >.
|
inlineconstexpr |
Retrieves the worker's configuration parameters.
|
inline |
Registers a callback for a specific message type.
Associates a member function of a derived class with a message type. The callback is stored in the slots table and invoked when a matching message is received. Should not be called while the worker is running.
| Derived | The derived worker class type. |
| T | The message type to associate with the callback. |
| worker | Pointer to the derived worker instance. |
| callback | The member function to call for the message type. |
|
inlineoverridevirtual |
Starts the worker thread's message polling.
Creates the worker thread, signals sig_running_, and waits for initialization to complete. Returns the initialization status.
Implements tec::Daemon.
|
inlineoverridevirtual |
Sends a message to the worker's queue.
Enqueues a message for processing if the worker thread is joinable. Logs the operation using tracing if enabled.
| msg | The message to send. |
Implements tec::Daemon.
|
inlineoverridevirtual |
Retrieves the signal indicating the worker has terminated.
Implements tec::Daemon.
|
inlineoverridevirtual |
Terminates the worker thread.
Sends a null message to stop the message loop if initialized successfully, waits for termination, and joins the thread. Should not be called from the worker thread.
Implements tec::Daemon.