|
| 1 | +///===--- DistributedActor.cpp - Distributed actor implementation ----------===/// |
| 2 | +/// |
| 3 | +/// This source file is part of the Swift.org open source project |
| 4 | +/// |
| 5 | +/// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors |
| 6 | +/// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +/// |
| 8 | +/// See https:///swift.org/LICENSE.txt for license information |
| 9 | +/// See https:///swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +/// |
| 11 | +///===----------------------------------------------------------------------===/// |
| 12 | +/// |
| 13 | +/// The implementation of Swift distributed actors. |
| 14 | +/// |
| 15 | +///===----------------------------------------------------------------------===/// |
| 16 | + |
| 17 | +#include "swift/ABI/Task.h" |
| 18 | +#include "swift/ABI/Actor.h" |
| 19 | +#include "swift/ABI/Metadata.h" |
| 20 | +#include "swift/Runtime/AccessibleFunction.h" |
| 21 | +#include "swift/Runtime/Concurrency.h" |
| 22 | + |
| 23 | +using namespace swift; |
| 24 | + |
| 25 | +static const AccessibleFunctionRecord * |
| 26 | +findDistributedAccessor(const char *targetNameStart, size_t targetNameLength) { |
| 27 | + if (auto *func = runtime::swift_findAccessibleFunction(targetNameStart, |
| 28 | + targetNameLength)) { |
| 29 | + assert(func->Flags.isDistributed()); |
| 30 | + return func; |
| 31 | + } |
| 32 | + return nullptr; |
| 33 | +} |
| 34 | + |
| 35 | +SWIFT_CC(swift) |
| 36 | +SWIFT_EXPORT_FROM(swift_Distributed) |
| 37 | +void *swift_distributed_getGenericEnvironment(const char *targetNameStart, |
| 38 | + size_t targetNameLength) { |
| 39 | + auto *accessor = findDistributedAccessor(targetNameStart, targetNameLength); |
| 40 | + return accessor ? accessor->GenericEnvironment.get() : nullptr; |
| 41 | +} |
| 42 | + |
| 43 | +/// func _executeDistributedTarget( |
| 44 | +/// on: AnyObject, |
| 45 | +/// _ targetName: UnsafePointer<UInt8>, |
| 46 | +/// _ targetNameLength: UInt, |
| 47 | +/// argumentBuffer: Builtin.RawPointer, |
| 48 | +/// argumentTypes: UnsafeBufferPointer<Any.Type>, |
| 49 | +/// resultBuffer: Builtin.RawPointer, |
| 50 | +/// substitutions: UnsafeRawPointer?, |
| 51 | +/// witnessTables: UnsafeRawPointer?, |
| 52 | +/// numWitnessTables: UInt |
| 53 | +/// ) async throws |
| 54 | +using TargetExecutorSignature = |
| 55 | + AsyncSignature<void(/*on=*/DefaultActor *, |
| 56 | + /*targetName=*/const char *, /*targetNameSize=*/size_t, |
| 57 | + /*argumentBuffer=*/void *, |
| 58 | + /*argumentTypes=*/const Metadata *const *, |
| 59 | + /*resultBuffer=*/void *, |
| 60 | + /*substitutions=*/void *, |
| 61 | + /*witnessTables=*/void **, |
| 62 | + /*numWitnessTables=*/size_t, |
| 63 | + /*resumeFunc=*/TaskContinuationFunction *, |
| 64 | + /*callContext=*/AsyncContext *), |
| 65 | + /*throws=*/true>; |
| 66 | + |
| 67 | +SWIFT_CC(swiftasync) |
| 68 | +SWIFT_EXPORT_FROM(swift_Distributed) |
| 69 | +TargetExecutorSignature::FunctionType swift_distributed_execute_target; |
| 70 | + |
| 71 | +/// Accessor takes: |
| 72 | +/// - an async context |
| 73 | +/// - an argument buffer as a raw pointer |
| 74 | +/// - a list of all argument types (with substitutions applied) |
| 75 | +/// - a result buffer as a raw pointer |
| 76 | +/// - a list of substitutions |
| 77 | +/// - a list of witness tables |
| 78 | +/// - a number of witness tables in the buffer |
| 79 | +/// - a reference to an actor to execute method on. |
| 80 | +using DistributedAccessorSignature = |
| 81 | + AsyncSignature<void(/*argumentBuffer=*/void *, |
| 82 | + /*argumentTypes=*/const Metadata *const *, |
| 83 | + /*resultBuffer=*/void *, |
| 84 | + /*substitutions=*/void *, |
| 85 | + /*witnessTables=*/void **, |
| 86 | + /*numWitnessTables=*/size_t, |
| 87 | + /*actor=*/HeapObject *), |
| 88 | + /*throws=*/true>; |
| 89 | + |
| 90 | +SWIFT_CC(swiftasync) |
| 91 | +static DistributedAccessorSignature::ContinuationType |
| 92 | + swift_distributed_execute_target_resume; |
| 93 | + |
| 94 | +SWIFT_CC(swiftasync) |
| 95 | +static void ::swift_distributed_execute_target_resume( |
| 96 | + SWIFT_ASYNC_CONTEXT AsyncContext *context, |
| 97 | + SWIFT_CONTEXT SwiftError *error) { |
| 98 | + auto parentCtx = context->Parent; |
| 99 | + auto resumeInParent = |
| 100 | + reinterpret_cast<TargetExecutorSignature::ContinuationType *>( |
| 101 | + parentCtx->ResumeParent); |
| 102 | + swift_task_dealloc(context); |
| 103 | + // See `swift_distributed_execute_target` - `parentCtx` in this case |
| 104 | + // is `callContext` which should be completely transparent on resume. |
| 105 | + return resumeInParent(parentCtx->Parent, error); |
| 106 | +} |
| 107 | + |
| 108 | +SWIFT_CC(swiftasync) |
| 109 | +void ::swift_distributed_execute_target( |
| 110 | + SWIFT_ASYNC_CONTEXT AsyncContext *callerContext, |
| 111 | + DefaultActor *actor, |
| 112 | + const char *targetNameStart, size_t targetNameLength, |
| 113 | + void *argumentBuffer, |
| 114 | + const Metadata *const *argumentTypes, |
| 115 | + void *resultBuffer, |
| 116 | + void *substitutions, |
| 117 | + void **witnessTables, |
| 118 | + size_t numWitnessTables, |
| 119 | + TaskContinuationFunction *resumeFunc, |
| 120 | + AsyncContext *callContext) { |
| 121 | + auto *accessor = findDistributedAccessor(targetNameStart, targetNameLength); |
| 122 | + if (!accessor) { |
| 123 | + assert(false && "no distributed accessor"); |
| 124 | + return; // FIXME(distributed): return -1 here so the lib can fail the call |
| 125 | + } |
| 126 | + |
| 127 | + auto *asyncFnPtr = reinterpret_cast< |
| 128 | + const AsyncFunctionPointer<DistributedAccessorSignature> *>( |
| 129 | + accessor->Function.get()); |
| 130 | + assert(asyncFnPtr && "no function pointer for distributed_execute_target"); |
| 131 | + |
| 132 | + DistributedAccessorSignature::FunctionType *accessorEntry = |
| 133 | + asyncFnPtr->Function.get(); |
| 134 | + |
| 135 | + AsyncContext *calleeContext = reinterpret_cast<AsyncContext *>( |
| 136 | + swift_task_alloc(asyncFnPtr->ExpectedContextSize)); |
| 137 | + |
| 138 | + // TODO(concurrency): Special functions like this one are currently set-up |
| 139 | + // to pass "caller" context and resume function as extra parameters due to |
| 140 | + // how they are declared in C. But this particular function behaves exactly |
| 141 | + // like a regular `async throws`, which means that we need to initialize |
| 142 | + // intermediate `callContext` using parent `callerContext`. A better fix for |
| 143 | + // this situation would be to adjust IRGen and handle function like this |
| 144 | + // like regular `async` functions even though they are classified as special. |
| 145 | + callContext->Parent = callerContext; |
| 146 | + callContext->ResumeParent = resumeFunc; |
| 147 | + |
| 148 | + calleeContext->Parent = callContext; |
| 149 | + calleeContext->ResumeParent = reinterpret_cast<TaskContinuationFunction *>( |
| 150 | + swift_distributed_execute_target_resume); |
| 151 | + |
| 152 | + accessorEntry(calleeContext, |
| 153 | + argumentBuffer, argumentTypes, |
| 154 | + resultBuffer, |
| 155 | + substitutions, |
| 156 | + witnessTables, |
| 157 | + numWitnessTables, |
| 158 | + actor); |
| 159 | +} |
0 commit comments