Skip to content

Commit d6e754a

Browse files
committed
added ConfigureAwait(false) to all await statements
1 parent c972bf5 commit d6e754a

File tree

6 files changed

+60
-40
lines changed

6 files changed

+60
-40
lines changed

src/PSParallelPipeline/Commands/InvokeParallelCommand.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,12 @@ protected override void BeginProcessing()
5959
.AddFunctions(Functions, this)
6060
.AddVariables(Variables, this);
6161

62-
PoolSettings poolSettings = new()
63-
{
64-
MaxRunspaces = ThrottleLimit,
65-
UseNewRunspace = UseNewRunspace,
66-
InitialSessionState = iss
67-
};
62+
PoolSettings poolSettings = new(
63+
ThrottleLimit, UseNewRunspace, iss);
6864

69-
TaskSettings workerSettings = new()
70-
{
71-
Script = ScriptBlock.ToString(),
72-
UsingStatements = ScriptBlock.GetUsingParameters(this)
73-
};
65+
TaskSettings workerSettings = new(
66+
ScriptBlock.ToString(),
67+
ScriptBlock.GetUsingParameters(this));
7468

7569
_worker = new Worker(poolSettings, workerSettings, _cts.Token);
7670
_worker.Run();

src/PSParallelPipeline/PSTask.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ static internal async Task<PSTask> CreateAsync(
3737
{
3838
PSTask ps = new(runspacePool);
3939
SetStreams(ps._internalStreams, runspacePool.Streams);
40-
ps.Runspace = await runspacePool.GetRunspaceAsync();
40+
ps.Runspace = await runspacePool
41+
.GetRunspaceAsync()
42+
.ConfigureAwait(false);
4143

4244
return ps
4345
.AddInput(input)
@@ -98,7 +100,7 @@ internal async Task InvokeAsync()
98100
try
99101
{
100102
using CancellationTokenRegistration _ = _pool.RegisterCancellation(Cancel);
101-
await InvokePowerShellAsync(_powershell, OutputStreams.Success);
103+
await InvokePowerShellAsync(_powershell, OutputStreams.Success).ConfigureAwait(false);
102104
}
103105
catch (Exception exception)
104106
{

src/PSParallelPipeline/PoolSettings.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22

33
namespace PSParallelPipeline;
44

5-
internal record struct PoolSettings(
6-
int MaxRunspaces,
7-
bool UseNewRunspace,
8-
InitialSessionState InitialSessionState);
5+
internal class PoolSettings(
6+
int maxRunspaces,
7+
bool useNewRunspace,
8+
InitialSessionState initialSessionState)
9+
{
10+
internal int MaxRunspaces { get; } = maxRunspaces;
11+
12+
internal bool UseNewRunspace { get; } = useNewRunspace;
13+
14+
internal InitialSessionState InitialSessionState { get; } = initialSessionState;
15+
}

src/PSParallelPipeline/RunspacePool.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,30 @@ namespace PSParallelPipeline;
88

99
internal sealed class RunspacePool : IDisposable
1010
{
11+
private readonly PoolSettings _settings;
12+
1113
private readonly CancellationToken _token;
1214

13-
private readonly InitialSessionState _iss;
15+
private InitialSessionState InitialSessionState { get => _settings.InitialSessionState; }
1416

1517
private readonly ConcurrentQueue<Runspace> _pool = [];
1618

1719
private readonly ConcurrentDictionary<Guid, Runspace> _created;
1820

19-
private readonly bool _useNew;
21+
private bool UseNewRunspace { get => _settings.UseNewRunspace; }
2022

2123
private readonly SemaphoreSlim _semaphore;
2224

2325
internal PSOutputStreams Streams { get; }
2426

25-
internal int MaxRunspaces { get; }
27+
internal int MaxRunspaces { get => _settings.MaxRunspaces; }
2628

2729
internal RunspacePool(
2830
PoolSettings settings,
2931
PSOutputStreams streams,
3032
CancellationToken token)
3133
{
32-
(MaxRunspaces, _useNew, _iss) = settings;
34+
_settings = settings;
3335
Streams = streams;
3436
_token = token;
3537
_semaphore = new SemaphoreSlim(MaxRunspaces, MaxRunspaces);
@@ -45,7 +47,7 @@ internal void PushRunspace(Runspace runspace)
4547
return;
4648
}
4749

48-
if (_useNew)
50+
if (UseNewRunspace)
4951
{
5052
runspace.Dispose();
5153
_created.TryRemove(runspace.InstanceId, out _);
@@ -61,15 +63,18 @@ internal CancellationTokenRegistration RegisterCancellation(Action callback) =>
6163

6264
private Runspace CreateRunspace()
6365
{
64-
Runspace rs = RunspaceFactory.CreateRunspace(_iss);
66+
Runspace rs = RunspaceFactory.CreateRunspace(InitialSessionState);
6567
_created[rs.InstanceId] = rs;
6668
rs.Open();
6769
return rs;
6870
}
6971

7072
internal async Task<Runspace> GetRunspaceAsync()
7173
{
72-
await _semaphore.WaitAsync(_token);
74+
await _semaphore
75+
.WaitAsync(_token)
76+
.ConfigureAwait(false);
77+
7378
if (_pool.TryDequeue(out Runspace runspace))
7479
{
7580
return runspace;

src/PSParallelPipeline/TaskSettings.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace PSParallelPipeline;
44

5-
internal record struct TaskSettings(
6-
string Script,
7-
Dictionary<string, object?> UsingStatements);
5+
internal class TaskSettings(
6+
string script,
7+
Dictionary<string, object?> usingStatements)
8+
{
9+
internal string Script { get; } = script;
10+
11+
internal Dictionary<string, object?> UsingStatements { get; } = usingStatements;
12+
}

src/PSParallelPipeline/Worker.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,15 @@ internal Worker(
3333
_pool = new RunspacePool(poolSettings, _streams, _token);
3434
}
3535

36-
internal void Wait() => _worker?.GetAwaiter().GetResult();
36+
internal void Wait() => _worker?.ConfigureAwait(false).GetAwaiter().GetResult();
3737

3838
internal void Enqueue(object? input) => _input.Add(input, _token);
3939

40-
internal bool TryTake(out PSOutputData output) =>
41-
_output.TryTake(out output, 0, _token);
40+
internal bool TryTake(out PSOutputData output) => _output.TryTake(out output, 0, _token);
4241

4342
internal void CompleteInputAdding() => _input.CompleteAdding();
4443

45-
internal IEnumerable<PSOutputData> GetConsumingEnumerable() =>
46-
_output.GetConsumingEnumerable(_token);
44+
internal IEnumerable<PSOutputData> GetConsumingEnumerable() => _output.GetConsumingEnumerable(_token);
4745

4846
internal void Run() => _worker = Task.Run(Start, cancellationToken: _token);
4947

@@ -57,13 +55,15 @@ private async Task Start()
5755
{
5856
if (tasks.Count == tasks.Capacity)
5957
{
60-
await ProcessAnyAsync(tasks);
58+
await ProcessAnyAsync(tasks).ConfigureAwait(false);
6159
}
6260

63-
PSTask task = await PSTask.CreateAsync(
64-
input: input,
65-
runspacePool: _pool,
66-
settings: _taskSettings);
61+
PSTask task = await PSTask
62+
.CreateAsync(
63+
input: input,
64+
runspacePool: _pool,
65+
settings: _taskSettings)
66+
.ConfigureAwait(false);
6767

6868
tasks.Add(task.InvokeAsync());
6969
}
@@ -72,16 +72,23 @@ private async Task Start()
7272
{ }
7373
finally
7474
{
75-
await Task.WhenAll(tasks);
75+
await Task
76+
.WhenAll(tasks)
77+
.ConfigureAwait(false);
78+
7679
_output.CompleteAdding();
7780
}
7881
}
7982

8083
private static async Task ProcessAnyAsync(List<Task> tasks)
8184
{
82-
Task task = await Task.WhenAny(tasks);
85+
Task task = await Task
86+
.WhenAny(tasks)
87+
.ConfigureAwait(false);
88+
8389
tasks.Remove(task);
84-
await task;
90+
await task
91+
.ConfigureAwait(false);
8592
}
8693

8794
public void Dispose()

0 commit comments

Comments
 (0)