提交 27ea137e 编写于 作者: S Sam Harwell

Pass CancellationToken instead of CancellationTokenSource

上级 ff0359d9
......@@ -177,19 +177,19 @@ protected void Cancel_NoLock(object key)
}
}
public bool TryTake(TKey key, out WorkItem workInfo, out CancellationTokenSource source)
public bool TryTake(TKey key, out WorkItem workInfo, out CancellationToken cancellationToken)
{
lock (_gate)
{
if (TryTake_NoLock(key, out workInfo))
{
source = GetNewCancellationSource_NoLock(key);
cancellationToken = GetNewCancellationToken_NoLock(key);
workInfo.AsyncToken.Dispose();
return true;
}
else
{
source = null;
cancellationToken = CancellationToken.None;
return false;
}
}
......@@ -199,33 +199,33 @@ public bool TryTake(TKey key, out WorkItem workInfo, out CancellationTokenSource
ProjectId preferableProjectId,
ProjectDependencyGraph dependencyGraph,
IDiagnosticAnalyzerService analyzerService,
out WorkItem workItem, out CancellationTokenSource source)
out WorkItem workItem, out CancellationToken cancellationToken)
{
lock (_gate)
{
// there must be at least one item in the map when this is called unless host is shutting down.
if (TryTakeAnyWork_NoLock(preferableProjectId, dependencyGraph, analyzerService, out workItem))
{
source = GetNewCancellationSource_NoLock(workItem.Key);
cancellationToken = GetNewCancellationToken_NoLock(workItem.Key);
workItem.AsyncToken.Dispose();
return true;
}
else
{
source = null;
cancellationToken = CancellationToken.None;
return false;
}
}
}
protected CancellationTokenSource GetNewCancellationSource_NoLock(object key)
protected CancellationToken GetNewCancellationToken_NoLock(object key)
{
Debug.Assert(!_cancellationMap.ContainsKey(key));
var source = new CancellationTokenSource();
_cancellationMap.Add(key, source);
return source;
return source.Token;
}
protected ProjectId GetBestProjectId_NoLock<T>(
......
......@@ -124,13 +124,13 @@ protected override async Task ExecuteAsync()
}
}
private bool GetNextWorkItem(out WorkItem workItem, out CancellationTokenSource documentCancellation)
private bool GetNextWorkItem(out WorkItem workItem, out CancellationToken cancellationToken)
{
// GetNextWorkItem since it can't fail. we still return bool to confirm that this never fail.
var documentId = _processor._documentTracker.TryGetActiveDocument();
if (documentId != null)
{
if (_workItemQueue.TryTake(documentId, out workItem, out documentCancellation))
if (_workItemQueue.TryTake(documentId, out workItem, out cancellationToken))
{
return true;
}
......@@ -141,10 +141,10 @@ private bool GetNextWorkItem(out WorkItem workItem, out CancellationTokenSource
dependencyGraph: _processor.DependencyGraph,
analyzerService: _processor.DiagnosticAnalyzerService,
workItem: out workItem,
source: out documentCancellation);
cancellationToken: out cancellationToken);
}
private async Task ProcessDocumentAsync(Solution solution, ImmutableArray<IIncrementalAnalyzer> analyzers, WorkItem workItem, CancellationTokenSource source)
private async Task ProcessDocumentAsync(Solution solution, ImmutableArray<IIncrementalAnalyzer> analyzers, WorkItem workItem, CancellationToken cancellationToken)
{
if (this.CancellationToken.IsCancellationRequested)
{
......@@ -156,9 +156,8 @@ private async Task ProcessDocumentAsync(Solution solution, ImmutableArray<IIncre
try
{
using (Logger.LogBlock(FunctionId.WorkCoordinator_ProcessDocumentAsync, w => w.ToString(), workItem, source.Token))
using (Logger.LogBlock(FunctionId.WorkCoordinator_ProcessDocumentAsync, w => w.ToString(), workItem, cancellationToken))
{
var cancellationToken = source.Token;
var document = solution.GetDocument(documentId);
if (document != null)
{
......
......@@ -114,7 +114,7 @@ private void CancelRunningTaskIfHigherQueueHasWorkItem()
_workItemQueue.RequestCancellationOnRunningTasks();
}
private async Task ProcessProjectAsync(ImmutableArray<IIncrementalAnalyzer> analyzers, WorkItem workItem, CancellationTokenSource source)
private async Task ProcessProjectAsync(ImmutableArray<IIncrementalAnalyzer> analyzers, WorkItem workItem, CancellationToken cancellationToken)
{
if (this.CancellationToken.IsCancellationRequested)
{
......@@ -128,10 +128,8 @@ private async Task ProcessProjectAsync(ImmutableArray<IIncrementalAnalyzer> anal
try
{
using (Logger.LogBlock(FunctionId.WorkCoordinator_ProcessProjectAsync, w => w.ToString(), workItem, source.Token))
using (Logger.LogBlock(FunctionId.WorkCoordinator_ProcessProjectAsync, w => w.ToString(), workItem, cancellationToken))
{
var cancellationToken = source.Token;
var project = processingSolution.GetProject(projectId);
if (project != null)
{
......@@ -195,14 +193,12 @@ public override void Shutdown()
internal void WaitUntilCompletion_ForTestingPurposesOnly(ImmutableArray<IIncrementalAnalyzer> analyzers, List<WorkItem> items)
{
CancellationTokenSource source = new CancellationTokenSource();
var uniqueIds = new HashSet<ProjectId>();
foreach (var item in items)
{
if (uniqueIds.Add(item.ProjectId))
{
ProcessProjectAsync(analyzers, item, source).Wait();
ProcessProjectAsync(analyzers, item, CancellationToken.None).Wait();
}
}
}
......
......@@ -301,7 +301,7 @@ private void RemoveHigherPriorityDocument(DocumentId documentId)
}
}
private async Task ProcessDocumentAsync(ImmutableArray<IIncrementalAnalyzer> analyzers, WorkItem workItem, CancellationTokenSource source)
private async Task ProcessDocumentAsync(ImmutableArray<IIncrementalAnalyzer> analyzers, WorkItem workItem, CancellationToken cancellationToken)
{
if (this.CancellationToken.IsCancellationRequested)
{
......@@ -332,9 +332,8 @@ private async Task ProcessDocumentAsync(ImmutableArray<IIncrementalAnalyzer> ana
var solution = this.Processor.CurrentSolution;
try
{
using (Logger.LogBlock(FunctionId.WorkCoordinator_ProcessDocumentAsync, w => w.ToString(), workItem, source.Token))
using (Logger.LogBlock(FunctionId.WorkCoordinator_ProcessDocumentAsync, w => w.ToString(), workItem, cancellationToken))
{
var cancellationToken = source.Token;
var document = solution.GetDocument(documentId);
if (document != null)
......@@ -542,11 +541,9 @@ public override void Shutdown()
internal void WaitUntilCompletion_ForTestingPurposesOnly(ImmutableArray<IIncrementalAnalyzer> analyzers, List<WorkItem> items)
{
CancellationTokenSource source = new CancellationTokenSource();
foreach (var item in items)
{
ProcessDocumentAsync(analyzers, item, source).Wait();
ProcessDocumentAsync(analyzers, item, CancellationToken.None).Wait();
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册