-
Notifications
You must be signed in to change notification settings - Fork 31
/
fiber.hpp
65 lines (51 loc) · 1.4 KB
/
fiber.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#pragma once
#ifndef WAYWARD_SUPPORT_FIBER_HPP_INCLUDED
#define WAYWARD_SUPPORT_FIBER_HPP_INCLUDED
#include <memory>
#include <functional>
#include <tuple>
#include <wayward/support/error.hpp>
#include <wayward/support/datetime.hpp>
namespace wayward {
struct FiberError : Error {
FiberError(const std::string& msg) : Error(msg) {}
};
struct FiberTermination {};
struct Fiber;
/*
Fiber is implemented in terms of std::shared_ptr. A running fiber has a
reference to the fiber that invoked it.
*/
using FiberPtr = std::shared_ptr<Fiber>;
namespace fiber {
using Function = std::function<void()>;
using ErrorHandler = std::function<void(std::exception_ptr)>;
FiberPtr current();
/*
Create a new fiber without starting it.
*/
FiberPtr create(Function);
/*
Create a new fiber with an error handler, without starting it.
*/
FiberPtr create(Function, ErrorHandler);
/*
Resume a fiber (or start it if it hasn't been started yet).
*/
void resume(FiberPtr);
/*
Create a fiber and start it right away.
*/
void start(Function);
/*
Resume the fiber with a signal to terminate.
*/
void terminate(FiberPtr);
/*
Resume the fiber that started the current fiber.
Throws an exception if the current fiber is orphaned.
*/
void yield();
}
}
#endif // WAYWARD_SUPPORT_FIBER_HPP_INCLUDED