Lely core libraries  2.2.5
lely::util::Fiber Class Reference

A fiber. More...

#include <fiber.hpp>

Public Member Functions

 Fiber () noexcept=default
 Creates an invalid fiber. More...
 
 Fiber (Fiber &&other) noexcept
 Moves the state of other to *this. More...
 
template<class F , class = typename ::std::enable_if<!::std::is_same< typename ::std::decay<F>::type, Fiber>::value>::type>
 Fiber (F &&f)
 Equivalent to Fiber::Fiber(f, static_cast<FiberFlag>(0), 0).
 
template<class F >
 Fiber (F &&f, FiberFlag flags)
 Equivalent to Fiber::Fiber(f, flags, 0).
 
template<class F >
 Fiber (F &&f, ::std::size_t stack_size)
 Equivalent to Fiber::Fiber(f, static_cast<FiberFlag>(0), stack_size).
 
template<class F , class = typename ::std::enable_if<compat::is_invocable_r< Fiber, F, Fiber&&>::value>::type>
 Fiber (F &&f, FiberFlag flags, ::std::size_t stack_size)
 Constructs a fiber with a newly allocated stack. More...
 
Fiberoperator= (Fiber &&other) noexcept
 Moves the state of other to *this. More...
 
 ~Fiber ()
 Destroys a Fiber instance. More...
 
 operator bool () const noexcept
 Checks whether *this is a valid fiber.
 
Fiber resume () &&
 Suspends the calling fiber and resumes *this. More...
 
template<class F >
Fiber resume_with (F &&f) &&
 Suspends the calling fiber and resumes *this, but calls f(other) in the resumed fiber as if called by the suspended callable object, where other is the fiber that has been suspended in order to resume the current fiber. More...
 
void swap (Fiber &other) noexcept
 Swaps the states of *this and other.
 

Detailed Description

A fiber.

See also
fiber_t

Definition at line 193 of file fiber.hpp.

Constructor & Destructor Documentation

◆ Fiber() [1/3]

lely::util::Fiber::Fiber ( )
defaultnoexcept

Creates an invalid fiber.

Postcondition
operator bool() returns false.

◆ Fiber() [2/3]

lely::util::Fiber::Fiber ( Fiber &&  other)
inlinenoexcept

Moves the state of other to *this.

Postcondition
other is an invalid fiber (operator bool() returns false).

Definition at line 209 of file fiber.hpp.

◆ Fiber() [3/3]

template<class F , class = typename ::std::enable_if<compat::is_invocable_r< Fiber, F, Fiber&&>::value>::type>
lely::util::Fiber::Fiber ( F &&  f,
FiberFlag  flags,
::std::size_t  stack_size 
)
inline

Constructs a fiber with a newly allocated stack.

The specified callable object is not invoked until the first call to resume() or resume_with().

Parameters
fa callable object with signature Fiber(Fiber&&).
flagsany supported combination of FiberFlag::SAVE_MASK, FiberFlag::SAVE_FENV, FiberFlag::SAVE_ERROR and FiberFlag::GUARD_STACK.
stack_sizethe size (in bytes) of the stack frame to be allocated for the fiber. If 0, the default size (LELY_FIBER_STKSZ) is used. The size of the allocated stack is always at least LELY_FIBER_MINSTKSZ bytes.

Definition at line 242 of file fiber.hpp.

◆ ~Fiber()

lely::util::Fiber::~Fiber ( )
inline

Destroys a Fiber instance.

If the instance represents a fiber of execution (operator bool() returns true), then the fiber of execution is destroyed also. If the callable object with which the fiber was created has not yet terminated, its stack is unwound as if by

::std::move(*this).resume_with([](Fiber&& f) -> Fiber {
throw fiber_unwind(::std::move(f));
return {};
});

To ensure proper destruction, the callable MUST NOT catch the lely::util::fiber_unwind exception (or rethrow it if it does).

Definition at line 289 of file fiber.hpp.

Member Function Documentation

◆ operator=()

Fiber& lely::util::Fiber::operator= ( Fiber &&  other)
inlinenoexcept

Moves the state of other to *this.

Postcondition
other is an invalid fiber (operator bool() returns false).
Returns
*this.

Definition at line 270 of file fiber.hpp.

◆ resume()

Fiber lely::util::Fiber::resume ( ) &&
inline

Suspends the calling fiber and resumes *this.

If *this is an invalid fiber (operator bool() returns false), the fiber associated with the calling thread is resumed.

Returns
the fiber that has been suspended in order to resume the current fiber.
Precondition
the calling thread is the thread on which *this was created.
Postcondition
*this is an invalid fiber (operator bool() returns false).
Exceptions
fiber_unwindif the fiber is being destroyed. This exception MUST NOT be caught (or rethrown if it is).

Definition at line 326 of file fiber.hpp.

◆ resume_with()

template<class F >
Fiber lely::util::Fiber::resume_with ( F &&  f) &&
inline

Suspends the calling fiber and resumes *this, but calls f(other) in the resumed fiber as if called by the suspended callable object, where other is the fiber that has been suspended in order to resume the current fiber.

If *this is an invalid fiber (operator bool() returns false), the fiber associated with the calling thread is resumed.

Returns
the result of f().
Precondition
the calling thread is the thread on which *this was created.
Postcondition
*this is an invalid fiber (operator bool() returns false).
Exceptions
fiber_unwindif the fiber is being destroyed. This exception MUST NOT be caught (or rethrown if it is).

Definition at line 346 of file fiber.hpp.


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