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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
|
//===--- AsyncLet.h - async let object management -00------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 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
//
//===----------------------------------------------------------------------===//
//
// Object management routines for asynchronous task objects.
//
//===----------------------------------------------------------------------===//
#include "swift/Runtime/Concurrency.h"
#include "../CompatibilityOverride/CompatibilityOverride.h"
#include "Debug.h"
#include "TaskPrivate.h"
#include "swift/ABI/AsyncLet.h"
#include "swift/ABI/Metadata.h"
#include "swift/ABI/Task.h"
#include "swift/ABI/TaskOptions.h"
#include "swift/Runtime/Heap.h"
#include "swift/Runtime/HeapObject.h"
#include "swift/Threading/Mutex.h"
#include "llvm/ADT/PointerIntPair.h"
#if !defined(_WIN32) && !defined(__wasi__) && __has_include(<dlfcn.h>)
#include <dlfcn.h>
#endif
#include <new>
using namespace swift;
namespace {
class alignas(Alignment_AsyncLet) AsyncLetImpl: public ChildTaskStatusRecord {
public:
// This is where we could define a Status or other types important for async-let
private:
// Flags stored in the low bits of the task pointer.
enum {
HasResult = 1 << 0,
DidAllocateFromParentTask = 1 << 1,
};
/// The task that was kicked off to initialize this `async let`,
/// and flags.
llvm::PointerIntPair<AsyncTask *, 2, unsigned> taskAndFlags;
/// Reserved space for a future_wait context frame, used during suspensions
/// on the child task future.
std::aligned_storage<sizeof(TaskFutureWaitAsyncContext),
alignof(TaskFutureWaitAsyncContext)>::type futureWaitContextStorage;
friend class ::swift::AsyncTask;
public:
explicit AsyncLetImpl(AsyncTask* task)
: ChildTaskStatusRecord(task),
taskAndFlags(task, 0) {
assert(task->hasChildFragment() && "async let task must be a child task.");
}
/// Returns the task record representing this async let task.
/// The record is stored in the parent task, and should be removed when the
/// async let goes out of scope.
ChildTaskStatusRecord *getTaskRecord() {
return reinterpret_cast<ChildTaskStatusRecord *>(this);
}
AsyncTask *getTask() const {
return taskAndFlags.getPointer();
}
bool hasResultInBuffer() const {
return taskAndFlags.getInt() & HasResult;
}
void setHasResultInBuffer(bool value = true) {
if (value)
taskAndFlags.setInt(taskAndFlags.getInt() | HasResult);
else
taskAndFlags.setInt(taskAndFlags.getInt() & ~HasResult);
}
bool didAllocateFromParentTask() const {
return taskAndFlags.getInt() & DidAllocateFromParentTask;
}
void setDidAllocateFromParentTask(bool value = true) {
if (value)
taskAndFlags.setInt(taskAndFlags.getInt() | DidAllocateFromParentTask);
else
taskAndFlags.setInt(taskAndFlags.getInt() & ~DidAllocateFromParentTask);
}
// The compiler preallocates a large fixed space for the `async let`, with the
// intent that most of it be used for the child task context. The next two
// methods return the address and size of that space.
/// Return a pointer to the unused space within the async let block.
void *getPreallocatedSpace() {
return (void*)(this + 1);
}
/// Return the size of the unused space within the async let block.
static constexpr size_t getSizeOfPreallocatedSpace() {
return sizeof(AsyncLet) - sizeof(AsyncLetImpl);
}
TaskFutureWaitAsyncContext *getFutureContext() {
return reinterpret_cast<TaskFutureWaitAsyncContext*>(&futureWaitContextStorage);
}
}; // end AsyncLetImpl
} // end anonymous namespace
/******************************************************************************/
/************************* ASYNC LET IMPLEMENTATION ***************************/
/******************************************************************************/
static_assert(sizeof(AsyncLetImpl) <= sizeof(AsyncLet) &&
alignof(AsyncLetImpl) <= alignof(AsyncLet),
"AsyncLetImpl doesn't fit in AsyncLet");
static AsyncLetImpl *asImpl(AsyncLet *alet) {
return reinterpret_cast<AsyncLetImpl*>(alet);
}
static AsyncLetImpl *asImpl(const AsyncLet *alet) {
return reinterpret_cast<AsyncLetImpl*>(
const_cast<AsyncLet*>(alet));
}
void swift::asyncLet_addImpl(AsyncTask *task, AsyncLet *asyncLet,
bool didAllocateInParentTask) {
AsyncLetImpl *impl = ::new (asyncLet) AsyncLetImpl(task);
impl->setDidAllocateFromParentTask(didAllocateInParentTask);
auto record = impl->getTaskRecord();
assert(impl == record && "the async-let IS the task record");
// ok, now that the async let task actually is initialized: attach it to the
// current task
bool addedRecord = addStatusRecordToSelf(record,
[&](ActiveTaskStatus parentStatus, ActiveTaskStatus& newStatus) {
updateNewChildWithParentAndGroupState(task, parentStatus, NULL);
return true;
});
(void)addedRecord;
assert(addedRecord);
}
// =============================================================================
// ==== start ------------------------------------------------------------------
SWIFT_CC(swift)
void swift::swift_asyncLet_start(AsyncLet *alet,
TaskOptionRecord *options,
const Metadata *futureResultType,
void *closureEntryPoint,
HeapObject *closureContext) {
auto flags = TaskCreateFlags();
#if SWIFT_CONCURRENCY_TASK_TO_THREAD_MODEL
// In the task to thread model, we don't want tasks to start running on
// separate threads - they will run in the context of the parent
flags.setEnqueueJob(false);
#else
flags.setEnqueueJob(true);
#endif
AsyncLetTaskOptionRecord asyncLetOptionRecord(alet);
asyncLetOptionRecord.Parent = options;
swift_task_create(
flags.getOpaqueValue(),
&asyncLetOptionRecord,
futureResultType,
closureEntryPoint, closureContext);
}
SWIFT_CC(swift)
void swift::swift_asyncLet_begin(AsyncLet *alet,
TaskOptionRecord *options,
const Metadata *futureResultType,
void *closureEntryPoint,
HeapObject *closureContext,
void *resultBuffer) {
SWIFT_TASK_DEBUG_LOG("creating async let buffer of type %s at %p",
swift_getTypeName(futureResultType, true).data,
resultBuffer);
auto flags = TaskCreateFlags();
#if SWIFT_CONCURRENCY_TASK_TO_THREAD_MODEL
// In the task to thread model, we don't want tasks to start running on
// separate threads - they will run in the context of the parent
flags.setEnqueueJob(false);
#else
flags.setEnqueueJob(true);
#endif
AsyncLetWithBufferTaskOptionRecord asyncLetOptionRecord(alet, resultBuffer);
asyncLetOptionRecord.Parent = options;
swift_task_create(
flags.getOpaqueValue(),
&asyncLetOptionRecord,
futureResultType,
closureEntryPoint, closureContext);
}
// =============================================================================
// ==== wait -------------------------------------------------------------------
SWIFT_CC(swiftasync)
static void swift_asyncLet_waitImpl(
OpaqueValue *result, SWIFT_ASYNC_CONTEXT AsyncContext *callerContext,
AsyncLet *alet, TaskContinuationFunction *resumeFunction,
AsyncContext *callContext) {
auto task = alet->getTask();
swift_task_future_wait(result, callerContext, task, resumeFunction,
callContext);
}
SWIFT_CC(swiftasync)
static void swift_asyncLet_wait_throwingImpl(
OpaqueValue *result, SWIFT_ASYNC_CONTEXT AsyncContext *callerContext,
AsyncLet *alet,
ThrowingTaskFutureWaitContinuationFunction *resumeFunction,
AsyncContext * callContext) {
auto task = alet->getTask();
swift_task_future_wait_throwing(result, callerContext, task, resumeFunction,
callerContext);
}
// =============================================================================
// ==== get -------------------------------------------------------------------
SWIFT_CC(swiftasync)
static void swift_asyncLet_getImpl(SWIFT_ASYNC_CONTEXT AsyncContext *callerContext,
AsyncLet *alet,
void *resultBuffer,
TaskContinuationFunction *resumeFunction,
AsyncContext *callContext) {
// Don't need to do anything if the result buffer is already populated.
if (asImpl(alet)->hasResultInBuffer()) {
return resumeFunction(callerContext);
}
// Mark the async let as having its result populated.
// The only task that can ask this of the async let is the same parent task
// that's currently executing, so we can set it now and tail-call future_wait,
// since by the time we can call back it will be populated.
asImpl(alet)->setHasResultInBuffer();
swift_task_future_wait(reinterpret_cast<OpaqueValue*>(resultBuffer),
callerContext, alet->getTask(),
resumeFunction, callContext);
}
struct AsyncLetContinuationContext: AsyncContext {
AsyncLet *alet;
OpaqueValue *resultBuffer;
};
static_assert(sizeof(AsyncLetContinuationContext) <= sizeof(TaskFutureWaitAsyncContext),
"compiler provides the same amount of context space to each");
SWIFT_CC(swiftasync)
static void _asyncLet_get_throwing_continuation(
SWIFT_ASYNC_CONTEXT AsyncContext *callContext,
SWIFT_CONTEXT void *error) {
auto continuationContext = static_cast<AsyncLetContinuationContext*>(callContext);
auto alet = continuationContext->alet;
// If the future completed successfully, its result is now in the async let
// buffer.
if (!error) {
asImpl(alet)->setHasResultInBuffer();
}
// Continue the caller's execution.
auto throwingResume
= reinterpret_cast<ThrowingTaskFutureWaitContinuationFunction*>(callContext->ResumeParent);
return throwingResume(callContext->Parent, error);
}
SWIFT_CC(swiftasync)
static void swift_asyncLet_get_throwingImpl(
SWIFT_ASYNC_CONTEXT AsyncContext *callerContext,
AsyncLet *alet,
void *resultBuffer,
ThrowingTaskFutureWaitContinuationFunction *resumeFunction,
AsyncContext *callContext) {
// Don't need to do anything if the result buffer is already populated.
if (asImpl(alet)->hasResultInBuffer()) {
return resumeFunction(callerContext, nullptr);
}
auto aletContext = static_cast<AsyncLetContinuationContext*>(callContext);
aletContext->ResumeParent
= reinterpret_cast<TaskContinuationFunction*>(resumeFunction);
aletContext->Parent = callerContext;
aletContext->alet = alet;
auto futureContext = asImpl(alet)->getFutureContext();
// Unlike the non-throwing variant, whether we end up with a result depends
// on the success of the task. If we raise an error, then the result buffer
// will not be populated. Save the async let binding so we can fetch it
// after completion.
return swift_task_future_wait_throwing(
reinterpret_cast<OpaqueValue*>(resultBuffer),
aletContext, alet->getTask(),
_asyncLet_get_throwing_continuation,
futureContext);
}
// =============================================================================
// ==== end --------------------------------------------------------------------
SWIFT_CC(swift)
static void swift_asyncLet_endImpl(AsyncLet *alet) {
auto task = alet->getTask();
// Cancel the task as we exit the scope
swift_task_cancel(task);
// Remove the child record from the parent task
auto record = asImpl(alet)->getTaskRecord();
removeStatusRecordFromSelf(record);
// TODO: we need to implicitly await either before the end or here somehow.
// and finally, release the task and free the async-let
AsyncTask *parent = swift_task_getCurrent();
assert(parent && "async-let must have a parent task");
SWIFT_TASK_DEBUG_LOG("async let end of task %p, parent: %p", task, parent);
_swift_task_dealloc_specific(parent, task);
}
// =============================================================================
// ==== finish -----------------------------------------------------------------
SWIFT_CC(swiftasync)
// FIXME: noinline to work around an LLVM bug where the outliner breaks
// musttail.
SWIFT_NOINLINE
static void asyncLet_finish_after_task_completion(SWIFT_ASYNC_CONTEXT AsyncContext *callerContext,
AsyncLet *alet,
TaskContinuationFunction *resumeFunction,
AsyncContext *callContext,
SWIFT_CONTEXT void *error) {
auto task = alet->getTask();
// Remove the child record from the parent task
auto record = asImpl(alet)->getTaskRecord();
removeStatusRecordFromSelf(record);
// and finally, release the task and destroy the async-let
assert(swift_task_getCurrent() && "async-let must have a parent task");
SWIFT_TASK_DEBUG_LOG("async let end of task %p, parent: %p", task,
swift_task_getCurrent());
// Destruct the task.
task->~AsyncTask();
// Deallocate it out of the parent, if it was allocated there.
if (alet->didAllocateFromParentTask()) {
swift_task_dealloc(task);
}
return reinterpret_cast<ThrowingTaskFutureWaitContinuationFunction*>(resumeFunction)
(callerContext, error);
}
SWIFT_CC(swiftasync)
static void _asyncLet_finish_continuation(
SWIFT_ASYNC_CONTEXT AsyncContext *callContext,
SWIFT_CONTEXT void *error) {
// Retrieve the async let pointer from the context.
auto continuationContext
= reinterpret_cast<AsyncLetContinuationContext*>(callContext);
auto alet = continuationContext->alet;
auto resultBuffer = continuationContext->resultBuffer;
// Destroy the error, or the result that was stored to the buffer.
if (error) {
#if SWIFT_CONCURRENCY_EMBEDDED
swift_unreachable("untyped error used in embedded Swift");
#else
swift_errorRelease((SwiftError*)error);
#endif
} else {
alet->getTask()->futureFragment()->getResultType().vw_destroy(resultBuffer);
}
// Clean up the async let now that the task has finished.
return asyncLet_finish_after_task_completion(callContext->Parent,
alet,
callContext->ResumeParent,
callContext,
nullptr);
}
SWIFT_CC(swiftasync)
static void swift_asyncLet_finishImpl(SWIFT_ASYNC_CONTEXT AsyncContext *callerContext,
AsyncLet *alet,
void *resultBuffer,
TaskContinuationFunction *resumeFunction,
AsyncContext *callContext) {
auto task = alet->getTask();
// If the result buffer is already populated, then we just need to destroy
// the value in it and then clean up the task.
if (asImpl(alet)->hasResultInBuffer()) {
task->futureFragment()->getResultType().vw_destroy(
reinterpret_cast<OpaqueValue*>(resultBuffer));
return asyncLet_finish_after_task_completion(callerContext,
alet,
resumeFunction,
callContext,
nullptr);
}
// Otherwise, cancel the task and let it finish first.
swift_task_cancel(task);
// Save the async let pointer in the context so we can clean it up once the
// future completes.
auto aletContext = static_cast<AsyncLetContinuationContext*>(callContext);
aletContext->Parent = callerContext;
aletContext->ResumeParent = resumeFunction;
aletContext->alet = alet;
aletContext->resultBuffer = reinterpret_cast<OpaqueValue*>(resultBuffer);
auto futureContext = asImpl(alet)->getFutureContext();
// TODO: It would be nice if we could await the future without having to
// provide a buffer to store the value to, since we're going to dispose of
// it anyway.
return swift_task_future_wait_throwing(
reinterpret_cast<OpaqueValue*>(resultBuffer),
callContext, alet->getTask(),
_asyncLet_finish_continuation,
futureContext);
}
// =============================================================================
// ==== consume ----------------------------------------------------------------
SWIFT_CC(swiftasync)
static void _asyncLet_consume_continuation(
SWIFT_ASYNC_CONTEXT AsyncContext *callContext) {
// Retrieve the async let pointer from the context.
auto continuationContext
= reinterpret_cast<AsyncLetContinuationContext*>(callContext);
auto alet = continuationContext->alet;
// Clean up the async let now that the task has finished.
return asyncLet_finish_after_task_completion(callContext->Parent, alet,
callContext->ResumeParent,
callContext,
nullptr);
}
SWIFT_CC(swiftasync)
static void swift_asyncLet_consumeImpl(SWIFT_ASYNC_CONTEXT AsyncContext *callerContext,
AsyncLet *alet,
void *resultBuffer,
TaskContinuationFunction *resumeFunction,
AsyncContext *callContext) {
// If the result buffer is already populated, then we just need to clean up
// the task.
if (asImpl(alet)->hasResultInBuffer()) {
return asyncLet_finish_after_task_completion(callerContext,
alet,
resumeFunction,
callContext,
nullptr);
}
// Save the async let pointer in the context so we can clean it up once the
// future completes.
auto aletContext = static_cast<AsyncLetContinuationContext*>(callContext);
aletContext->Parent = callerContext;
aletContext->ResumeParent = resumeFunction;
aletContext->alet = alet;
auto futureContext = asImpl(alet)->getFutureContext();
// Await completion of the task. We'll destroy the task afterward.
return swift_task_future_wait(
reinterpret_cast<OpaqueValue*>(resultBuffer),
callContext, alet->getTask(),
_asyncLet_consume_continuation,
futureContext);
}
SWIFT_CC(swiftasync)
static void _asyncLet_consume_throwing_continuation(
SWIFT_ASYNC_CONTEXT AsyncContext *callContext,
SWIFT_CONTEXT void *error) {
// Get the async let pointer so we can destroy the task.
auto continuationContext = static_cast<AsyncLetContinuationContext*>(callContext);
auto alet = continuationContext->alet;
return asyncLet_finish_after_task_completion(callContext->Parent,
alet,
callContext->ResumeParent,
callContext,
error);
}
SWIFT_CC(swiftasync)
static void swift_asyncLet_consume_throwingImpl(
SWIFT_ASYNC_CONTEXT AsyncContext *callerContext,
AsyncLet *alet,
void *resultBuffer,
ThrowingTaskFutureWaitContinuationFunction *resumeFunction,
AsyncContext *callContext) {
// If the result buffer is already populated, we just need to clean up the
// task.
if (asImpl(alet)->hasResultInBuffer()) {
return asyncLet_finish_after_task_completion(callerContext,
alet,
reinterpret_cast<TaskContinuationFunction*>(resumeFunction),
callContext,
nullptr);
}
auto aletContext = static_cast<AsyncLetContinuationContext*>(callContext);
aletContext->ResumeParent
= reinterpret_cast<TaskContinuationFunction*>(resumeFunction);
aletContext->Parent = callerContext;
aletContext->alet = alet;
auto futureContext = asImpl(alet)->getFutureContext();
// Unlike the non-throwing variant, whether we end up with a result depends
// on the success of the task. If we raise an error, then the result buffer
// will not be populated. Save the async let binding so we can fetch it
// after completion.
return swift_task_future_wait_throwing(
reinterpret_cast<OpaqueValue*>(resultBuffer),
aletContext, alet->getTask(),
_asyncLet_consume_throwing_continuation,
futureContext);
}
// =============================================================================
// ==== AsyncLet Implementation ------------------------------------------------
AsyncTask* AsyncLet::getTask() const {
return asImpl(this)->getTask();
}
void *AsyncLet::getPreallocatedSpace() {
return asImpl(this)->getPreallocatedSpace();
}
size_t AsyncLet::getSizeOfPreallocatedSpace() {
return AsyncLetImpl::getSizeOfPreallocatedSpace();
}
bool AsyncLet::didAllocateFromParentTask() {
return asImpl(this)->didAllocateFromParentTask();
}
void AsyncLet::setDidAllocateFromParentTask(bool value) {
return asImpl(this)->setDidAllocateFromParentTask(value);
}
// =============================================================================
#define OVERRIDE_ASYNC_LET COMPATIBILITY_OVERRIDE
#include COMPATIBILITY_OVERRIDE_INCLUDE_PATH
|