Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Microsoft.VisualStudio.Threading/AsyncLazy`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ public Task<T> GetValueAsync(CancellationToken cancellationToken)
// to synchronously block the Main thread waiting for the result
// without leading to deadlocks.
this.joinableTask = this.jobFactory.RunAsync(valueFactory);

// this ensures that this.joinableTask must be committed before this.value
Thread.MemoryBarrier();

this.value = this.joinableTask.Task;
}
else
Expand All @@ -245,6 +249,9 @@ public Task<T> GetValueAsync(CancellationToken cancellationToken)
resumableAwaiter?.Resume();
}

// this ensures that this.joinableTask cannot be retrieved before the conditional check using this.value
Thread.MemoryBarrier();

return this.joinableTask?.JoinAsync(continueOnCapturedContext: false, cancellationToken) ?? this.value.WithCancellation(cancellationToken);
}

Expand Down