-
Notifications
You must be signed in to change notification settings - Fork 67
/
ActionTickScheduler.cs
295 lines (246 loc) · 8.29 KB
/
ActionTickScheduler.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
292
293
294
295
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
namespace AltV.Net.Async
{
internal class ActionTickScheduler : ITickScheduler
{
private readonly struct ActionContainer
{
private readonly Action action;
public ActionContainer(Action action)
{
this.action = action;
}
public void Run()
{
try
{
action();
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
}
private readonly struct ActionContainer2
{
private readonly Action<object> action;
private readonly object state;
public ActionContainer2(Action<object> action, object state)
{
this.action = action;
this.state = state;
}
public void Run()
{
try
{
action(state);
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
}
private readonly struct ActionContainer3<TResult>
{
private readonly Func<object, TResult> func;
private readonly object state;
private readonly TaskCompletionSource<TResult> result;
public ActionContainer3(Func<object, TResult> func, object state, TaskCompletionSource<TResult> result)
{
this.func = func;
this.state = state;
this.result = result;
}
public void Run()
{
try
{
result.SetResult(func(state));
}
catch (Exception exception)
{
result.SetException(exception);
}
}
}
private readonly struct ActionContainer4<TResult>
{
private readonly Func<TResult> func;
private readonly TaskCompletionSource<TResult> result;
public ActionContainer4(Func<TResult> func, TaskCompletionSource<TResult> result)
{
this.func = func;
this.result = result;
}
public void Run()
{
try
{
result.SetResult(func());
}
catch (Exception exception)
{
result.SetException(exception);
}
}
}
private readonly struct ActionContainer5
{
private readonly Action action;
private readonly TaskCompletionSource<bool> result;
public ActionContainer5(Action action, TaskCompletionSource<bool> result)
{
this.action = action;
this.result = result;
}
public void Run()
{
try
{
action();
result.SetResult(true);
}
catch (Exception exception)
{
result.SetException(exception);
}
}
}
private readonly struct ActionContainer6
{
private readonly Action<object> action;
private readonly object state;
private readonly TaskCompletionSource<bool> result;
public ActionContainer6(Action<object> action, object state,TaskCompletionSource<bool> result)
{
this.action = action;
this.state = state;
this.result = result;
}
public void Run()
{
try
{
action(state);
result.SetResult(true);
}
catch (Exception exception)
{
result.SetException(exception);
}
}
}
private readonly struct ActionContainer7
{
private readonly Action action;
private readonly SemaphoreSlim semaphoreSlim;
public ActionContainer7(Action action, SemaphoreSlim semaphoreSlim)
{
this.action = action;
this.semaphoreSlim = semaphoreSlim;
}
public void Run()
{
try
{
action();
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
semaphoreSlim.Release();
}
}
private struct ActionContainer8
{
private readonly Action action;
private readonly SemaphoreSlim semaphoreSlim;
public Exception Exception;
public ActionContainer8(Action action, SemaphoreSlim semaphoreSlim, Exception exception)
{
this.action = action;
this.semaphoreSlim = semaphoreSlim;
Exception = exception;
}
public void Run()
{
try
{
action();
}
catch (Exception exception)
{
Exception = exception;
}
semaphoreSlim.Release();
}
}
private int runs;
private readonly ConcurrentQueue<Action> actions = new ();
public ActionTickScheduler()
{
}
public void Schedule(Action action)
{
actions.Enqueue(new ActionContainer(action).Run);
}
public void Schedule(Action<object> action, object state)
{
actions.Enqueue(new ActionContainer2(action, state).Run);
}
public void ScheduleBlocking(Action action, SemaphoreSlim semaphoreSlim)
{
actions.Enqueue(new ActionContainer7(action, semaphoreSlim).Run);
semaphoreSlim.Wait();
}
public void ScheduleBlockingThrows(Action action, SemaphoreSlim semaphoreSlim)
{
var container = new ActionContainer8(action, semaphoreSlim, null);
actions.Enqueue(container.Run);
semaphoreSlim.Wait();
if (container.Exception != null)
{
throw container.Exception;
}
}
public Task ScheduleTask(Action action)
{
var completionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
actions.Enqueue(new ActionContainer5(action, completionSource).Run);
return completionSource.Task;
}
public Task ScheduleTask(Action<object> action, object state)
{
var completionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
actions.Enqueue(new ActionContainer6(action, state, completionSource).Run);
return completionSource.Task;
}
public Task<TResult> ScheduleTask<TResult>(Func<TResult> action)
{
var completionSource = new TaskCompletionSource<TResult>(TaskCreationOptions.RunContinuationsAsynchronously);
actions.Enqueue(new ActionContainer4<TResult>(action, completionSource).Run);
return completionSource.Task;
}
public Task<TResult> ScheduleTask<TResult>(Func<object, TResult> action, object value)
{
var completionSource = new TaskCompletionSource<TResult>(TaskCreationOptions.RunContinuationsAsynchronously);
actions.Enqueue(new ActionContainer3<TResult>(action, value, completionSource).Run);
return completionSource.Task;
}
public void Tick()
{
runs = actions.Count;
while (runs-- > 0 && actions.TryDequeue(out var currentActionContainer))
{
currentActionContainer();
}
}
}
}