1 // Copyright 2019 The Marl Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Minimal assembly implementations of fiber context switching for Unix-based
16 // platforms.
17 //
18 // Note: Unlike makecontext, swapcontext or the Windows fiber APIs, these
19 // assembly implementations *do not* save or restore signal masks,
20 // floating-point control or status registers, FS and GS segment registers,
21 // thread-local storage state nor any SIMD registers. This should not be a
22 // problem as the marl scheduler requires fibers to be executed on the same
23 // thread throughout their lifetime.
24
25 #if defined(__x86_64__)
26 #include "osfiber_asm_x64.h"
27 #elif defined(__i386__)
28 #include "osfiber_asm_x86.h"
29 #elif defined(__aarch64__)
30 #include "osfiber_asm_aarch64.h"
31 #elif defined(__arm__)
32 #include "osfiber_asm_arm.h"
33 #elif defined(__powerpc64__)
34 #include "osfiber_asm_ppc64.h"
35 #elif defined(__mips__) && _MIPS_SIM == _ABI64
36 #include "osfiber_asm_mips64.h"
37 #else
38 #error "Unsupported target"
39 #endif
40
41 #include "marl/memory.h"
42
43 #include <functional>
44 #include <memory>
45
46 extern "C" {
47
48 extern void marl_fiber_set_target(marl_fiber_context*,
49 void* stack,
50 uint32_t stack_size,
51 void (*target)(void*),
52 void* arg);
53 extern void marl_fiber_swap(marl_fiber_context* from,
54 const marl_fiber_context* to);
55
56 } // extern "C"
57
58 namespace marl {
59
60 class OSFiber {
61 public:
62 inline OSFiber(Allocator*);
63 inline ~OSFiber();
64
65 // createFiberFromCurrentThread() returns a fiber created from the current
66 // thread.
67 static inline Allocator::unique_ptr<OSFiber> createFiberFromCurrentThread(
68 Allocator* allocator);
69
70 // createFiber() returns a new fiber with the given stack size that will
71 // call func when switched to. func() must end by switching back to another
72 // fiber, and must not return.
73 static inline Allocator::unique_ptr<OSFiber> createFiber(
74 Allocator* allocator,
75 size_t stackSize,
76 const std::function<void()>& func);
77
78 // switchTo() immediately switches execution to the given fiber.
79 // switchTo() must be called on the currently executing fiber.
80 inline void switchTo(OSFiber*);
81
82 private:
83 static inline void run(OSFiber* self);
84
85 Allocator* allocator;
86 marl_fiber_context context;
87 std::function<void()> target;
88 Allocation stack;
89 };
90
OSFiber(Allocator * allocator)91 OSFiber::OSFiber(Allocator* allocator) : allocator(allocator) {}
92
~OSFiber()93 OSFiber::~OSFiber() {
94 if (stack.ptr != nullptr) {
95 allocator->free(stack);
96 }
97 }
98
createFiberFromCurrentThread(Allocator * allocator)99 Allocator::unique_ptr<OSFiber> OSFiber::createFiberFromCurrentThread(
100 Allocator* allocator) {
101 auto out = allocator->make_unique<OSFiber>(allocator);
102 out->context = {};
103 return out;
104 }
105
createFiber(Allocator * allocator,size_t stackSize,const std::function<void ()> & func)106 Allocator::unique_ptr<OSFiber> OSFiber::createFiber(
107 Allocator* allocator,
108 size_t stackSize,
109 const std::function<void()>& func) {
110 Allocation::Request request;
111 request.size = stackSize;
112 request.alignment = 16;
113 request.usage = Allocation::Usage::Stack;
114 #if MARL_USE_FIBER_STACK_GUARDS
115 request.useGuards = true;
116 #endif
117
118 auto out = allocator->make_unique<OSFiber>(allocator);
119 out->context = {};
120 out->target = func;
121 out->stack = allocator->allocate(request);
122 marl_fiber_set_target(&out->context, out->stack.ptr, stackSize,
123 reinterpret_cast<void (*)(void*)>(&OSFiber::run),
124 out.get());
125 return out;
126 }
127
run(OSFiber * self)128 void OSFiber::run(OSFiber* self) {
129 self->target();
130 }
131
switchTo(OSFiber * fiber)132 void OSFiber::switchTo(OSFiber* fiber) {
133 marl_fiber_swap(&context, &fiber->context);
134 }
135
136 } // namespace marl
137