Zth (libzth)
zth Namespace Reference

Namespaces

 fibered
 
 fsm
 
 guards
 
 impl
 
 io
 
 zmq
 

Classes

struct  vector_type
 std::vector type using Config::Allocator::type. More...
 
struct  map_type
 std::map type using Config::Allocator::type. More...
 
struct  list_type
 std::list type using Config::Allocator::type. More...
 
class  TypedFiber
 
class  FiberManipulator
 
class  setStackSize
 Change the stack size of a fiber returned by async. More...
 
class  setName
 Change the name of a fiber returned by async. More...
 
class  passOnExit
 Makes the fiber pass the given gate upon exit. More...
 
class  AutoFuture
 
class  TypedFiberN
 
class  TypedFiberN< void, Args... >
 
struct  TypedFiberType
 
struct  TypedFiberType< R(*)(Args...)>
 
class  TypedFiberFactory
 
struct  fiber_type_impl
 
struct  fiber_type
 
struct  fiber_type< R(*)(Args...)>
 
struct  Env
 
struct  DefaultConfig
 
class  Context
 
struct  ContextAttr
 
class  Fiber
 The fiber. More...
 
class  Runnable
 An abstract class, that can be started as a fiber. More...
 
class  FsmGuard
 A guard is evaluated, and when true, the corresponding transition is taken. More...
 
struct  FsmDescription
 The description of a Fsm. More...
 
class  FsmCompiler
 A compiler to turn an zth::FsmDescription into something that can be used by zth::Fsm. More...
 
class  Fsm
 A Finite-state machine. More...
 
class  FsmCallback
 
class  FsmCallback< State_, void, Input_, FsmImpl_ >
 
class  List
 
class  SortedList
 
class  Listable
 
class  Backtrace
 Save a backtrace. More...
 
struct  PerfEvent
 An event to be processed by perf_event(). More...
 
struct  PerfEvent< false >
 
class  Load
 Measure the load of some activity. More...
 
class  EventRate
 Measure the rate of some event in Hz. More...
 
struct  Pollable
 A pollable thing. More...
 
struct  PollableFd
 A pollable file descriptor. More...
 
class  PollerInterface
 Abstract base class of a poller. More...
 
class  PollerClientBase
 The abstract base class of a Poller client. More...
 
class  PollerServerBase
 Abstract base class of a Poller server. More...
 
class  PollerServer
 Poller to be executed by the Waiter. More...
 
class  ZmqPoller
 A PollerServer that uses zmq_poll(). More...
 
class  PollerClient
 The poller to be used by a fiber. More...
 
struct  Register
 Helper class to read/write (bitfields in) hardware registers. More...
 
class  RefCounted
 
class  SharedPointer
 
class  Synchronizer
 
class  Mutex
 Fiber-aware mutex. More...
 
class  Semaphore
 Fiber-aware semaphore. More...
 
class  Signal
 Fiber-aware signal. More...
 
class  Future
 Fiber-aware future. More...
 
class  Future< void >
 
class  Gate
 Fiber-aware barrier/gate. More...
 
class  TimeInterval
 Convenient wrapper around struct timespec that contains a time interval. More...
 
class  Timestamp
 Convenient wrapper around struct timespec that contains an absolute timestamp. More...
 
class  cow_string
 Copy-on-write string. More...
 
class  UniqueIDBase
 
class  UniqueID
 Keeps track of a process-wide unique ID within the type T. More...
 
struct  choose_type
 
struct  choose_type< void, WhenTIsVoid >
 
struct  Sequence
 
class  safe_ptr
 Wrapper for a pointer, which checks validity of the pointer upon dereference. More...
 
class  Singleton
 Singleton pattern. More...
 
class  ThreadLocalSingleton
 Singleton pattern, but only per-thread. More...
 
class  small_vector
 A simple std::vector, which can contain Prealloc without heap allocation. More...
 
struct  smallest_uint_size
 
struct  smallest_uint< x, smallest_uint_size< 1 > >
 
struct  smallest_uint< x, smallest_uint_size< 2 > >
 
struct  smallest_uint< x, smallest_uint_size< 4 > >
 
class  Waitable
 
class  TimedWaitable
 
class  PolledWaiting
 
struct  PolledMemberWaitingHelper
 
class  PolledMemberWaiting
 
class  Waiter
 A single fiber per Worker that manages sleeping and blocked fibers. More...
 
class  PeriodicWakeUp
 Periodic wakeup after fixed interval. More...
 
class  Worker
 The class that manages the fibers within this thread. More...
 
struct  Config
 The configuration of Zth. More...
 
class  PerfFiber
 

Typedefs

typedef vector_type< PerfEvent<> >::type perf_eventBuffer_type
 
typedef ZmqPoller DefaultPollerServer
 The poller server, by default instantiated by the zth::Waiter. More...
 
typedef PollerClient Poller
 The default Poller to use. More...
 
typedef std::basic_string< char, std::char_traits< char >, Config::Allocator< char >::type > string
 std::string type using Config::Allocator::type. More...
 

Functions

template<typename Traits , typename Allocator >
string to_zth_string (std::basic_string< char, Traits, Allocator > const &s)
 
std::string to_std_string (string const &s)
 
template<typename R , typename F >
TypedFiber< R, F > & operator<< (TypedFiber< R, F > &f, FiberManipulator const &m)
 
template<typename F >
fiber_type< F >::factory fiber (F f, char const *name=nullptr)
 Create a new fiber. More...
 
bool config (int env, bool whenUnset)
 Checks if a given environment option is set. More...
 
int context_init () noexcept
 One-time context mechanism initialization. More...
 
void context_deinit () noexcept
 Final cleanup. More...
 
int context_create (Context *&context, ContextAttr const &attr) noexcept
 Create a context. More...
 
void context_switch (Context *from, Context *to) noexcept
 Perform context switch. More...
 
void context_destroy (Context *context) noexcept
 Destroy and cleanup a context. More...
 
size_t context_stack_usage (Context *context) noexcept
 Return the high water mark of the stack of the given context. More...
 
void stack_watermark_init (void *stack, size_t size) noexcept
 Initialize the memory region for stack high water marking. More...
 
size_t stack_watermark_size (void *stack) noexcept
 Return the size of the given stack region. More...
 
size_t stack_watermark_maxused (void *stack) noexcept
 Return the high water mark of the stack. More...
 
size_t stack_watermark_remaining (void *stack) noexcept
 Return the remaining stack size that was never touched. More...
 
template<typename R , typename... A, typename... A_>
std::enable_if<!std::is_void< R >::value, R >::type stack_switch (void *stack, size_t size, R(*f)(A...) noexcept, A_ &&... a) noexcept
 Call the function f using the new stack pointer. More...
 
template<typename... A, typename... A_>
void stack_switch (void *stack, size_t size, void(*f)(A...) noexcept, A_ &&... a) noexcept
 Call the function f using the new stack pointer. More...
 
FibercurrentFiber () noexcept
 Return the currently executing fiber. More...
 
void * fls () noexcept
 Return the fiber-local storage, as set by setFls(). More...
 
void setFls (void *data=nullptr) noexcept
 Set the fiber-local storage. More...
 
int perf_init ()
 
void perf_deinit ()
 
UniqueID< Fiber > const & currentFiberID () noexcept
 
void perf_flushEventBuffer () noexcept
 
template<typename... Args>
void perf_event (Args &&... args) noexcept
 
void perf_mark (char const *marker)
 Put a string marker into the perf output. More...
 
void perf_log (char const *fmt,...)
 Put a formatted log string into the perf output. More...
 
void perf_logv (char const *fmt, va_list args)
 Put a formatted log string into the perf output. More...
 
void perf_syscall (char const *syscall, Timestamp const &t=Timestamp())
 Put a syscall into the perf output. More...
 
template<typename P >
int poll (P pollable, int timeout_ms=-1)
 Fiber-aware poll() for a single pollable thing. More...
 
template<>
cow_string str< TimeInterval const & > (TimeInterval const &value)
 
constexpr TimeInterval operator""_s (unsigned long long int x) noexcept
 Define literals like 123_s, which is a zth::TimeInterval of 123 seconds. More...
 
constexpr TimeInterval operator""_ms (unsigned long long int x) noexcept
 Define literals like 123_ms, which is a zth::TimeInterval of 123 milliseconds. More...
 
constexpr TimeInterval operator""_us (unsigned long long int x) noexcept
 Define literals like 123_us, which is a zth::TimeInterval of 123 microseconds. More...
 
TimeInterval operator""_s (long double x)
 Define literals like 12.3_s, which is a zth::TimeInterval of 12.3 seconds. More...
 
char const * banner () noexcept
 Prints a banner line with version and configuration information. More...
 
void abort (char const *fmt,...) noexcept
 Aborts the process after printing the given printf() formatted message. More...
 
void abortv (char const *fmt, va_list args) noexcept
 Aborts the process after printing the given printf() formatted message. More...
 
void assert_handler (char const *file, int line, char const *expr)
 
bool log_supports_ansi_colors () noexcept
 Returns if the system supports ANSI colors. More...
 
void log_colorv (int color, char const *fmt, va_list args)
 Logs a given printf()-like formatted string using an ANSI color code. More...
 
void log_color (int color, char const *fmt,...)
 Logs a given printf()-like formatted string using an ANSI color code. More...
 
void logv (char const *fmt, va_list arg)
 Logs a given printf()-like formatted string. More...
 
void log (char const *fmt,...)
 Logs a given printf()-like formatted string. More...
 
string formatv (char const *fmt, va_list args)
 Format like vsprintf(), but save the result in an zth::string. More...
 
string format (char const *fmt,...)
 Format like sprintf(), but save the result in an zth::string. More...
 
template<typename T >
cow_string str (T value)
 Returns an zth::string representation of the given value. More...
 
template<>
cow_string str< char > (char value)
 
template<>
cow_string str< signed char > (signed char value)
 
template<>
cow_string str< unsigned char > (unsigned char value)
 
template<>
cow_string str< short > (short value)
 
template<>
cow_string str< unsigned short > (unsigned short value)
 
template<>
cow_string str< int > (int value)
 
template<>
cow_string str< unsigned int > (unsigned int value)
 
template<>
cow_string str< long > (long value)
 
template<>
cow_string str< unsigned long > (unsigned long value)
 
template<>
cow_string str< long long > (long long value)
 
template<>
cow_string str< unsigned long long > (unsigned long long value)
 
template<>
cow_string str< float > (float value)
 
template<>
cow_string str< double > (double value)
 
template<>
cow_string str< long double > (long double value)
 
template<>
cow_string str< string && > (string &&value)
 
string err (int e)
 Return a string like strerror() does, but as a zth::string. More...
 
template<>
cow_string str< UniqueIDBase const & > (UniqueIDBase const &value)
 
constexpr char const * version () noexcept
 Returns the version of Zth. More...
 
void 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 waitUntil (F f, TimeInterval const &pollInterval=TimeInterval())
 Wait until the given function f returns true. More...
 
template<typename C >
void waitUntil (C &that, bool(C::*f)(), TimeInterval const &pollInterval=TimeInterval())
 Wait until the given member function f returns true. More...
 
void scheduleTask (TimedWaitable &w)
 
void unscheduleTask (TimedWaitable &w)
 
void wakeup (TimedWaitable &w)
 
void nap (Timestamp const &sleepUntil)
 Sleep until the given time stamp. More...
 
void nap (TimeInterval const &sleepFor)
 Sleep for the given time interval. More...
 
void mnap (long sleepFor_ms)
 Sleep for the given amount of milliseconds. More...
 
void unap (long sleepFor_us)
 Sleep for the given amount of microseconds. More...
 
void sigchld_check ()
 
WorkercurrentWorker () noexcept
 Return the (thread-local) singleton Worker instance. More...
 
void getContext (Worker **worker, Fiber **fiber) noexcept
 
void yield (Fiber *preferFiber=nullptr, bool alwaysYield=false, Timestamp const &now=Timestamp::now())
 Allow a context switch. More...
 
void outOfWork ()
 Force a context switch. More...
 
void suspend ()
 
void resume (Fiber &fiber)
 
int startWorkerThread (void(*f)(), size_t stack, char const *name)
 Start a new thread, create a Worker, with one fiber, which executes f. More...
 
int execlp (char const *file, char const *arg,...)
 Start an external program. More...
 
int execvp (char const *file, char *const arg[])
 Start an external program. More...
 
void context_entry (Context *context)
 
void worker_global_init ()
 

Variables

__thread perf_eventBuffer_typeperf_eventBuffer = nullptr
 
Timestamp const startTime
 

Typedef Documentation

◆ perf_eventBuffer_type

Definition at line 255 of file perf.h.

Function Documentation

◆ assert_handler()

void zth::assert_handler ( char const *  file,
int  line,
char const *  expr 
)

Definition at line 17 of file assert.cpp.

◆ config()

bool zth::config ( int  env,
bool  whenUnset 
)

Checks if a given environment option is set.

Parameters
envone of zth::Env
whenUnsetwhen env does not exist in the environment, return this value

Definition at line 41 of file config.cpp.

◆ context_create()

int zth::context_create ( Context *&  context,
ContextAttr const &  attr 
)
noexcept

Create a context.

Normally, let zth::Fiber manage the context.

Parameters
contextthe variable that will receive the newly created context, when successful
attrthe attributes used to create the context
Returns
0 on success, otherwise an errno
Examples
measure.cpp.

Definition at line 58 of file context.cpp.

◆ context_deinit()

void zth::context_deinit ( )
noexcept

Final cleanup.

Normally, let zth::Worker call this function.

Definition at line 43 of file context.cpp.

◆ context_destroy()

void zth::context_destroy ( Context context)
noexcept

Destroy and cleanup a context.

Normally, let zth::Fiber manage the context.

Examples
measure.cpp.

Definition at line 99 of file context.cpp.

◆ context_entry()

void zth::context_entry ( Context context)

◆ context_init()

int zth::context_init ( )
noexcept

One-time context mechanism initialization.

Normally, let zth::Worker call this function.

Definition at line 32 of file context.cpp.

◆ context_stack_usage()

size_t zth::context_stack_usage ( Context context)
noexcept

Return the high water mark of the stack of the given context.

This does not take any zth::stack_switch() calls into account.

Definition at line 143 of file context.cpp.

◆ context_switch()

void zth::context_switch ( Context from,
Context to 
)
noexcept

Perform context switch.

Normally, let zth::Fiber call this function, after scheduled by the zth::Worker.

Examples
measure.cpp.

Definition at line 116 of file context.cpp.

◆ currentFiberID()

UniqueID< Fiber > const & zth::currentFiberID ( )
inlinenoexcept

Definition at line 407 of file worker.h.

◆ err()

string zth::err ( int  e)
inline

Return a string like strerror() does, but as a zth::string.

Examples
measure.cpp, and zmq.cpp.

Definition at line 617 of file util.h.

◆ format()

string zth::format ( char const *  fmt,
  ... 
)
inline

Format like sprintf(), but save the result in an zth::string.

Examples
5_perf.cpp, measure.cpp, and socks.cpp.

Definition at line 503 of file util.h.

◆ formatv()

string zth::formatv ( char const *  fmt,
va_list  args 
)

Format like vsprintf(), but save the result in an zth::string.

Definition at line 238 of file util.cpp.

◆ getContext()

void zth::getContext ( Worker **  worker,
Fiber **  fiber 
)
inlinenoexcept

Definition at line 412 of file worker.h.

◆ log_supports_ansi_colors()

bool zth::log_supports_ansi_colors ( )
noexcept

Returns if the system supports ANSI colors.

Definition at line 179 of file util.cpp.

◆ operator<<()

template<typename R , typename F >
TypedFiber<R, F>& zth::operator<< ( TypedFiber< R, F > &  f,
FiberManipulator const &  m 
)

Definition at line 796 of file async.h.

◆ perf_deinit()

void zth::perf_deinit ( )

Definition at line 479 of file perf.cpp.

◆ perf_event()

template<typename... Args>
void zth::perf_event ( Args &&...  args)
inlinenoexcept

Definition at line 272 of file perf.h.

◆ perf_flushEventBuffer()

void zth::perf_flushEventBuffer ( )
noexcept

Definition at line 492 of file perf.cpp.

◆ perf_init()

int zth::perf_init ( )

Definition at line 465 of file perf.cpp.

◆ perf_syscall()

void zth::perf_syscall ( char const *  syscall,
Timestamp const &  t = Timestamp() 
)
inline

Put a syscall into the perf output.

Definition at line 359 of file perf.h.

◆ resume()

void zth::resume ( Fiber fiber)
inline

Definition at line 470 of file worker.h.

◆ scheduleTask()

void zth::scheduleTask ( TimedWaitable w)

Definition at line 57 of file waiter.cpp.

◆ sigchld_check()

void zth::sigchld_check ( )

Definition at line 190 of file worker.cpp.

◆ str()

template<typename T >
cow_string zth::str ( value)
inline

Returns an zth::string representation of the given value.

Specialize for your own types.

Examples
socks.cpp.

Definition at line 517 of file util.h.

◆ str< char >()

template<>
cow_string zth::str< char > ( char  value)
inline

Definition at line 523 of file util.h.

◆ str< double >()

template<>
cow_string zth::str< double > ( double  value)
inline

Definition at line 595 of file util.h.

◆ str< float >()

template<>
cow_string zth::str< float > ( float  value)
inline

Definition at line 589 of file util.h.

◆ str< int >()

template<>
cow_string zth::str< int > ( int  value)
inline

Definition at line 553 of file util.h.

◆ str< long >()

template<>
cow_string zth::str< long > ( long  value)
inline

Definition at line 565 of file util.h.

◆ str< long double >()

template<>
cow_string zth::str< long double > ( long double  value)
inline

Definition at line 601 of file util.h.

◆ str< long long >()

template<>
cow_string zth::str< long long > ( long long  value)
inline

Definition at line 577 of file util.h.

◆ str< short >()

template<>
cow_string zth::str< short > ( short  value)
inline

Definition at line 541 of file util.h.

◆ str< signed char >()

template<>
cow_string zth::str< signed char > ( signed char  value)
inline

Definition at line 529 of file util.h.

◆ str< string && >()

template<>
cow_string zth::str< string && > ( string &&  value)
inline

Definition at line 608 of file util.h.

◆ str< TimeInterval const & >()

template<>
cow_string zth::str< TimeInterval const & > ( TimeInterval const &  value)
inline

Definition at line 467 of file time.h.

◆ str< UniqueIDBase const & >()

template<>
cow_string zth::str< UniqueIDBase const & > ( UniqueIDBase const &  value)
inline

Definition at line 647 of file util.h.

◆ str< unsigned char >()

template<>
cow_string zth::str< unsigned char > ( unsigned char  value)
inline

Definition at line 535 of file util.h.

◆ str< unsigned int >()

template<>
cow_string zth::str< unsigned int > ( unsigned int  value)
inline

Definition at line 559 of file util.h.

◆ str< unsigned long >()

template<>
cow_string zth::str< unsigned long > ( unsigned long  value)
inline

Definition at line 571 of file util.h.

◆ str< unsigned long long >()

template<>
cow_string zth::str< unsigned long long > ( unsigned long long  value)
inline

Definition at line 583 of file util.h.

◆ str< unsigned short >()

template<>
cow_string zth::str< unsigned short > ( unsigned short  value)
inline

Definition at line 547 of file util.h.

◆ suspend()

void zth::suspend ( )
inline

Definition at line 462 of file worker.h.

◆ to_std_string()

std::string zth::to_std_string ( string const &  s)
inline

Definition at line 163 of file allocator.h.

◆ to_zth_string()

template<typename Traits , typename Allocator >
string zth::to_zth_string ( std::basic_string< char, Traits, Allocator > const &  s)

Definition at line 158 of file allocator.h.

◆ unscheduleTask()

void zth::unscheduleTask ( TimedWaitable w)

Definition at line 69 of file waiter.cpp.

◆ wakeup()

void zth::wakeup ( TimedWaitable w)

Definition at line 80 of file waiter.cpp.

◆ worker_global_init()

void zth::worker_global_init ( )

Definition at line 34 of file worker.cpp.

Variable Documentation

◆ perf_eventBuffer

__thread perf_eventBuffer_type * zth::perf_eventBuffer = nullptr

Definition at line 43 of file perf.cpp.

◆ startTime

Timestamp const zth::startTime
extern

Definition at line 191 of file time.cpp.