|
|
| 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.
|
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;
...
io.Stop();
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
-
| TPoller | Polling backend; any POSIX poller. |
- Note
- Not available on Windows: it needs TNotifier.
template<typename TPoller>
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.