Zth (libzth)
Loading...
Searching...
No Matches
fiber.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2019-2026 Jochem Rutgers
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 */
6
7#include <libzth/fiber.h>
8
9#include <libzth/worker.h>
10
11namespace zth {
12
13// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
14Fiber::FiberHook* Fiber::hookNew = nullptr;
15// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
16Fiber::FiberHook* Fiber::hookDead = nullptr;
17
18int Runnable::run()
19{
21 if(unlikely(!w))
22 return EAGAIN;
23
24 Fiber* f = new Fiber(&Runnable::entry_, static_cast<void*>(this));
25 try {
26 int res = fiberHook(*f);
27 if(unlikely(res)) {
28 // Rollback.
29 delete f;
30 return res;
31 }
32 } catch(...) {
33 delete f;
34 zth_throw();
35 }
36
37 w->add(f);
38 return 0;
39}
40
41} // namespace zth
The fiber.
Definition fiber.h:49
virtual int fiberHook(Fiber &f)
Definition fiber.h:510
static safe_ptr< singleton_type >::type instance() noexcept
Return the only instance of T within this thread.
Definition util.h:1047
The class that manages the fibers within this thread.
Definition worker.h:35
void add(Fiber *fiber) noexcept
Definition worker.h:104
#define zth_throw(...)
Definition macros.h:229
#define unlikely(expr)
Marks the given expression to likely be evaluated to true.
Definition util.h:55