Skip to content

Commit a2c5103

Browse files
committed
ensure task completion happens-before await resumption
It should be the case that non-atomic writes to memory performed by task A are observable by any task B who awaits A's completion.
1 parent 41a232d commit a2c5103

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

stdlib/public/Concurrency/Task.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,11 @@ static void completeTaskImpl(AsyncTask *task,
438438
SwiftError *error) {
439439
assert(task && "completing task, but there is no active task registered");
440440

441+
// This release-fence synchronizes with the acquire-fences performed
442+
// by any tasks that resume after awaiting a task's completion. It ensures
443+
// non-atomic writes done by this task are observable by those tasks.
444+
std::atomic_thread_fence(std::memory_order_release);
445+
441446
// Store the error result.
442447
auto asyncContextPrefix = reinterpret_cast<AsyncContextPrefix *>(
443448
reinterpret_cast<char *>(context) - sizeof(AsyncContextPrefix));
@@ -540,6 +545,8 @@ static void future_adapter(SWIFT_ASYNC_CONTEXT AsyncContext *_context) {
540545

541546
SWIFT_CC(swiftasync)
542547
static void task_wait_throwing_resume_adapter(SWIFT_ASYNC_CONTEXT AsyncContext *_context) {
548+
// This acquire synchronizes with the release in `completeTaskImpl`.
549+
std::atomic_thread_fence(std::memory_order_acquire);
543550

544551
auto context = static_cast<TaskFutureWaitAsyncContext *>(_context);
545552
auto resumeWithError =
@@ -550,6 +557,9 @@ static void task_wait_throwing_resume_adapter(SWIFT_ASYNC_CONTEXT AsyncContext *
550557
SWIFT_CC(swiftasync)
551558
static void
552559
task_future_wait_resume_adapter(SWIFT_ASYNC_CONTEXT AsyncContext *_context) {
560+
// This acquire synchronizes with the release in `completeTaskImpl`.
561+
std::atomic_thread_fence(std::memory_order_acquire);
562+
553563
return _context->ResumeParent(_context->Parent);
554564
}
555565

0 commit comments

Comments
 (0)