1 // Copyright 2023 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 #if defined(__EMSCRIPTEN__)
16
17 #include "osfiber_emscripten.h"
18
19 #include "marl/export.h"
20
21
22 extern "C" {
23
24 MARL_EXPORT
marl_fiber_trampoline(void (* target)(void *),void * arg)25 void marl_fiber_trampoline(void (*target)(void*), void* arg) {
26 target(arg);
27 }
28
29 MARL_EXPORT
marl_main_fiber_init(marl_fiber_context * ctx)30 void marl_main_fiber_init(marl_fiber_context* ctx) {
31 emscripten_fiber_init_from_current_context(
32 &ctx->context,
33 ctx->asyncify_stack.data(),
34 ctx->asyncify_stack.size());
35 }
36
37 MARL_EXPORT
marl_fiber_set_target(marl_fiber_context * ctx,void * stack,uint32_t stack_size,void (* target)(void *),void * arg)38 void marl_fiber_set_target(marl_fiber_context* ctx,
39 void* stack,
40 uint32_t stack_size,
41 void (*target)(void*),
42 void* arg) {
43
44 emscripten_fiber_init(
45 &ctx->context,
46 target,
47 arg,
48 stack,
49 stack_size,
50 ctx->asyncify_stack.data(),
51 ctx->asyncify_stack.size());
52 }
53
54 MARL_EXPORT
marl_fiber_swap(marl_fiber_context * from,const marl_fiber_context * to)55 extern void marl_fiber_swap(marl_fiber_context* from,
56 const marl_fiber_context* to) {
57 emscripten_fiber_swap(&from->context, const_cast<emscripten_fiber_t*>(&to->context));
58 }
59 }
60 #endif // defined(__EMSCRIPTEN__)
61