mirror of
https://github.com/Ellpeck/Coroutine.git
synced 2024-11-21 21:33:29 +01:00
synchronize coroutines to allow interaction from different threads
This commit is contained in:
parent
b850b3bae0
commit
708941ff01
1 changed files with 37 additions and 26 deletions
|
@ -7,23 +7,28 @@ namespace Coroutine {
|
|||
|
||||
private static readonly List<ActiveCoroutine> TickingCoroutines = 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) {
|
||||
var inst = new ActiveCoroutine(coroutine);
|
||||
var type = inst.GetCurrentType();
|
||||
if (type == WaitType.Tick)
|
||||
TickingCoroutines.Add(inst);
|
||||
else if (type == WaitType.Event)
|
||||
EventCoroutines.Add(inst);
|
||||
return inst;
|
||||
lock (Lock) {
|
||||
var inst = new ActiveCoroutine(coroutine);
|
||||
var type = inst.GetCurrentType();
|
||||
if (type == WaitType.Tick)
|
||||
TickingCoroutines.Add(inst);
|
||||
else if (type == WaitType.Event)
|
||||
EventCoroutines.Add(inst);
|
||||
return inst;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Stop(ActiveCoroutine coroutine) {
|
||||
if (TickingCoroutines.Remove(coroutine) || EventCoroutines.Remove(coroutine)) {
|
||||
coroutine.Finish(true);
|
||||
return true;
|
||||
lock (Lock) {
|
||||
if (TickingCoroutines.Remove(coroutine) || EventCoroutines.Remove(coroutine)) {
|
||||
coroutine.Finish(true);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void InvokeLater(IWait wait, Action action) {
|
||||
|
@ -31,31 +36,37 @@ namespace Coroutine {
|
|||
}
|
||||
|
||||
public static void Tick(double deltaSeconds) {
|
||||
for (var i = TickingCoroutines.Count - 1; i >= 0; i--) {
|
||||
var coroutine = TickingCoroutines[i];
|
||||
if (coroutine.Tick(deltaSeconds)) {
|
||||
TickingCoroutines.RemoveAt(i);
|
||||
} else if (coroutine.GetCurrentType() != WaitType.Tick) {
|
||||
TickingCoroutines.RemoveAt(i);
|
||||
EventCoroutines.Add(coroutine);
|
||||
lock (Lock) {
|
||||
for (var i = TickingCoroutines.Count - 1; i >= 0; i--) {
|
||||
var coroutine = TickingCoroutines[i];
|
||||
if (coroutine.Tick(deltaSeconds)) {
|
||||
TickingCoroutines.RemoveAt(i);
|
||||
} else if (coroutine.GetCurrentType() != WaitType.Tick) {
|
||||
TickingCoroutines.RemoveAt(i);
|
||||
EventCoroutines.Add(coroutine);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void RaiseEvent(Event evt) {
|
||||
for (var i = EventCoroutines.Count - 1; i >= 0; i--) {
|
||||
var coroutine = EventCoroutines[i];
|
||||
if (coroutine.OnEvent(evt)) {
|
||||
EventCoroutines.RemoveAt(i);
|
||||
} else if (coroutine.GetCurrentType() != WaitType.Event) {
|
||||
EventCoroutines.RemoveAt(i);
|
||||
TickingCoroutines.Add(coroutine);
|
||||
lock (Lock) {
|
||||
for (var i = EventCoroutines.Count - 1; i >= 0; i--) {
|
||||
var coroutine = EventCoroutines[i];
|
||||
if (coroutine.OnEvent(evt)) {
|
||||
EventCoroutines.RemoveAt(i);
|
||||
} else if (coroutine.GetCurrentType() != WaitType.Event) {
|
||||
EventCoroutines.RemoveAt(i);
|
||||
TickingCoroutines.Add(coroutine);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<ActiveCoroutine> GetActiveCoroutines() {
|
||||
return TickingCoroutines.Concat(EventCoroutines);
|
||||
lock (Lock) {
|
||||
return TickingCoroutines.Concat(EventCoroutines);
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerator<IWait> InvokeLaterImpl(IWait wait, Action action) {
|
||||
|
|
Loading…
Reference in a new issue