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 if(fiber())
24 return EALREADY;
25
26 Fiber* f = new Fiber(&Runnable::entry_, static_cast<void*>(this));
27
28 try {
29 int res = fiberHook(*f);
30 if(unlikely(res)) {
31 // Rollback.
32 m_fiber = nullptr;
33 delete f;
34 return res;
35 }
36 } catch(...) {
37 m_fiber = nullptr;
38 delete f;
39 zth_throw();
40 }
41
42 f->used();
43 w->add(f);
44 return 0;
45}
46
47} // namespace zth
The fiber.
Definition fiber.h:49
void used() noexcept
Definition util.h:954
Fiber * fiber() const noexcept
Definition fiber.h:504
virtual int fiberHook(Fiber &f)
Definition fiber.h:524
static safe_ptr< singleton_type >::type instance() noexcept
Return the only instance of T within this thread.
Definition util.h:1181
The class that manages the fibers within this thread.
Definition worker.h:35
void add(Fiber *fiber, bool front=false) noexcept
Definition worker.h:104
#define zth_throw(...)
Definition macros.h:365
#define unlikely(expr)
Marks the given expression to likely be evaluated to true.
Definition util.h:60