Repository navigation
Expand file tree
/
Copy pathThreadling.cs
More file actions
169 lines (151 loc) · 4.29 KB
/
Copy pathThreadling.cs
File metadata and controls
169 lines (151 loc) · 4.29 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
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
using System.Collections.Concurrent;
using System.Diagnostics;
namespace Scopie;
internal readonly struct IdleActionResult
{
public readonly int Kind;
public readonly TimeSpan TimeSpan;
private IdleActionResult(int kind, TimeSpan timeSpan)
{
Kind = kind;
TimeSpan = timeSpan;
}
public static IdleActionResult LoopImmediately => new(0, TimeSpan.Zero);
public static IdleActionResult WaitForNextEvent => new(1, TimeSpan.Zero);
public static IdleActionResult WaitWithTimeout(TimeSpan timeSpan) => new(2, timeSpan);
}
internal sealed class Threadling : IDisposable
{
private readonly Thread _thread;
private readonly BlockingCollection<Action> _sendToThread = [];
private Func<IdleActionResult>? _idleAction;
public Threadling(Func<IdleActionResult>? idleAction)
{
_idleAction = idleAction;
_thread = new Thread(RunThread);
_thread.Start();
}
public Func<IdleActionResult>? IdleAction
{
set
{
_idleAction = value;
_sendToThread.Add(() => { });
}
}
public void Dispose()
{
_sendToThread.CompleteAdding();
_sendToThread.Dispose();
}
public Task<T> Do<T>(Func<T> action)
{
if (_thread == Thread.CurrentThread)
{
try
{
return Task.FromResult(action());
}
catch (Exception e)
{
return Task.FromException<T>(e);
}
}
TaskCompletionSource<T> tcs = new();
_sendToThread.Add(() =>
{
try
{
tcs.SetResult(action());
}
catch (Exception e)
{
tcs.SetException(e);
}
});
return tcs.Task;
}
public Task Do(Action action)
{
if (_thread == Thread.CurrentThread)
{
try
{
action();
return Task.CompletedTask;
}
catch (Exception e)
{
return Task.FromException<int>(e);
}
}
TaskCompletionSource<int> tcs = new();
_sendToThread.Add(() =>
{
try
{
action();
tcs.SetResult(0);
}
catch (Exception e)
{
tcs.SetException(e);
}
});
return tcs.Task;
}
private void RunThread()
{
var idleActionResult = IdleActionResult.WaitForNextEvent;
var stopwatch = new Stopwatch();
while (true)
{
var first = true;
while (true)
{
Action action;
try
{
if (first && idleActionResult.Kind == 1)
action = _sendToThread.Take();
else
{
if (idleActionResult.Kind == 2)
{
TimeSpan elapsed;
if (first)
{
stopwatch.Restart();
elapsed = TimeSpan.Zero;
}
else
elapsed = stopwatch.Elapsed;
if (elapsed > idleActionResult.TimeSpan)
break;
var toWait = idleActionResult.TimeSpan - elapsed;
if (!_sendToThread.TryTake(out action!, toWait))
break;
}
else if (!_sendToThread.TryTake(out action!))
break;
}
}
catch
{
return;
}
action();
first = false;
}
try
{
idleActionResult = _idleAction?.Invoke() ?? IdleActionResult.WaitForNextEvent;
}
catch (Exception e)
{
ExceptionReporter.Report(e);
idleActionResult = IdleActionResult.WaitForNextEvent;
}
}
}
}