COROIO: NNet::TLoopThread< TPoller > Class Template Reference
COROIO
Loading...
Searching...
No Matches
NNet::TLoopThread< TPoller > Class Template Reference

A thread that runs its own event loop. More...

#include <loop_thread.hpp>

Public Types

using TFailureHook = std::function<void(std::exception_ptr)>
 Called on the loop thread with the exception that ended the loop, see Start().

Public Member Functions

 TLoopThread (const TLoopThread &)=delete
TLoopThread & operator= (const TLoopThread &)=delete
 ~TLoopThread ()
TPoller & Poller ()
 The poller of the loop. See the class description for which thread may use it.
void Start (TFailureHook onFailure={})
 Starts the loop thread. Call once, from the owner thread.
void Stop ()
 Asks the loop to exit. Any thread, any number of times, while the TLoopThread is alive: not concurrently with its destructor.
void Join ()
 Waits for the loop thread to exit. Owner thread only; does nothing if it is not running.
void Submit (TFuture< void, TLazyDetachedPolicy > &&task)
 Starts task on the loop thread, with the poller of the loop as its TCurrentPoller. Any thread.
void Post (std::coroutine_handle<> handle)
 Resumes the suspended coroutine handle on the loop thread. Any thread.

Detailed Description

template<typename TPoller>
class NNet::TLoopThread< TPoller >

A thread that runs its own event loop.

Owns a TLoop<TPoller> and a thread that runs it between Start() and Stop(). The poller is single-threaded, so it has one owner at a time:

Create the sockets, notifiers and coroutines of this poller before Start() and destroy them after Join(). While the loop runs, other threads reach its coroutines only through thread-safe objects: INotifier, TGrowMailbox, and Submit and Post of the TLoopThread.

TLoopThread<TDefaultPoller> io;
TNotifier notifier(io.Poller());
TFuture<void> consumer = Consume(notifier, queue); // runs up to its first wait
io.Start();
...
io.Stop();
io.Join();
// consumer is destroyed here, before io
void Join()
Waits for the loop thread to exit. Owner thread only; does nothing if it is not running.
Definition loop_thread.hpp:131
TPoller & Poller()
The poller of the loop. See the class description for which thread may use it.
Definition loop_thread.hpp:86
void Start(TFailureHook onFailure={})
Starts the loop thread. Call once, from the owner thread.
Definition loop_thread.hpp:99
Thread-safe notification with a single asynchronous listener.
Definition notifier.hpp:97
Definition corochain.hpp:74

Stop() does not destroy coroutines: they stay suspended where they were and are destroyed by their owners after Join(). A stopped loop cannot be started again.

A poller closes a descriptor at its next poll, and after Join() there is none: sockets and notifiers destroyed after Join() stay open until the TLoopThread is destroyed, and only then does a peer see the end of its stream.

An exception that ends the loop (a poller throws std::system_error when its system call fails) does not terminate the process: Join() rethrows it. An owner that must learn of it at once, not at Join(), passes a failure hook to Start().

Template Parameters
TPollerPolling backend; any POSIX poller.
Note
Not available on Windows: it needs TNotifier.

Constructor & Destructor Documentation

◆ ~TLoopThread()

template<typename TPoller>
NNet::TLoopThread< TPoller >::~TLoopThread ( )
inline

Stops the loop and joins the thread. An exception of the loop that Join() has not rethrown is dropped. No other thread may call Stop() during or after this.

Member Function Documentation

◆ Join()

template<typename TPoller>
void NNet::TLoopThread< TPoller >::Join ( )
inline

Waits for the loop thread to exit. Owner thread only; does nothing if it is not running.

Exceptions
Theexception that ended the loop, if there was one; only the first call after it.

◆ Post()

template<typename TPoller>
void NNet::TLoopThread< TPoller >::Post ( std::coroutine_handle<> handle)
inline

Resumes the suspended coroutine handle on the loop thread. Any thread.

Call it from await_suspend to move a coroutine to this loop. A handle that the loop has not resumed when it stops stays suspended, like every coroutine of a stopped loop, and its owner destroys it after Join(). The TLoopThread does not destroy it, because the frame may belong to a TFuture that will. A detached coroutine has no owner and leaks then, so it must have run before Stop(). With g++ 15 an eagerly started coroutine must not post itself from its first suspension: its ramp still writes the frame after that await_suspend returns.

◆ Start()

template<typename TPoller>
void NNet::TLoopThread< TPoller >::Start ( TFailureHook onFailure = {})
inline

Starts the loop thread. Call once, from the owner thread.

Parameters
onFailureOptional. If the loop ends with an exception, it is called with that exception on the loop thread, before the thread exits; Join() still rethrows the exception. A loop that is stopped does not call it. It must not throw: an exception that leaves it terminates the process. Use it when the owner must act at once, for example to end a process that cannot work without this loop.

◆ Stop()

template<typename TPoller>
void NNet::TLoopThread< TPoller >::Stop ( )
inline

Asks the loop to exit. Any thread, any number of times, while the TLoopThread is alive: not concurrently with its destructor.

The loop exits after the step that sees the request, so a coroutine that never returns to the loop delays it. A request made before Start() is kept: the loop then makes one step and exits.

◆ Submit()

template<typename TPoller>
void NNet::TLoopThread< TPoller >::Submit ( TFuture< void, TLazyDetachedPolicy > && task)
inline

Starts task on the loop thread, with the poller of the loop as its TCurrentPoller. Any thread.

Tasks and handles of Post run in the order each thread passed them. A task that has not started when the TLoopThread is destroyed is destroyed with it.


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