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

Classes

class  zth::setStackSize
 Change the stack size of a fiber returned by zth_async. More...
 
class  zth::setName
 Change the name of a fiber returned by zth_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).
 
#define zth_fiber_define(...)
 Do the definition part of zth_fiber() (to be used in a .cpp file).
 
#define zth_fiber(...)
 Prepare every given function to become a fiber by zth_async.
 
#define zth_async
 Run a function as a new fiber.
 

Functions

template<typename F >
fiber_type< F >::factory zth::fiber (F f, char const *name=nullptr)
 Create 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

Macro Definition Documentation

◆ zth_async

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

◆ zth_fiber

#define zth_fiber (   ...)

Prepare every given function to become a fiber by zth_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 806 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 779 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 799 of file async.h.

Function Documentation

◆ currentFiber()

Fiber & zth::currentFiber ( )
inlinenoexcept

Return the currently executing fiber.

Examples
measure.cpp.

Definition at line 399 of file worker.h.

◆ currentWorker()

Worker & zth::currentWorker ( )
inlinenoexcept

Return the (thread-local) singleton Worker instance.

Definition at line 389 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.

◆ 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
7_no_async.cpp.

Definition at line 754 of file async.h.

◆ fls()

void * zth::fls ( )
inlinenoexcept

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

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

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

Definition at line 436 of file worker.h.