44# if __cplusplus >= 201103L
94 , m_stackSize(
Config::DefaultFiberStackSize)
97 , m_timeslice(
Config::MinTimeslice())
98 , m_dtMax(
Config::CheckTimesliceOverrun
111 m_cleanup.
once(*
this);
122 zth_dbg(
fiber,
"[%s] Destructed. Stack usage: 0x%x of 0x%x, total CPU: %s",
123 id_str(), (
unsigned int)stackUsage_, (
unsigned int)stackSize_,
124 m_totalTime.
str().c_str());
168 void*
fls() const noexcept
173 void setFls(
void* data =
nullptr) noexcept
202 m_cleanup.
add(f, arg);
250 from.m_totalTime += dt;
270 m_stateEnd =
now + m_timeslice;
337# ifdef __cpp_exceptions
356 if(sleepUntil.isNull())
376 m_stateEnd = sleepUntil;
447 res +=
" Uninitialized";
477 res +=
format(
" t=%s", m_totalTime.
str().c_str());
482 virtual void changedName(
string const&
name)
override
518 }
catch(std::exception
const& e) {
519# ifdef __cpp_exceptions
615 static void entry_(
void* that)
631ZTH_EXPORT inline
void* fls() noexcept
643ZTH_EXPORT
inline void setFls(
void* data =
nullptr) noexcept
677EXTERN_C ZTH_EXPORT
int main_fiber(
int argc,
char** argv);
State state() const noexcept
Context * context() const noexcept
Timestamp const & stateEnd() const noexcept
void setState(State state, Timestamp const &t=Timestamp::now()) noexcept
static FiberHook * hookDead
Hook to be called when a Fiber is destroyed.
static Fiber * fromHandle(zth_fiber_t const &h) noexcept
virtual ~Fiber() noexcept override
void atCleanup(Hook_type::function_type f, Hook_type::arg_type arg=Hook_type::arg_type())
void() FiberHook(Fiber &)
void setFls(void *data=nullptr) noexcept
size_t stackSize() const noexcept
int init(Timestamp const &now=Timestamp::now())
bool allowYield(Timestamp const &now=Timestamp::now()) const noexcept
Hook< Fiber & > Hook_type
void atExit(Hook_type::function_type f, Hook_type::arg_type arg=Hook_type::arg_type())
static uintptr_t const ExitUnknown
TimeInterval const & totalTime() const noexcept
void fiberEntry_() noexcept
zth_fiber_t handle() const noexcept
int setStackSize(size_t size) noexcept
Timestamp const & runningSince() const noexcept
size_t stackUsage() const
Fiber(Entry entry, EntryArg arg=EntryArg())
static FiberHook * hookNew
Hook to be called when a Fiber is created.
int run(Fiber &from, Timestamp now=Timestamp::now())
void nap(Timestamp const &sleepUntil=Timestamp::null()) noexcept
ContextAttr::EntryArg EntryArg
static void fiberEntry(void *that) noexcept
void * fls() const noexcept
void add(function_type f, arg_type a=arg_type()) noexcept
hookable_type::function_type function_type
hookable_type::arg_type arg_type
void once(type x) noexcept
virtual char const * id_str() const noexcept override
string const & name() const noexcept
An abstract class, that can be started as a fiber.
Fiber * fiber() const noexcept
virtual int fiberHook(Fiber &f)
char const * id_str() const
virtual void cleanup() noexcept
virtual ~Runnable()=default
constexpr Runnable() noexcept
Convenient wrapper around struct timespec that contains a time interval.
Convenient wrapper around struct timespec that contains an absolute timestamp.
constexpr bool isNull() const noexcept
static constexpr Timestamp null() noexcept
Keeps track of a process-wide unique ID within the type T.
void const * normptr() const noexcept
int main_fiber(int argc, char **argv)
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.
#define zth_perf_mark(marker)
Put a string marker into the perf output.
#define zth_config(name)
Checks if the given zth::Config field is enabled.
void outOfWork()
Force a context switch.
void * fls() noexcept
Return the fiber-local storage, as set by setFls().
void setFls(void *data=nullptr) noexcept
Set the fiber-local storage.
fiber_type< F >::fiber fiber(F &&f, Args &&... args)
Create and start a new fiber.
Fiber & currentFiber() noexcept
Return the currently executing fiber.
#define zth_dbg(group, fmt, a...)
Debug printf()-like function.
void log_color(int color, char const *fmt,...)
Logs a given printf()-like formatted string using an ANSI color code.
impl::PickBacktrace ::type Backtrace
Save a backtrace.
#define ZTH_CLASS_NEW_DELETE(T)
Define new/delete operators for a class, which are allocator-aware.
void zth_init()
Perform one-time global initialization of the Zth library.
void perf_fiber_state(Fiber &f, int state=-1, Timestamp const &t=Timestamp()) noexcept
Record the current fiber state.
void now(struct timespec &ts)
Returns the current timestamp.
void context_switch(Context *from, Context *to) noexcept
Perform context switch.
void context_destroy(Context *context) noexcept
Destroy and cleanup a context.
int context_create(Context *&context, ContextAttr const &attr) noexcept
Create a context.
string format(char const *fmt,...)
Format like sprintf(), but save the result in an zth::string.
void perf_fiber(Fiber &f) noexcept
Write fiber ID/name to the perf buffer.
size_t context_stack_usage(Context *context) noexcept
Return the high water mark of the stack of the given context.
The configuration of Zth.
static int const Print_perf
static bool const EnablePerfEvent
Enable (but not necessarily record) perf.
Exception thrown when a fiber is cancelled.
Opaque fiber handle type.
#define ZTH_DBG_PREFIX
Prefix for every zth_dbg() call.
#define zth_assert(expr)
assert(), but better integrated in Zth.
#define likely(expr)
Marks the given expression to likely be evaluated to true.
#define ZTH_CLASS_NOCOPY(Class)
#define unlikely(expr)
Marks the given expression to likely be evaluated to true.