Zth (libzth)
fiber.cpp
Go to the documentation of this file.
1 /*
2  * Zth (libzth), a cooperative userspace multitasking library.
3  * Copyright (C) 2019-2022 Jochem Rutgers
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
8  */
9 
10 #include <libzth/fiber.h>
11 #include <libzth/worker.h>
12 
13 namespace zth {
14 
17 
19 {
20  Worker* w = Worker::instance();
21  if(unlikely(!w))
22  return EAGAIN;
23 
24  Fiber* f = new Fiber(&Runnable::entry_, (void*)this);
25  int res = 0;
26  if(unlikely((res = fiberHook(*f)))) {
27  // Rollback.
28  delete f;
29  return res;
30  }
31 
32  w->add(f);
33  return 0;
34 }
35 
36 } // namespace zth
The fiber.
Definition: fiber.h:52
static FiberHook * hookDead
Hook to be called when a Fiber is destroyed.
Definition: fiber.h:69
void() FiberHook(Fiber &)
Definition: fiber.h:55
static FiberHook * hookNew
Hook to be called when a Fiber is created.
Definition: fiber.h:62
int run()
Definition: fiber.cpp:18
virtual int fiberHook(Fiber &f)
Definition: fiber.h:496
static safe_ptr< singleton_type >::type instance() noexcept
Return the only instance of T within this thread.
Definition: util.h:989
The class that manages the fibers within this thread.
Definition: worker.h:38
void add(Fiber *fiber) noexcept
Definition: worker.h:106
Definition: allocator.h:23
#define unlikely(expr)
Marks the given expression to likely be evaluated to true.
Definition: util.h:56