Repository navigation
Expand file tree
/
Copy pathScriptExecutionState.cs
More file actions
105 lines (89 loc) · 3.63 KB
/
Copy pathScriptExecutionState.cs
File metadata and controls
105 lines (89 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.CodeAnalysis.Scripting
{
/// <summary>
/// Represents the submission states and globals that get passed to a script entry point when run.
/// </summary>
internal sealed class ScriptExecutionState
{
private object[] _submissionStates;
private int _count;
private int _frozen;
private ScriptExecutionState(object[] submissionStates, int count)
{
_submissionStates = submissionStates;
_count = count;
}
public static ScriptExecutionState Create(object globals)
{
// first submission state is the globals.
var submissionStates = new object[2];
submissionStates[0] = globals;
return new ScriptExecutionState(submissionStates, count: 1);
}
public ScriptExecutionState FreezeAndClone()
{
// freeze state so it can no longer be modified.
var wasAlreadyFrozen = Interlocked.CompareExchange(ref _frozen, 1, 0) == 1;
if (wasAlreadyFrozen)
{
// since only one state can add to the submissions, if its already been frozen and clone before
// make a copy of the visible contents for the new state.
var copy = new object[_count];
Array.Copy(_submissionStates, copy, _count);
return new ScriptExecutionState(copy, _count);
}
else
{
// cloned state can continue to add submission states
return new ScriptExecutionState(_submissionStates, _count);
}
}
public int SubmissionStateCount => _count;
public object GetSubmissionState(int index)
{
Debug.Assert(index >= 0 && index < _count);
return _submissionStates[index];
}
internal async Task<TResult> RunSubmissionsAsync<TResult>(
ImmutableArray<Func<object[], Task>> precedingExecutors,
Func<object[], Task> currentExecutor,
CancellationToken cancellationToken)
{
Debug.Assert(_frozen == 0);
foreach (var executor in precedingExecutors)
{
cancellationToken.ThrowIfCancellationRequested();
EnsureStateCapacity();
await executor(_submissionStates).ConfigureAwait(continueOnCapturedContext: false);
AdvanceStateCounter();
}
cancellationToken.ThrowIfCancellationRequested();
EnsureStateCapacity();
TResult result = await ((Task<TResult>)currentExecutor(_submissionStates)).ConfigureAwait(continueOnCapturedContext: false);
AdvanceStateCounter();
return result;
}
private void EnsureStateCapacity()
{
// make sure there is enough free space for the submission to add its state
if (_count >= _submissionStates.Length)
{
Array.Resize(ref _submissionStates, Math.Max(_count, _submissionStates.Length * 2));
}
}
private void AdvanceStateCounter()
{
// check to see if state was added (submissions that don't make declarations don't add state)
if (_submissionStates[_count] != null)
{
_count++;
}
}
}
}