-
Notifications
You must be signed in to change notification settings - Fork 0
/
StubFiber.cs
205 lines (189 loc) · 5.77 KB
/
StubFiber.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
using System;
using System.Collections.Concurrent;
using AsyncFiberWorks.Core;
using AsyncFiberWorks.Executors;
using AsyncFiberWorks.Threading;
namespace AsyncFiberWorks.Fibers
{
/// <summary>
/// This is a fiber that needs to be pumped manually.
/// Queued actions are added to the pending list.
/// Consume them by periodically calling methods for execution.
/// Periodically call a method for execution. They are executed on their calling thread.
/// </summary>
public sealed class StubFiber : IFiber
{
private readonly object _lock = new object();
private readonly ConcurrentQueue<Action> _pending = new ConcurrentQueue<Action>();
private readonly IExecutor _executor;
private readonly FiberExecutionEventArgs _eventArgs;
private readonly ThreadPoolAdaptor _queueUsedDuringPause;
private bool _enabledPause;
private int _paused = 0;
/// <summary>
/// Create a stub fiber with a simple executor.
/// </summary>
public StubFiber()
: this(SimpleExecutor.Instance)
{
}
/// <summary>
/// Create a stub fiber with the specified executor.
/// </summary>
/// <param name="executor"></param>
public StubFiber(IExecutor executor)
{
_executor = executor;
_queueUsedDuringPause = new ThreadPoolAdaptor(new DefaultQueue());
_eventArgs = new FiberExecutionEventArgs(this.Pause, this.Resume, _queueUsedDuringPause);
}
/// <summary>
/// Enqueue a single action.
/// </summary>
/// <param name="action"></param>
public void Enqueue(Action action)
{
_pending.Enqueue(action);
}
/// <summary>
/// Execute until there are no more pending actions.
/// </summary>
public void ExecuteAll()
{
bool isPaused = ExecuteResumeProcess();
if (isPaused)
{
return;
}
while (true)
{
lock (_lock)
{
if (_paused != 0)
{
break;
}
}
if (!_pending.TryDequeue(out var toExecute))
{
break;
}
_executor.Execute(toExecute);
}
}
/// <summary>
/// Execute only what is pending now.
/// </summary>
public void ExecuteOnlyPendingNow()
{
bool isPaused = ExecuteResumeProcess();
if (isPaused)
{
return;
}
int count = _pending.Count;
while (true)
{
lock (_lock)
{
if (_paused != 0)
{
break;
}
}
if (!_pending.TryDequeue(out var toExecute))
{
break;
}
_executor.Execute(toExecute);
count -= 1;
if (count <= 0)
{
break;
}
}
}
/// <summary>
/// Pauses the consumption of the task queue.
/// This is only called during an Execute in the fiber.
/// </summary>
/// <exception cref="InvalidOperationException">Pause was called twice.</exception>
private void Pause()
{
lock (_lock)
{
if (_paused != 0)
{
throw new InvalidOperationException("Pause was called twice.");
}
if (!_enabledPause)
{
throw new InvalidOperationException("Pause is only possible within the execution context.");
}
_paused = 1;
}
}
/// <summary>
/// Resumes consumption of a paused task queue.
/// </summary>
/// <exception cref="InvalidOperationException">Resume was called in the unpaused state.</exception>
private void Resume()
{
lock (_lock)
{
if (_paused == 0)
{
throw new InvalidOperationException("Resume was called in the unpaused state.");
}
if (_paused != 1)
{
throw new InvalidOperationException("Resume was called twice.");
}
_paused = 2;
}
}
private bool ExecuteResumeProcess()
{
lock (_lock)
{
if (_paused == 0)
{
return false;
}
}
_queueUsedDuringPause.Queue((_) =>
{
_queueUsedDuringPause.Stop();
});
_queueUsedDuringPause.Run();
lock (_lock)
{
if (_paused != 2)
{
return true;
}
_paused = 0;
return false;
}
}
/// <summary>
/// Enqueue a single action. It is executed sequentially.
/// </summary>
/// <param name="action">Action to be executed.</param>
public void Enqueue(Action<FiberExecutionEventArgs> action)
{
this.Enqueue(() =>
{
lock (_lock)
{
_enabledPause = true;
}
action(_eventArgs);
lock (_lock)
{
_enabledPause = false;
}
});
}
}
}