Zth (libzth)
Loading...
Searching...
No Matches
fiber

Classes

struct  zth::setStackSize
 Change the stack size of a fiber returned by zth_async. More...
 
struct  zth::setName
 Change the name of a fiber returned by zth_async. More...
 
struct  zth::passOnExit
 Makes the fiber pass the given gate upon exit. More...
 
struct  zth::asFuture
 Forces the fiber to have a future that outlives the fiber. More...
 
struct  zth::fiber_future< T >
 The future returned by a fiber. More...
 
class  zth::joiner
 RAII class to join fibers and futures on destruction. More...
 
class  zth::PeriodicWakeUp
 Periodic wakeup after fixed interval. More...
 
class  zth::Worker
 The class that manages the fibers within this thread. More...
 

Functions

template<typename F >
fiber_type< F >::factory zth::factory (F &&f, char const *name=nullptr)
 Create a new fiber.
 
template<typename F , typename... Args>
fiber_type< F >::fiber zth::fiber (F &&f, Args &&... args)
 Create and start a new fiber.
 
void * zth::fls () noexcept
 Return the fiber-local storage, as set by setFls().
 
void zth::setFls (void *data=nullptr) noexcept
 Set the fiber-local storage.
 
void zth::waitUntil (TimedWaitable &w)
 Wait until the given Waitable has passed.
 
template<typename F , typename std::enable_if<!std::is_base_of< TimedWaitable, F >::value, int >::type = 0>
void zth::waitUntil (F f, TimeInterval const &pollInterval=TimeInterval())
 Wait until the given function f returns true.
 
template<typename C >
void zth::waitUntil (C &that, bool(C::*f)(), TimeInterval const &pollInterval=TimeInterval())
 Wait until the given member function f returns true.
 
void zth::nap (Timestamp const &sleepUntil)
 Sleep until the given time stamp.
 
void zth::nap (TimeInterval const &sleepFor)
 Sleep for the given time interval.
 
void zth::mnap (long sleepFor_ms)
 Sleep for the given amount of milliseconds.
 
void zth::unap (long sleepFor_us)
 Sleep for the given amount of microseconds.
 
Workerzth::currentWorker () noexcept
 Return the (thread-local) singleton Worker instance.
 
Fiberzth::currentFiber () noexcept
 Return the currently executing fiber.
 
void zth::yield (Fiber *preferFiber=nullptr, bool alwaysYield=false, Timestamp const &now=Timestamp::now())
 Allow a context switch.
 
void zth::outOfWork ()
 Force a context switch.
 
int zth::startWorkerThread (void(*f)(), size_t stack, char const *name)
 Start a new thread, create a Worker, with one fiber, which executes f.
 
int zth::execlp (char const *file, char const *arg,...)
 Start an external program.
 
int zth::execvp (char const *file, char *const arg[])
 Start an external program.
 

Detailed Description

Function Documentation

◆ currentFiber()

Fiber & zth::currentFiber ( )
inlinenoexcept

Return the currently executing fiber.

Examples
4_sync.cpp, 7_coro.cpp, and measure.cpp.

Definition at line 417 of file worker.h.

◆ currentWorker()

Worker & zth::currentWorker ( )
inlinenoexcept

Return the (thread-local) singleton Worker instance.

Definition at line 407 of file worker.h.

◆ execlp()

int zth::execlp ( char const *  file,
char const *  arg,
  ... 
)

Start an external program.

Definition at line 93 of file worker.cpp.

◆ execvp()

int zth::execvp ( char const *  file,
char *const  arg[] 
)

Start an external program.

Definition at line 153 of file worker.cpp.

◆ factory()

template<typename F >
fiber_type< F >::factory zth::factory ( F &&  f,
char const *  name = nullptr 
)

Create a new fiber.

Actually, it returns a factory, that allows passing the fiber arguments afterwards.

Definition at line 1131 of file async.h.

◆ fiber()

template<typename F , typename... Args>
fiber_type< F >::fiber zth::fiber ( F &&  f,
Args &&...  args 
)

Create and start a new fiber.

Parameters
fFunction/lambda/functor to be executed in the fiber.
argsArguments to be passed to f.

This function creates and starts a new fiber in one step. It is equivalent to:

auto f = zth::factory(func)(args...);
fiber_type< F >::factory factory(F &&f, char const *name=nullptr)
Create a new fiber.
Definition async.h:1131
Examples
1_helloworld.cpp, 2_fibers.cpp, 3_coop.cpp, 4_sync.cpp, 5_perf.cpp, daemon_pattern.cpp, fsm14.cpp, measure.cpp, socks.cpp, and zmq.cpp.

Definition at line 1192 of file async.h.

◆ fls()

void * zth::fls ( )
inlinenoexcept

Return the fiber-local storage, as set by setFls().

Definition at line 564 of file fiber.h.

◆ mnap()

void zth::mnap ( long  sleepFor_ms)
inline

Sleep for the given amount of milliseconds.

Examples
5_perf.cpp.

Definition at line 294 of file waiter.h.

◆ nap() [1/2]

void zth::nap ( TimeInterval const &  sleepFor)
inline

Sleep for the given time interval.

Definition at line 284 of file waiter.h.

◆ nap() [2/2]

void zth::nap ( Timestamp const &  sleepUntil)
inline

Sleep until the given time stamp.

Examples
daemon_pattern.cpp, measure.cpp, socks.cpp, and zmq.cpp.

Definition at line 274 of file waiter.h.

◆ outOfWork()

void zth::outOfWork ( )
inline

Force a context switch.

Normally, yield() does not yield when the time slice did not end. This prevents excessive context switching, without actually doing much work in between. However, if there is no work, this function forces a context switch anyway.

Examples
3_coop.cpp, 4_sync.cpp, and 7_coro.cpp.

Definition at line 476 of file worker.h.

◆ setFls()

void zth::setFls ( void *  data = nullptr)
inlinenoexcept

Set the fiber-local storage.

This is just like thread-local storage, but per fiber.

Definition at line 576 of file fiber.h.

◆ startWorkerThread()

int zth::startWorkerThread ( void(*)()  f,
size_t  stack = 0,
char const *  name = nullptr 
)

Start a new thread, create a Worker, with one fiber, which executes f.

Definition at line 62 of file worker.cpp.

◆ unap()

void zth::unap ( long  sleepFor_us)
inline

Sleep for the given amount of microseconds.

Definition at line 303 of file waiter.h.

◆ waitUntil() [1/3]

template<typename C >
void zth::waitUntil ( C &  that,
bool(C::*)()  f,
TimeInterval const &  pollInterval = TimeInterval() 
)

Wait until the given member function f returns true.

Definition at line 260 of file waiter.h.

◆ waitUntil() [2/3]

template<typename F , typename std::enable_if<!std::is_base_of< TimedWaitable, F >::value, int >::type = 0>
void zth::waitUntil ( f,
TimeInterval const &  pollInterval = TimeInterval() 
)

Wait until the given function f returns true.

Definition at line 242 of file waiter.h.

◆ waitUntil() [3/3]

void zth::waitUntil ( TimedWaitable w)

Wait until the given Waitable has passed.

Definition at line 25 of file waiter.cpp.

◆ yield()

void zth::yield ( Fiber preferFiber = nullptr,
bool  alwaysYield = false,
Timestamp const &  now = Timestamp::now() 
)
inline

Allow a context switch.

Parameters
preferFibercontext switch to this fiber. Do normal scheduling when nullptr.
alwaysYieldalways perform a context switch, even if we are within Config::MinTimeslice_s().
nowthe current time stamp
Examples
1_helloworld.cpp, 3_coop.cpp, 4_sync.cpp, and measure.cpp.

Definition at line 454 of file worker.h.