synchronize coroutines to allow interaction from different threads

This commit is contained in:
Ellpeck 2020-03-26 01:35:37 +01:00
parent b850b3bae0
commit 708941ff01

View file

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