forked from swiftwasm/JavaScriptKit
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutor.cpp
More file actions
90 lines (75 loc) · 3.05 KB
/
Executor.cpp
File metadata and controls
90 lines (75 loc) · 3.05 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "Queue.h"
#include "AsyncCall.h"
#include <swift/ABI/Executor.h>
#include <swift/Runtime/Concurrency.h>
#include <iostream>
using namespace swift;
static Queue GlobalQueue;
SWIFT_CC(swift)
static void enqueueGlobal(Job *job) {
GlobalQueue.insertJob(job);
}
extern "C" void installTaskEnqueueHook(void) {
swift_task_enqueueGlobal_hook = enqueueGlobal;
}
struct ThickAsyncFunctionContext: HeapObject {
uint32_t ExpectedContextSize;
};
using RunAndBlockSignature =
AsyncSignature<void(HeapObject*), /*throws*/ false>;
struct RunAndBlockContext: AsyncContext {
const void *Function;
HeapObject *FunctionContext;
};
using RunAndBlockCalleeContext =
AsyncCalleeContext<RunAndBlockContext, RunAndBlockSignature>;
/// Second half of the runAndBlock async function.
SWIFT_CC(swiftasync)
static void runAndBlock_finish(AsyncTask *task, ExecutorRef executor,
AsyncContext *_context) {
auto calleeContext = static_cast<RunAndBlockCalleeContext*>(_context);
auto context = popAsyncContext(task, calleeContext);
return context->ResumeParent(task, executor, context);
}
/// First half of the runAndBlock async function.
SWIFT_CC(swiftasync)
static void runAndBlock_start(AsyncTask *task, ExecutorRef executor,
AsyncContext *_context) {
auto callerContext = static_cast<RunAndBlockContext*>(_context);
size_t calleeContextSize;
RunAndBlockSignature::FunctionType *function;
// If the function context is non-null, then the function pointer is
// an ordinary function pointer.
auto functionContext = callerContext->FunctionContext;
if (functionContext) {
function = reinterpret_cast<RunAndBlockSignature::FunctionType*>(
const_cast<void*>(callerContext->Function));
calleeContextSize =
static_cast<ThickAsyncFunctionContext*>(functionContext)
->ExpectedContextSize;
// Otherwise, the function pointer is an async function pointer.
} else {
auto fnPtr = reinterpret_cast<const RunAndBlockSignature::FunctionPointer*>(
callerContext->Function);
function = fnPtr->Function;
calleeContextSize = fnPtr->ExpectedContextSize;
}
auto calleeContext =
pushAsyncContext<RunAndBlockSignature>(task, executor, callerContext,
calleeContextSize,
&runAndBlock_finish,
functionContext);
return function(task, executor, calleeContext);
}
SWIFT_CC(swift)
extern "C" void swift_run_async(const void *function, HeapObject *functionContext) {
auto pair = swift_task_create_f(JobFlags(JobKind::Task,
JobPriority::Default),
/*parent*/ nullptr,
&runAndBlock_start,
sizeof(RunAndBlockContext));
auto context = static_cast<RunAndBlockContext*>(pair.InitialContext);
context->Function = function;
context->FunctionContext = functionContext;
swift_task_enqueueGlobal(pair.Task);
}