Skip to content

Commit 7d17e8b

Browse files
author
Lifeng Lu
committed
For customized SynchronizationContext implementation, knowing whether a pending request
has been completed could allow it to remove completed requests. Because JTF requests are potentially sent to multiple queues, including the JTF internal queue, many of those requests can be processed especially inside low priority or delayed queues. For low priority queue, completed tasks showing up for high memory usages. This new contract provides a way to make improvements. This is only for advanced scenarios, common JTF consumers would not access the request or have chance to use it incorrectly.
1 parent 43c1138 commit 7d17e8b

File tree

6 files changed

+30
-1
lines changed

6 files changed

+30
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace Microsoft.VisualStudio.Threading
5+
{
6+
/// <summary>
7+
/// An optional interface implemented by pending request state posted to the underline synchronization context. It allows synchronization context to remove completed requests.
8+
/// </summary>
9+
public interface IPendingExecutionRequestState
10+
{
11+
/// <summary>
12+
/// Gets a value indicating whether the current request has been completed, and can be skipped.
13+
/// </summary>
14+
bool IsCompleted { get; }
15+
}
16+
}

src/Microsoft.VisualStudio.Threading/JoinableTaskFactory.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ public void Dispose()
10581058
/// A delegate wrapper that ensures the delegate is only invoked at most once.
10591059
/// </summary>
10601060
[DebuggerDisplay("{DelegateLabel}")]
1061-
internal class SingleExecuteProtector
1061+
internal class SingleExecuteProtector : IPendingExecutionRequestState
10621062
{
10631063
/// <summary>
10641064
/// Executes the delegate if it has not already executed.
@@ -1112,6 +1112,11 @@ private SingleExecuteProtector(JoinableTask job)
11121112
this.job = job;
11131113
}
11141114

1115+
/// <summary>
1116+
/// Gets a value indicating whether the current request has been completed, and can be skipped.
1117+
/// </summary>
1118+
bool IPendingExecutionRequestState.IsCompleted => this.HasBeenExecuted;
1119+
11151120
/// <summary>
11161121
/// Gets a value indicating whether this instance has already executed.
11171122
/// </summary>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Microsoft.VisualStudio.Threading.AsyncBarrier.SignalAndWait(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
2+
Microsoft.VisualStudio.Threading.IPendingExecutionRequestState
3+
Microsoft.VisualStudio.Threading.IPendingExecutionRequestState.IsCompleted.get -> bool
24
static Microsoft.VisualStudio.Threading.JoinableTaskContext.CreateNoOpContext() -> Microsoft.VisualStudio.Threading.JoinableTaskContext!
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Microsoft.VisualStudio.Threading.AsyncBarrier.SignalAndWait(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
2+
Microsoft.VisualStudio.Threading.IPendingExecutionRequestState
3+
Microsoft.VisualStudio.Threading.IPendingExecutionRequestState.IsCompleted.get -> bool
24
static Microsoft.VisualStudio.Threading.JoinableTaskContext.CreateNoOpContext() -> Microsoft.VisualStudio.Threading.JoinableTaskContext!
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Microsoft.VisualStudio.Threading.AsyncBarrier.SignalAndWait(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
2+
Microsoft.VisualStudio.Threading.IPendingExecutionRequestState
3+
Microsoft.VisualStudio.Threading.IPendingExecutionRequestState.IsCompleted.get -> bool
24
static Microsoft.VisualStudio.Threading.JoinableTaskContext.CreateNoOpContext() -> Microsoft.VisualStudio.Threading.JoinableTaskContext!
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Microsoft.VisualStudio.Threading.AsyncBarrier.SignalAndWait(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
2+
Microsoft.VisualStudio.Threading.IPendingExecutionRequestState
3+
Microsoft.VisualStudio.Threading.IPendingExecutionRequestState.IsCompleted.get -> bool
24
static Microsoft.VisualStudio.Threading.JoinableTaskContext.CreateNoOpContext() -> Microsoft.VisualStudio.Threading.JoinableTaskContext!

0 commit comments

Comments
 (0)