Zth (libzth)
fiber

Classes

class  zth::setStackSize
 Change the stack size of a fiber returned by async. More...
 
class  zth::setName
 Change the name of a fiber returned by async. More...
 
class  zth::passOnExit
 Makes the fiber pass the given gate upon exit. More...
 
class  zth::PeriodicWakeUp
 Periodic wakeup after fixed interval. More...
 
class  zth::Worker
 The class that manages the fibers within this thread. More...
 

Macros

#define zth_fiber_declare(...)
 Do the declaration part of zth_fiber() (to be used in an .h file). More...
 
#define zth_fiber_define(...)
 Do the definition part of zth_fiber() (to be used in a .cpp file). More...
 
#define zth_fiber(...)
 Prepare every given function to become a fiber by async. More...
 
#define async
 Run a function as a new fiber. More...
 

Functions

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

Detailed Description

Macro Definition Documentation

◆ async

#define async

Run a function as a new fiber.

The function must have passed through zth_fiber() (or friends) first. Example:

void foo(int i) { ... }
int main_fiber(int argc, char** argv) {
async foo(42);
return 0;
}
int main_fiber(int argc, char **argv)
Definition: main.cpp:14
#define zth_fiber(...)
Prepare every given function to become a fiber by async.
Definition: async.h:765
#define async
Run a function as a new fiber.
Definition: async.h:789
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 789 of file async.h.

◆ zth_fiber

#define zth_fiber (   ...)

Prepare every given function to become a fiber by async.

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 765 of file async.h.

◆ zth_fiber_declare

#define zth_fiber_declare (   ...)

Do the declaration part of zth_fiber() (to be used in an .h file).

Examples
2_fibers.cpp.

Definition at line 738 of file async.h.

◆ zth_fiber_define

#define zth_fiber_define (   ...)

Do the definition part of zth_fiber() (to be used in a .cpp file).

Examples
2_fibers.cpp.

Definition at line 758 of file async.h.

Function Documentation

◆ currentFiber()

Fiber & zth::currentFiber ( )
inlinenoexcept

Return the currently executing fiber.

Examples
measure.cpp.

Definition at line 398 of file worker.h.

◆ currentWorker()

Worker& zth::currentWorker ( )
inlinenoexcept

Return the (thread-local) singleton Worker instance.

Definition at line 388 of file worker.h.

◆ execlp()

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

Start an external program.

Definition at line 96 of file worker.cpp.

◆ execvp()

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

Start an external program.

Definition at line 155 of file worker.cpp.

◆ fiber()

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

Create a new fiber.

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

Examples
4_sync.cpp, 7_no_async.cpp, and 9_c_api.c.

Definition at line 713 of file async.h.

◆ fls()

void* zth::fls ( )
inlinenoexcept

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

Definition at line 536 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 297 of file waiter.h.

◆ nap() [1/2]

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

Sleep for the given time interval.

Definition at line 287 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 277 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, and 4_sync.cpp.

Definition at line 457 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 548 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 65 of file worker.cpp.

◆ unap()

void zth::unap ( long  sleepFor_us)
inline

Sleep for the given amount of microseconds.

Definition at line 306 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 263 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 245 of file waiter.h.

◆ waitUntil() [3/3]

void zth::waitUntil ( TimedWaitable w)

Wait until the given Waitable has passed.

Definition at line 28 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
3_coop.cpp, 4_sync.cpp, and measure.cpp.

Definition at line 435 of file worker.h.