-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathDistributedActor.cpp
163 lines (147 loc) · 6.23 KB
/
DistributedActor.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
///===--- DistributedActor.cpp - Distributed actor implementation ----------===///
///
/// This source file is part of the Swift.org open source project
///
/// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
/// Licensed under Apache License v2.0 with Runtime Library Exception
///
/// See https:///swift.org/LICENSE.txt for license information
/// See https:///swift.org/CONTRIBUTORS.txt for the list of Swift project authors
///
///===----------------------------------------------------------------------===///
///
/// The implementation of Swift distributed actors.
///
///===----------------------------------------------------------------------===///
#include "swift/ABI/Task.h"
#include "swift/ABI/Actor.h"
#include "swift/ABI/Metadata.h"
#include "swift/Runtime/AccessibleFunction.h"
#include "swift/Runtime/Concurrency.h"
using namespace swift;
static const AccessibleFunctionRecord *
findDistributedAccessor(const char *targetNameStart, size_t targetNameLength) {
if (auto *func = runtime::swift_findAccessibleFunction(targetNameStart,
targetNameLength)) {
assert(func->Flags.isDistributed());
return func;
}
return nullptr;
}
SWIFT_CC(swift)
SWIFT_EXPORT_FROM(swiftDistributed)
void *swift_distributed_getGenericEnvironment(const char *targetNameStart,
size_t targetNameLength) {
auto *accessor = findDistributedAccessor(targetNameStart, targetNameLength);
return accessor ? accessor->GenericEnvironment.get() : nullptr;
}
/// func _executeDistributedTarget<D: DistributedTargetInvocationDecoder>(
/// on: AnyObject,
/// _ targetName: UnsafePointer<UInt8>,
/// _ targetNameLength: UInt,
/// argumentDecoder: inout D,
/// argumentTypes: UnsafeBufferPointer<Any.Type>,
/// resultBuffer: Builtin.RawPointer,
/// substitutions: UnsafeRawPointer?,
/// witnessTables: UnsafeRawPointer?,
/// numWitnessTables: UInt
/// ) async throws
using TargetExecutorSignature =
AsyncSignature<void(/*on=*/DefaultActor *,
/*targetName=*/const char *, /*targetNameSize=*/size_t,
/*argumentDecoder=*/HeapObject *,
/*argumentTypes=*/const Metadata *const *,
/*resultBuffer=*/void *,
/*substitutions=*/void *,
/*witnessTables=*/void **,
/*numWitnessTables=*/size_t,
/*decoderType=*/Metadata *,
/*decoderWitnessTable=*/void **),
/*throws=*/true>;
SWIFT_CC(swiftasync)
SWIFT_EXPORT_FROM(swiftDistributed)
TargetExecutorSignature::FunctionType swift_distributed_execute_target;
/// Accessor takes:
/// - an async context
/// - an argument decoder as an instance of type conforming to `InvocationDecoder`
/// - a list of all argument types (with substitutions applied)
/// - a result buffer as a raw pointer
/// - a list of substitutions
/// - a list of witness tables
/// - a number of witness tables in the buffer
/// - a reference to an actor to execute method on.
/// - a type of the argument decoder
/// - a witness table associated with argument decoder value
using DistributedAccessorSignature =
AsyncSignature<void(/*argumentDecoder=*/HeapObject *,
/*argumentTypes=*/const Metadata *const *,
/*resultBuffer=*/void *,
/*substitutions=*/void *,
/*witnessTables=*/void **,
/*numWitnessTables=*/size_t,
/*actor=*/HeapObject *,
/*decoderType=*/Metadata *,
/*decoderWitnessTable=*/void **),
/*throws=*/true>;
SWIFT_CC(swiftasync)
static DistributedAccessorSignature::ContinuationType
swift_distributed_execute_target_resume;
SWIFT_CC(swiftasync)
static void swift_distributed_execute_target_resume(
SWIFT_ASYNC_CONTEXT AsyncContext *context,
SWIFT_CONTEXT SwiftError *error) {
auto parentCtx = context->Parent;
auto resumeInParent =
reinterpret_cast<TargetExecutorSignature::ContinuationType *>(
parentCtx->ResumeParent);
swift_task_dealloc(context);
// See `swift_distributed_execute_target` - `parentCtx` in this case
// is `callContext` which should be completely transparent on resume.
return resumeInParent(parentCtx, error);
}
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
SwiftError* swift_distributed_makeDistributedTargetAccessorNotFoundError();
SWIFT_CC(swiftasync)
void swift_distributed_execute_target(
SWIFT_ASYNC_CONTEXT AsyncContext *callerContext,
DefaultActor *actor,
const char *targetNameStart, size_t targetNameLength,
HeapObject *argumentDecoder,
const Metadata *const *argumentTypes,
void *resultBuffer,
void *substitutions,
void **witnessTables,
size_t numWitnessTables,
Metadata *decoderType,
void **decoderWitnessTable) {
auto *accessor = findDistributedAccessor(targetNameStart, targetNameLength);
if (!accessor) {
SwiftError *error =
swift_distributed_makeDistributedTargetAccessorNotFoundError();
auto resumeInParent =
reinterpret_cast<TargetExecutorSignature::ContinuationType *>(
callerContext->ResumeParent);
return resumeInParent(callerContext, error);
}
auto *asyncFnPtr = reinterpret_cast<
const AsyncFunctionPointer<DistributedAccessorSignature> *>(
accessor->Function.get());
assert(asyncFnPtr && "no function pointer for distributed_execute_target");
DistributedAccessorSignature::FunctionType *accessorEntry =
asyncFnPtr->Function.get();
AsyncContext *calleeContext = reinterpret_cast<AsyncContext *>(
swift_task_alloc(asyncFnPtr->ExpectedContextSize));
calleeContext->Parent = callerContext;
calleeContext->ResumeParent = reinterpret_cast<TaskContinuationFunction *>(
swift_distributed_execute_target_resume);
accessorEntry(calleeContext,
argumentDecoder,
argumentTypes,
resultBuffer,
substitutions,
witnessTables,
numWitnessTables,
actor,
decoderType,
decoderWitnessTable);
}