-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvaluatorPool.cs
291 lines (230 loc) · 9.06 KB
/
EvaluatorPool.cs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#pragma warning disable 0420
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace JSIL.Tests {
public class EvaluatorPool : IDisposable {
public const int Capacity = 2;
public readonly string JSShellPath;
public readonly string Options;
public readonly Dictionary<string, string> EnvironmentVariables;
public readonly Action<Evaluator> Initializer;
private readonly ConcurrentBag<Evaluator> Evaluators = new ConcurrentBag<Evaluator>();
private readonly AutoResetEvent EvaluatorReadySignal = new AutoResetEvent(false);
private readonly AutoResetEvent WakeSignal = new AutoResetEvent(false);
private readonly Thread PoolManager;
private volatile int IsDisposed = 0;
public EvaluatorPool (string jsShellPath, string options, Action<Evaluator> initializer, Dictionary<string, string> environmentVariables = null) {
JSShellPath = jsShellPath;
Options = options;
Initializer = initializer;
EnvironmentVariables = environmentVariables;
PoolManager = new Thread(ThreadProc);
PoolManager.Priority = ThreadPriority.AboveNormal;
PoolManager.IsBackground = true;
PoolManager.Name = "Evaluator Pool Manager";
PoolManager.Start();
}
~EvaluatorPool () {
Dispose();
}
public void Dispose () {
if (Interlocked.CompareExchange(ref IsDisposed, 1, 0) != 0)
return;
GC.SuppressFinalize(this);
// The pool manager might dispose the signal before we get to it.
try {
WakeSignal.Set();
} catch {
}
if (!PoolManager.Join(100))
throw new Exception("Pool manager thread hung");
}
public Evaluator Get () {
Evaluator result;
var started = DateTime.UtcNow.Ticks;
while (!Evaluators.TryTake(out result)) {
WakeSignal.Set();
EvaluatorReadySignal.WaitOne();
}
WakeSignal.Set();
var ended = DateTime.UtcNow.Ticks;
// Console.WriteLine("Took {0:0000}ms to get an evaluator", TimeSpan.FromTicks(ended - started).TotalMilliseconds);
return result;
}
private Evaluator CreateEvaluator () {
var result = new Evaluator(
JSShellPath, Options, EnvironmentVariables
);
Initializer(result);
return result;
}
private void ThreadProc () {
try {
while (IsDisposed == 0) {
while (Evaluators.Count < Capacity)
Evaluators.Add(CreateEvaluator());
EvaluatorReadySignal.Set();
WakeSignal.WaitOne();
}
} finally {
EvaluatorReadySignal.Dispose();
WakeSignal.Dispose();
Evaluator evaluator;
while (Evaluators.TryTake(out evaluator)) {
evaluator.Dispose();
}
}
}
}
public class Evaluator : IDisposable {
public const bool TraceInput = false;
public const bool TraceOutput = false;
public readonly Process Process;
public readonly int Id;
private static int NextId = 0;
private volatile int InputClosed = 0;
private volatile int IsDisposed = 0;
private volatile int _ExitCode = 0;
private volatile string _StdOut = null, _StdErr = null;
private ManualResetEventSlim _DisposedSignal = new ManualResetEventSlim(false);
private Action _JoinImpl;
public Evaluator (string jsShellPath, string options, Dictionary<string, string> environmentVariables = null) {
Id = Interlocked.Increment(ref NextId);
var psi = new ProcessStartInfo(
jsShellPath, options
) {
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden
};
if (environmentVariables != null) {
foreach (var kvp in environmentVariables)
psi.EnvironmentVariables[kvp.Key] = kvp.Value;
}
Process = Process.Start(psi);
var streamsSignal = new ManualResetEventSlim(false);
var task = ReadStreams(streamsSignal);
_JoinImpl = () => {
WaitHandle.WaitAny(
new WaitHandle[] { streamsSignal.WaitHandle, _DisposedSignal.WaitHandle },
60000
);
};
}
private async System.Threading.Tasks.Task ReadStreams (ManualResetEventSlim signal) {
var stdout = Process.StandardOutput.ReadToEndAsync();
var stderr = Process.StandardError.ReadToEndAsync();
_StdOut = FixEncodingIfNeed(await stdout);
_StdErr = FixEncodingIfNeed(await stderr);
signal.Set();
}
private static string FixEncodingIfNeed(string input)
{
if (input!= null && input.StartsWith("獪"))
{
return Encoding.UTF8.GetString(Encoding.Unicode.GetBytes(input));
}
return input;
}
/// <summary>
/// Not available until process has exited.
/// </summary>
public string StandardOutput {
get {
return _StdOut;
}
}
/// <summary>
/// Not available until process has exited.
/// </summary>
public string StandardError {
get {
return _StdErr;
}
}
public int ExitCode {
get {
return _ExitCode;
}
}
public void WriteInput (string text) {
if (IsDisposed != 0)
throw new ObjectDisposedException("evaluator");
if (InputClosed != 0)
throw new InvalidOperationException("Input stream already closed");
#pragma warning disable 0162
if (TraceInput) {
foreach (var line in text.Split(new [] { Environment.NewLine }, StringSplitOptions.None))
Debug.WriteLine("{0:X2} in : {1}", Id, line);
}
#pragma warning restore 0162
// HACK: Workaround for NUnit/StreamWriter bug on Mono
var bytes = System.Text.Encoding.UTF8.GetBytes(text);
Process.StandardInput.BaseStream.Write(bytes, 0, bytes.Length);
Process.StandardInput.BaseStream.Flush();
//Process.StandardInput.Write(text);
//Process.StandardInput.Flush();
}
public void WriteInput (string format, params object[] args) {
WriteInput(String.Format(format, args));
}
public void CloseInput () {
if (IsDisposed != 0)
throw new ObjectDisposedException("evaluator");
if (Interlocked.CompareExchange(ref InputClosed, 1, 0) != 0)
return;
// HACK: Workaround for NUnit/StreamWriter bug on Mono
Process.StandardInput.BaseStream.Flush();
Process.StandardInput.BaseStream.Close();
//Process.StandardInput.Flush();
//Process.StandardInput.Close();
}
public void Join () {
if (IsDisposed != 0)
throw new ObjectDisposedException("evaluator");
CloseInput();
_JoinImpl();
Process.WaitForExit();
_ExitCode = Process.ExitCode;
#pragma warning disable 0162
if (TraceOutput) {
Debug.WriteLine("{0:X2} exit {1}", Id, _ExitCode);
foreach (var line in _StdOut.Split(new[] { Environment.NewLine }, StringSplitOptions.None))
Debug.WriteLine("{0:X2} out: {1}", Id, line);
foreach (var line in _StdErr.Split(new[] { Environment.NewLine }, StringSplitOptions.None))
Debug.WriteLine("{0:X2} err: {1}", Id, line);
}
#pragma warning restore 0162
}
public void Dispose () {
if (Interlocked.CompareExchange(ref IsDisposed, 1, 0) != 0)
return;
_DisposedSignal.Set();
// The Process class likes to throw exceptions randomly in accessors and method calls.
try {
if (!Process.HasExited) {
Process.WaitForExit(1);
Process.Kill();
} else
_ExitCode = Process.ExitCode;
} catch {
}
try {
Process.Close();
} catch {
}
try {
Process.Dispose();
} catch {
}
}
}
}