Revert "synchronize coroutines to allow interaction from different threads"

This reverts commit 708941ff
This commit is contained in:
Ellpeck 2020-03-26 01:48:02 +01:00
parent 708941ff01
commit 99aab497e5

View file

@ -7,28 +7,23 @@ namespace Coroutine {
private static readonly List<ActiveCoroutine> TickingCoroutines = new List<ActiveCoroutine>(); private static readonly List<ActiveCoroutine> TickingCoroutines = new List<ActiveCoroutine>();
private static readonly List<ActiveCoroutine> EventCoroutines = new List<ActiveCoroutine>(); private static readonly List<ActiveCoroutine> EventCoroutines = new List<ActiveCoroutine>();
private static readonly object Lock = new object();
public static ActiveCoroutine Start(IEnumerator<IWait> coroutine) { public static ActiveCoroutine Start(IEnumerator<IWait> coroutine) {
lock (Lock) { var inst = new ActiveCoroutine(coroutine);
var inst = new ActiveCoroutine(coroutine); var type = inst.GetCurrentType();
var type = inst.GetCurrentType(); if (type == WaitType.Tick)
if (type == WaitType.Tick) TickingCoroutines.Add(inst);
TickingCoroutines.Add(inst); else if (type == WaitType.Event)
else if (type == WaitType.Event) EventCoroutines.Add(inst);
EventCoroutines.Add(inst); return inst;
return inst;
}
} }
public static bool Stop(ActiveCoroutine coroutine) { public static bool Stop(ActiveCoroutine coroutine) {
lock (Lock) { if (TickingCoroutines.Remove(coroutine) || EventCoroutines.Remove(coroutine)) {
if (TickingCoroutines.Remove(coroutine) || EventCoroutines.Remove(coroutine)) { coroutine.Finish(true);
coroutine.Finish(true); return true;
return true;
}
return false;
} }
return false;
} }
public static void InvokeLater(IWait wait, Action action) { public static void InvokeLater(IWait wait, Action action) {
@ -36,37 +31,31 @@ namespace Coroutine {
} }
public static void Tick(double deltaSeconds) { public static void Tick(double deltaSeconds) {
lock (Lock) { for (var i = TickingCoroutines.Count - 1; i >= 0; i--) {
for (var i = TickingCoroutines.Count - 1; i >= 0; i--) { var coroutine = TickingCoroutines[i];
var coroutine = TickingCoroutines[i]; if (coroutine.Tick(deltaSeconds)) {
if (coroutine.Tick(deltaSeconds)) { TickingCoroutines.RemoveAt(i);
TickingCoroutines.RemoveAt(i); } else if (coroutine.GetCurrentType() != WaitType.Tick) {
} else if (coroutine.GetCurrentType() != WaitType.Tick) { TickingCoroutines.RemoveAt(i);
TickingCoroutines.RemoveAt(i); EventCoroutines.Add(coroutine);
EventCoroutines.Add(coroutine);
}
} }
} }
} }
public static void RaiseEvent(Event evt) { public static void RaiseEvent(Event evt) {
lock (Lock) { for (var i = EventCoroutines.Count - 1; i >= 0; i--) {
for (var i = EventCoroutines.Count - 1; i >= 0; i--) { var coroutine = EventCoroutines[i];
var coroutine = EventCoroutines[i]; if (coroutine.OnEvent(evt)) {
if (coroutine.OnEvent(evt)) { EventCoroutines.RemoveAt(i);
EventCoroutines.RemoveAt(i); } else if (coroutine.GetCurrentType() != WaitType.Event) {
} else if (coroutine.GetCurrentType() != WaitType.Event) { EventCoroutines.RemoveAt(i);
EventCoroutines.RemoveAt(i); TickingCoroutines.Add(coroutine);
TickingCoroutines.Add(coroutine);
}
} }
} }
} }
public static IEnumerable<ActiveCoroutine> GetActiveCoroutines() { public static IEnumerable<ActiveCoroutine> GetActiveCoroutines() {
lock (Lock) { return TickingCoroutines.Concat(EventCoroutines);
return TickingCoroutines.Concat(EventCoroutines);
}
} }
private static IEnumerator<IWait> InvokeLaterImpl(IWait wait, Action action) { private static IEnumerator<IWait> InvokeLaterImpl(IWait wait, Action action) {