提交 ad966b80 编写于 作者: S Sam Harwell

Simplify common calls to Parallel.For with a RoslynParallel.For helper

上级 89567d8b
......@@ -11,13 +11,12 @@
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis;
namespace Microsoft.CodeAnalysis.CSharp
{
......@@ -60,9 +59,10 @@ protected CSharpCompiler(CSharpCommandLineParser parser, string responseFile, st
if (Arguments.CompilationOptions.ConcurrentBuild)
{
Parallel.For(0, sourceFiles.Length, UICultureUtilities.WithCurrentUICulture<int>(i =>
{
try
RoslynParallel.For(
0,
sourceFiles.Length,
UICultureUtilities.WithCurrentUICulture<int>(i =>
{
//NOTE: order of trees is important!!
trees[i] = ParseFile(
......@@ -72,12 +72,8 @@ protected CSharpCompiler(CSharpCommandLineParser parser, string responseFile, st
sourceFiles[i],
diagnosticBag,
out normalizedFilePaths[i]);
}
catch (Exception e) when (FatalError.Report(e))
{
throw ExceptionUtilities.Unreachable;
}
}));
}),
CancellationToken.None);
}
else
{
......
......@@ -51,8 +51,6 @@ public sealed partial class CSharpCompilation : Compilation
//
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
internal static readonly ParallelOptions DefaultParallelOptions = new ParallelOptions();
private readonly CSharpCompilationOptions _options;
private readonly Lazy<Imports> _globalImports;
private readonly Lazy<Imports> _previousSubmissionImports;
......@@ -2472,31 +2470,16 @@ internal override void GetDiagnostics(CompilationStage stage, bool includeEarlie
var syntaxTrees = this.SyntaxTrees;
if (this.Options.ConcurrentBuild)
{
var parallelOptions = cancellationToken.CanBeCanceled
? new ParallelOptions() { CancellationToken = cancellationToken }
: DefaultParallelOptions;
Parallel.For(0, syntaxTrees.Length, parallelOptions,
RoslynParallel.For(
0,
syntaxTrees.Length,
UICultureUtilities.WithCurrentUICulture<int>(i =>
{
try
{
var syntaxTree = syntaxTrees[i];
AppendLoadDirectiveDiagnostics(builder, _syntaxAndDeclarations, syntaxTree);
builder.AddRange(syntaxTree.GetDiagnostics(cancellationToken));
}
catch (Exception e) when (FatalError.ReportUnlessCanceled(e))
{
throw ExceptionUtilities.Unreachable;
}
catch (OperationCanceledException e) when (cancellationToken.IsCancellationRequested && e.CancellationToken != cancellationToken)
{
// Parallel.For checks for a specific cancellation token, so make sure we throw with the
// correct one.
cancellationToken.ThrowIfCancellationRequested();
throw ExceptionUtilities.Unreachable;
}
}));
var syntaxTree = syntaxTrees[i];
AppendLoadDirectiveDiagnostics(builder, _syntaxAndDeclarations, syntaxTree);
builder.AddRange(syntaxTree.GetDiagnostics(cancellationToken));
}),
cancellationToken);
}
else
{
......
......@@ -2,14 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
......@@ -49,29 +43,11 @@ internal override void ForceComplete(SourceLocation locationOpt, CancellationTok
if (this.DeclaringCompilation.Options.ConcurrentBuild)
{
var po = cancellationToken.CanBeCanceled
? new ParallelOptions() { CancellationToken = cancellationToken }
: CSharpCompilation.DefaultParallelOptions;
Parallel.For(0, members.Length, po, UICultureUtilities.WithCurrentUICulture<int>(i =>
{
try
{
var member = members[i];
ForceCompleteMemberByLocation(locationOpt, member, cancellationToken);
}
catch (Exception e) when (FatalError.ReportUnlessCanceled(e))
{
throw ExceptionUtilities.Unreachable;
}
catch (OperationCanceledException e) when (cancellationToken.IsCancellationRequested && e.CancellationToken != cancellationToken)
{
// Parallel.For checks for a specific cancellation token, so make sure we throw with the
// correct one.
cancellationToken.ThrowIfCancellationRequested();
throw ExceptionUtilities.Unreachable;
}
}));
RoslynParallel.For(
0,
members.Length,
UICultureUtilities.WithCurrentUICulture<int>(i => ForceCompleteMemberByLocation(locationOpt, members[i], cancellationToken)),
cancellationToken);
foreach (var member in members)
{
......
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
namespace Roslyn.Utilities
{
internal static class RoslynParallel
{
internal static readonly ParallelOptions DefaultParallelOptions = new ParallelOptions();
/// <inheritdoc cref="Parallel.For(int, int, ParallelOptions, Action{int})"/>
public static ParallelLoopResult For(int fromInclusive, int toExclusive, Action<int> body, CancellationToken cancellationToken)
{
var parallelOptions = cancellationToken.CanBeCanceled
? new ParallelOptions { CancellationToken = cancellationToken }
: DefaultParallelOptions;
return Parallel.For(fromInclusive, toExclusive, parallelOptions, errorHandlingBody);
// Local function
void errorHandlingBody(int i)
{
try
{
body(i);
}
catch (Exception e) when (FatalError.ReportUnlessCanceled(e, cancellationToken))
{
throw ExceptionUtilities.Unreachable;
}
catch (OperationCanceledException e) when (cancellationToken.IsCancellationRequested && e.CancellationToken != cancellationToken)
{
// Parallel.For checks for a specific cancellation token, so make sure we throw with the
// correct one.
cancellationToken.ThrowIfCancellationRequested();
throw ExceptionUtilities.Unreachable;
}
}
}
}
}
......@@ -4,6 +4,7 @@
Imports System.Collections.Immutable
Imports System.IO
Imports System.Threading
Imports Microsoft.CodeAnalysis.Collections
Imports Microsoft.CodeAnalysis.Diagnostics
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
......@@ -99,22 +100,21 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Dim trees(sourceFiles.Length - 1) As SyntaxTree
If Arguments.CompilationOptions.ConcurrentBuild Then
Parallel.For(0, sourceFiles.Length,
UICultureUtilities.WithCurrentUICulture(Of Integer)(
RoslynParallel.For(
0,
sourceFiles.Length,
UICultureUtilities.WithCurrentUICulture(Of Integer)(
Sub(i As Integer)
Try
' NOTE: order of trees is important!!
trees(i) = ParseFile(
' NOTE: order of trees is important!!
trees(i) = ParseFile(
consoleOutput,
parseOptions,
scriptParseOptions,
hadErrors,
sourceFiles(i),
errorLogger)
Catch ex As Exception When FatalError.Report(ex)
Throw ExceptionUtilities.Unreachable
End Try
End Sub))
End Sub),
CancellationToken.None)
Else
For i = 0 To sourceFiles.Length - 1
' NOTE: order of trees is important!!
......
......@@ -2087,20 +2087,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
' Embedded trees shouldn't have any errors, let's avoid making decision if they should be added too early.
' Otherwise IDE performance might be affect.
If Options.ConcurrentBuild Then
Dim options = New ParallelOptions() With {.CancellationToken = cancellationToken}
Parallel.For(0, SyntaxTrees.Length, options, UICultureUtilities.WithCurrentUICulture(
Sub(i As Integer)
Try
RoslynParallel.For(
0,
SyntaxTrees.Length,
UICultureUtilities.WithCurrentUICulture(
Sub(i As Integer)
builder.AddRange(SyntaxTrees(i).GetDiagnostics(cancellationToken))
Catch e As Exception When FatalError.ReportUnlessCanceled(e)
Throw ExceptionUtilities.Unreachable
Catch e As OperationCanceledException When cancellationToken.IsCancellationRequested AndAlso e.CancellationToken <> cancellationToken
' Parallel.For checks for a specific cancellation token, so make sure we throw with the
' correct one.
cancellationToken.ThrowIfCancellationRequested()
Throw ExceptionUtilities.Unreachable
End Try
End Sub))
End Sub),
cancellationToken)
Else
For Each tree In SyntaxTrees
cancellationToken.ThrowIfCancellationRequested()
......
......@@ -616,22 +616,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Dim trees = ArrayBuilder(Of SyntaxTree).GetInstance()
trees.AddRange(SyntaxTrees)
Dim options = New ParallelOptions() With {.CancellationToken = cancellationToken}
Parallel.For(0, trees.Count, options,
RoslynParallel.For(
0,
trees.Count,
UICultureUtilities.WithCurrentUICulture(
Sub(i As Integer)
Try
cancellationToken.ThrowIfCancellationRequested()
TryGetSourceFile(trees(i)).GenerateAllDeclarationErrors()
Catch e As Exception When FatalError.ReportUnlessCanceled(e)
Throw ExceptionUtilities.Unreachable
Catch e As OperationCanceledException When cancellationToken.IsCancellationRequested AndAlso e.CancellationToken <> cancellationToken
' Parallel.For checks for a specific cancellation token, so make sure we throw with the
' correct one.
cancellationToken.ThrowIfCancellationRequested()
Throw ExceptionUtilities.Unreachable
End Try
End Sub))
TryGetSourceFile(trees(i)).GenerateAllDeclarationErrors()
End Sub),
cancellationToken)
trees.Free()
Else
For Each tree In SyntaxTrees
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册