-
Notifications
You must be signed in to change notification settings - Fork 2
/
Loop.cs
480 lines (435 loc) · 11.6 KB
/
Loop.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
using System;
using System.Threading;
using System.Collections.Generic;
using UnityEngine;
using UnityObject = UnityEngine.Object;
namespace TinyMUD
{
public static class Loop
{
public interface Event { }
private class EmptyEvent : Event { }
private static readonly Event _EmptyEvent = new EmptyEvent();
private static Action<Exception> errfn = e => { };
private static readonly object actions_mtx = new object();
private static List<Action> actions = new List<Action>();
private static List<Action> actions_tmp = new List<Action>();
private static readonly Queue<KeyValuePair<Action, Action>> async_actions = new Queue<KeyValuePair<Action, Action>>();
private static List<KeyValuePair<AsyncOperation, Action<AsyncOperation>>> operations = new List<KeyValuePair<AsyncOperation, Action<AsyncOperation>>>();
private static List<KeyValuePair<AsyncOperation, Action<AsyncOperation>>> operations_tmp = new List<KeyValuePair<AsyncOperation, Action<AsyncOperation>>>();
private static readonly List<KeyValuePair<AsyncOperation, Action<AsyncOperation>>> operations_action = new List<KeyValuePair<AsyncOperation, Action<AsyncOperation>>>();
private static readonly Dictionary<string, Dictionary<Delegate, Action<string, Event>>> events = new Dictionary<string,Dictionary<Delegate,Action<string,Event>>>();
private static readonly Stack<List<Action<string, Event>>> events_action = new Stack<List<Action<string, Event>>>();
private static List<Action> idles = new List<Action>();
private static List<Action> idles_tmp = new List<Action>();
private static readonly SortedDictionary<string, Action> always_actions = new SortedDictionary<string, Action>();
private static readonly List<Action> always_actions_order = new List<Action>();
private static bool always_actions_invaild = false;
private static readonly List<Exception> exceptions = new List<Exception>();
private static bool initialized = false;
private static bool main_thread_init = false;
private static int main_thread = 0;
private static int now_threads = 0;
public static int MaxAsync = 8;
public static bool IsCurrent
{
get { return main_thread_init && main_thread == Thread.CurrentThread.ManagedThreadId; }
}
public static void Initialize()
{
if (!initialized)
{
if (!Application.isPlaying)
return;
initialized = true;
GameObject go = new GameObject("Loop");
UnityObject.DontDestroyOnLoad(go);
go.hideFlags |= HideFlags.HideInHierarchy;
go.AddComponent<Updater>();
}
}
public static void Execute(Action action)
{
if (main_thread_init && main_thread == Thread.CurrentThread.ManagedThreadId)
{
try
{
action();
}
catch (Exception e)
{
errfn(e);
}
}
else
{
lock (actions_mtx)
{
actions.Add(action);
}
}
}
public static void ExecuteAsync(Action action, Action complete)
{
lock (async_actions)
{
async_actions.Enqueue(new KeyValuePair<Action, Action>(action, complete));
}
while (true)
{
if (now_threads >= MaxAsync)
return;
int old_threads = now_threads;
if (Interlocked.CompareExchange(ref now_threads, old_threads + 1, old_threads) == old_threads)
break;
}
ThreadPool.QueueUserWorkItem(AsyncAction);
}
private static void AsyncAction(object o)
{
while (true)
{
KeyValuePair<Action, Action> action;
lock (async_actions)
{
if (async_actions.Count == 0)
break;
action = async_actions.Dequeue();
}
try
{
action.Key();
}
catch (Exception e)
{
lock (exceptions)
{
exceptions.Add(e);
}
}
Execute(action.Value);
}
Interlocked.Decrement(ref now_threads);
}
public static void Wait<T>(this T operation, Action action) where T : AsyncOperation
{
if (operation == null)
throw new ArgumentNullException("operation");
if (action == null)
throw new ArgumentNullException("action");
operations.Add(new KeyValuePair<AsyncOperation, Action<AsyncOperation>>(operation, AsyncWaiter.Acquire(action).Emit));
}
public static void Wait<T>(this T operation, Action<T> action) where T : AsyncOperation
{
if (operation == null)
throw new ArgumentNullException("operation");
if (action == null)
throw new ArgumentNullException("action");
operations.Add(new KeyValuePair<AsyncOperation, Action<AsyncOperation>>(operation, AsyncWaiter<T>.Acquire(action).Emit));
}
public static void Cancel(this AsyncOperation operation)
{
if (operation == null)
throw new ArgumentNullException("operation");
for (int i = 0; i < operations.Count; ++i)
{
var kv = operations[i];
if (kv.Key == operation)
operations[i] = new KeyValuePair<AsyncOperation, Action<AsyncOperation>>(null, null);
}
}
private class AsyncWaiter
{
public Action action;
public readonly Action<AsyncOperation> Emit;
public AsyncWaiter()
{
Emit = operation =>
{
action();
action = null;
stack.Push(this);
};
}
public static AsyncWaiter Acquire(Action action)
{
if (stack.Count == 0)
return new AsyncWaiter {action = action};
AsyncWaiter waiter = stack.Pop();
waiter.action = action;
return waiter;
}
private static readonly Stack<AsyncWaiter> stack = new Stack<AsyncWaiter>();
}
private class AsyncWaiter<T> where T : AsyncOperation
{
public Action<T> action;
public readonly Action<AsyncOperation> Emit;
public AsyncWaiter()
{
Emit = operation =>
{
T t = operation as T;
if (t != null)
action(t);
action = null;
stack.Push(this);
};
}
public static AsyncWaiter<T> Acquire(Action<T> action)
{
if (stack.Count == 0)
return new AsyncWaiter<T> {action = action};
AsyncWaiter<T> waiter = stack.Pop();
waiter.action = action;
return waiter;
}
private static readonly Stack<AsyncWaiter<T>> stack = new Stack<AsyncWaiter<T>>();
}
public static void RunAlways(string name, Action action)
{
if (action == null)
throw new ArgumentNullException("action");
always_actions_invaild = true;
always_actions[name] = action;
}
public static void RemoveAlways(string name)
{
always_actions_invaild = true;
always_actions.Remove(name);
}
public static void Idle(Action action)
{
if (action == null)
throw new ArgumentNullException("action");
idles.Add(action);
}
public static void Broadcast(string msg)
{
Broadcast(msg, _EmptyEvent);
}
public static void Broadcast(string msg, Event evt)
{
if (evt == null)
throw new ArgumentNullException("evt");
if (evt == _EmptyEvent)
evt = null;
Dictionary<Delegate, Action<string, Event>> actions;
if (events.TryGetValue(msg, out actions))
{
var enumerator = actions.GetEnumerator();
List<Action<string, Event>> list = events_action.Count == 0
? new List<Action<string, Event>>()
: events_action.Pop();
while (enumerator.MoveNext())
{
list.Add(enumerator.Current.Value);
}
enumerator.Dispose();
for (int i = 0; i < list.Count; ++i)
{
try
{
list[i](msg, evt);
}
catch (Exception e)
{
errfn(e);
}
}
list.Clear();
events_action.Push(list);
}
}
private static Dictionary<Delegate, Action<string, Event>> eventActions(string msg)
{
Dictionary<Delegate, Action<string, Event>> actions;
if (!events.TryGetValue(msg, out actions))
{
actions = new Dictionary<Delegate, Action<string, Event>>();
events.Add(msg, actions);
}
return actions;
}
public static void Subscribe(string msg, Action action)
{
eventActions(msg).Add(action, (str, evt) => action());
}
public static void Subscribe(string msg, Action<string> action)
{
eventActions(msg).Add(action, (str, evt) => action(str));
}
public static void Subscribe<T>(string msg, Action<T> action) where T : class, Event
{
eventActions(msg).Add(action, (str, evt) =>
{
T t = evt as T;
if (t != null)
action(t);
});
}
public static void Subscribe<T>(string msg, Action<string, T> action) where T : class, Event
{
eventActions(msg).Add(action, (str, evt) =>
{
T t = evt as T;
if (t != null)
action(str, t);
});
}
public static void Unsubscribe(string msg, Delegate action)
{
Dictionary<Delegate, Action<string, Event>> actions;
if (events.TryGetValue(msg, out actions))
{
if (actions.Remove(action) && actions.Count == 0)
events.Remove(msg);
}
}
public static void Error(Action<Exception> fn)
{
if (fn == null)
{
errfn = e => { };
}
else
{
errfn = e =>
{
try
{
fn(e);
}
catch (Exception)
{
}
};
}
}
private class Updater : MonoBehaviour
{
void Start()
{
main_thread = Thread.CurrentThread.ManagedThreadId;
Thread.MemoryBarrier();
main_thread_init = true;
}
void Update()
{
Clock.Update();
#region 异步操作检测
if (operations.Count > 0)
{
for (int i = 0, j = operations.Count; i < j; ++i)
{
var kv = operations[i];
if (kv.Key != null && !kv.Key.isDone)
operations_tmp.Add(kv);
else if (kv.Value != null)
operations_action.Add(kv);
}
operations.Clear();
var tmp = operations;
operations = operations_tmp;
operations_tmp = tmp;
for (int i = 0, j = operations_action.Count; i < j; ++i)
{
var kv = operations_action[i];
try
{
kv.Value(kv.Key);
}
catch (Exception e)
{
errfn(e);
}
}
operations_action.Clear();
}
#endregion
#region 异步调用异常处理
if (exceptions.Count > 0)
{
lock (exceptions)
{
for (int i = 0, j = exceptions.Count; i < j; ++i)
errfn(exceptions[i]);
exceptions.Clear();
}
}
#endregion
#region 跨线程执行请求
if (actions.Count > 0)
{
lock (actions_mtx)
{
var tmp = actions_tmp;
actions_tmp = actions;
actions = tmp;
}
for (int i = 0, j = actions_tmp.Count; i < j; ++i)
{
try
{
actions_tmp[i]();
}
catch (Exception e)
{
errfn(e);
}
}
actions_tmp.Clear();
}
#endregion
#region 每帧更新调用
if (always_actions_invaild)
{
always_actions_invaild = false;
always_actions_order.Clear();
foreach (var action in always_actions)
{
always_actions_order.Add(action.Value);
}
}
for (int i = 0, j = always_actions_order.Count; i < j; ++i)
{
try
{
always_actions_order[i]();
}
catch (Exception e)
{
errfn(e);
}
}
#endregion
// 计时器调度
Clock.Update(errfn);
#region 帧尾调用
if (idles.Count > 0)
{
var tmp = idles_tmp;
idles_tmp = idles;
idles = tmp;
for (int i = 0; i < idles_tmp.Count; ++i)
{
try
{
idles_tmp[i]();
}
catch (Exception e)
{
errfn(e);
}
}
idles_tmp.Clear();
}
#endregion
}
void OnApplicationQuit()
{
NetManager.ExitAll();
}
}
}
}