35 # if __cplusplus >= 201103L
36 # include <functional>
86 , m_timeslice(
Config::MinTimeslice_s())
87 , m_dtMax(
Config::CheckTimesliceOverrun
89 *
Config::TimesliceOverrunFactorReportThreshold
100 for(decltype(m_cleanup.begin()) it = m_cleanup.begin(); it != m_cleanup.end(); ++it)
101 it->first(*
this, it->second);
110 size_t stackSize_ __attribute__((unused)) = this->
stackSize();
111 size_t stackUsage_ __attribute__((unused)) = this->
stackUsage();
114 zth_dbg(
fiber,
"[%s] Destructed. Stack usage: 0x%x of 0x%x, total CPU: %s",
115 id_str(), (
unsigned int)stackUsage_, (
unsigned int)stackSize_,
116 m_totalTime.
str().c_str());
148 void*
fls() const noexcept
153 void setFls(
void* data =
nullptr) noexcept
175 m_cleanup.push_back(std::make_pair(f, arg));
208 if((res =
init(now)))
220 from.m_totalTime += dt;
239 m_stateEnd = now + m_timeslice;
290 if(sleepUntil.isNull())
307 m_stateEnd = sleepUntil;
367 res +=
" Uninitialized";
393 res +=
format(
" t=%s", m_totalTime.
str().c_str());
429 }
catch(std::exception
const& e) {
430 #ifdef __cpp_exceptions
520 static void entry_(
void* that)
536 ZTH_EXPORT inline
void*
fls() noexcept
548 ZTH_EXPORT
inline void setFls(
void* data =
nullptr) noexcept
582 EXTERN_C ZTH_EXPORT
int main_fiber(
int argc,
char** argv);
void print(int color=-1) const
State state() const noexcept
void setState(State state, Timestamp const &t=Timestamp::now()) noexcept
static FiberHook * hookDead
Hook to be called when a Fiber is destroyed.
void() FiberHook(Fiber &)
void setFls(void *data=nullptr) noexcept
void * fls() const noexcept
size_t stackSize() const noexcept
int init(Timestamp const &now=Timestamp::now())
bool allowYield(Timestamp const &now=Timestamp::now()) const noexcept
virtual void changedName(string const &name) override
static uintptr_t const ExitUnknown
TimeInterval const & totalTime() const noexcept
Context * context() const noexcept
void fiberEntry_() noexcept
int setStackSize(size_t size) noexcept
Timestamp const & stateEnd() const noexcept
virtual ~Fiber() override
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
void addCleanup(void(*f)(Fiber &, void *), void *arg)
Timestamp const & runningSince() const noexcept
static void fiberEntry(void *that) noexcept
An abstract class, that can be started as a fiber.
virtual int fiberHook(Fiber &f)
char const * id_str() const
Fiber * fiber() const 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.
virtual char const * id_str() const override
string const & name() const noexcept
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_config(name)
Checks if the given zth::Config field is enabled.
void setFls(void *data=nullptr) noexcept
Set the fiber-local storage.
Fiber & currentFiber() noexcept
Return the currently executing fiber.
fiber_type< F >::factory fiber(F f, char const *name=nullptr)
Create a new fiber.
void * fls() noexcept
Return the fiber-local storage, as set by setFls().
constexpr auto entry
Guard that is only enabled upon entry of a state.
void perf_mark(char const *marker)
Put a string marker into the perf output.
#define zth_perf_event(...)
Construct a zth::PerfEvent with provided parameters, and forward it to the perf buffer for later proc...
#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.
#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 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.
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
std::list type using Config::Allocator::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.