Sonde v1.1.0
Embeddable Model Context Protocol server for Qt applications
Loading...
Searching...
No Matches
Sonde::HomeLink Class Reference

Shared state behind ToolCall handles. More...

#include <server_p.h>

Public Member Functions

 HomeLink (QObject *target, QThread *thread=nullptr)
 Links to target, which lives on thread (by default its current thread).
void run (std::function< void()> fn)
 Runs fn on the link's thread: right away if called there, queued otherwise.
void post (std::function< void()> fn)
 Queues fn to the link's thread, even when called there, so it runs from the event loop and never inside the caller.
void clear ()
 Called by the server when it starts being destroyed: from now on, posts from other threads are dropped.
QThread * thread () const
 The thread the link runs code on.

Static Public Member Functions

static std::mutex & handOverMutex ()
 Orders posted functions after their posting, for ThreadSanitizer (see run()).

Private Attributes

QThread * m_thread
 Where run() runs code (fixed at construction).
std::mutex m_mutex
 Guards m_target.
QObject * m_target = nullptr
 Receives posts; nullptr once cleared.

Detailed Description

Shared state behind ToolCall handles.

A thread-safe way to run code on the server's thread, which stays safe while the server is being destroyed.

Shared by the server and its tool calls. Another thread finishing a call posts the reply through run(); the server clears target before it is destroyed, under the same mutex, so nothing is ever posted to a dead object (QMetaObject::invokeMethod() on an object being destroyed in another thread is a race).

Member Function Documentation

◆ post()

void Sonde::HomeLink::post ( std::function< void()> fn)
inline

Queues fn to the link's thread, even when called there, so it runs from the event loop and never inside the caller.

Dropped once cleared.

◆ run()

void Sonde::HomeLink::run ( std::function< void()> fn)
inline

Runs fn on the link's thread: right away if called there, queued otherwise.

Code for the link's own thread always runs inline, even after clear(): that thread is the one tearing the server down, so nothing goes away underneath it, and ~Server needs it to run the cancel callbacks of the calls it ends. Posts from other threads are dropped once cleared.


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