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,8 +7,10 @@ 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)
@ -17,20 +19,24 @@ namespace Coroutine {
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) {
Start(InvokeLaterImpl(wait, action)); Start(InvokeLaterImpl(wait, action));
} }
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)) {
@ -41,8 +47,10 @@ namespace 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)) {
@ -53,10 +61,13 @@ namespace 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) {
yield return wait; yield return wait;