|
JH-Toolkit v1.4.1
An engineering-oriented C++20 toolkit with duck-typed concepts, static design, async coroutines, and semantic containers — header-only, RTTI-free, and concurrency-friendly.
|
Coroutine-based fiber providing manual suspension and resumption. More...
#include <jh/asynchronous/fiber.h>
Classes | |
| struct | promise_type |
Public Member Functions | |
| fiber (std::coroutine_handle< promise_type > h) noexcept | |
| Constructs a fiber from a coroutine handle. | |
| ~fiber () | |
| Destroys the fiber and its coroutine frame if valid. | |
| fiber (const fiber &)=delete | |
| Copy construction is disabled. | |
| fiber & | operator= (const fiber &)=delete |
| Copy assignment is disabled. | |
| fiber (fiber &&other) noexcept | |
| Move constructor transferring ownership of the coroutine handle. | |
| fiber & | operator= (fiber &&other) noexcept |
| Move assignment operator transferring ownership of the coroutine handle. | |
| bool | done () const noexcept |
| Checks whether the fiber has reached its final suspend point. | |
| bool | resume () |
| Resumes execution of the fiber. | |
Public Attributes | |
| std::coroutine_handle< promise_type > | co_ro |
| Handle to the coroutine. | |
Coroutine-based fiber providing manual suspension and resumption.
A fiber models a manually advanced coroutine, conceptually equivalent to a thread blocked on std::condition_variable.wait():
execution is suspended until an explicit external resume request is issued. Unlike an OS thread, a fiber is extremely lightweight because it stores only a coroutine frame, not a stack.
Suspension inside the coroutine is performed using co_await resume_tag, which introduces a controlled wait point analogous to condition_variable.wait():
resume(). Exception propagation is disabled inside a fiber. Any uncaught exception results in a call to std::terminate(), matching the semantics of a thread whose entry function throws without being caught. This avoids continuing execution after a fatal error.
resume() drive progress. co_await resume_tag provides explicit suspension points. std::terminate(), matching thread behavior.
|
inlineexplicitnoexcept |
Constructs a fiber from a coroutine handle.
The passed handle represents an allocated coroutine frame created by promise_type.get_return_object(). Ownership is adopted by the fiber instance, which is responsible for destroying the frame when no longer needed.
| h | The coroutine handle associated with this fiber. |
|
inline |
Destroys the fiber and its coroutine frame if valid.
If the internal coroutine handle is non-null, its coroutine frame is destroyed via destroy(). This fully releases all memory associated with the coroutine.
|
delete |
Copy construction is disabled.
A fiber represents exclusive ownership of a coroutine frame; copying the handle would produce double-destruction and is therefore prohibited.
|
inlinenoexcept |
Move constructor transferring ownership of the coroutine handle.
| other | Source fiber whose handle will be moved from. |
|
inlinenodiscardnoexcept |
Checks whether the fiber has reached its final suspend point.
A fiber is considered complete if:
done() == true. true if the coroutine is finished; otherwise false. Move assignment operator transferring ownership of the coroutine handle.
Destroys the current coroutine frame if present, then adopts the frame from other. The source fiber is left in a null state.
| other | Source fiber. |
*this.
|
inline |
Resumes execution of the fiber.
If the fiber is already complete, the function returns false. Otherwise, it resumes the coroutine, advancing it until the next suspension point (co_await resume_tag) or final completion.
true if further progress is possible; false if the fiber has finished execution.