Compare commits

...

3 commits

Author SHA1 Message Date
Ell 4f09e64039 update 2021-11-29 11:43:48 +01:00
Ell 8a3e55b8f4 Merge remote-tracking branch 'origin/main' into main 2021-11-29 11:37:39 +01:00
Ell 1deb8a9d89 ensure coroutine handler instance thread safety 2021-11-29 11:37:17 +01:00
3 changed files with 48 additions and 30 deletions

View file

@ -13,7 +13,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>Logo.png</PackageIcon>
<VersionPrefix>2.1.2</VersionPrefix>
<VersionPrefix>2.1.3</VersionPrefix>
</PropertyGroup>
<ItemGroup>

View file

@ -16,15 +16,26 @@ namespace Coroutine {
private readonly HashSet<ActiveCoroutine> outstandingEventCoroutines = new HashSet<ActiveCoroutine>();
private readonly HashSet<ActiveCoroutine> outstandingTickingCoroutines = new HashSet<ActiveCoroutine>();
private readonly Stopwatch stopwatch = new Stopwatch();
private readonly object lockObject = new object();
/// <summary>
/// The amount of <see cref="ActiveCoroutine"/> instances that are currently waiting for a tick (waiting for time to pass)
/// </summary>
public int TickingCount => this.tickingCoroutines.Count;
public int TickingCount {
get {
lock (this.lockObject)
return this.tickingCoroutines.Count;
}
}
/// <summary>
/// The amount of <see cref="ActiveCoroutine"/> instances that are currently waiting for an <see cref="Event"/>
/// </summary>
public int EventCount => this.eventCoroutines.Sum(c => c.Value.Count);
public int EventCount {
get {
lock (this.lockObject)
return this.eventCoroutines.Sum(c => c.Value.Count);
}
}
/// <summary>
/// Starts the given coroutine, returning a <see cref="ActiveCoroutine"/> object for management.
@ -47,8 +58,10 @@ namespace Coroutine {
/// <returns>An active coroutine object representing this coroutine</returns>
public ActiveCoroutine Start(IEnumerator<Wait> coroutine, string name = "", int priority = 0) {
var inst = new ActiveCoroutine(coroutine, name, priority, this.stopwatch);
if (inst.MoveNext())
if (inst.MoveNext()) {
lock (this.lockObject)
this.GetOutstandingCoroutines(inst.IsWaitingForEvent).Add(inst);
}
return inst;
}
@ -70,6 +83,7 @@ namespace Coroutine {
/// </summary>
/// <param name="deltaSeconds">The amount of seconds that have passed since the last time this method was invoked</param>
public void Tick(double deltaSeconds) {
lock (this.lockObject) {
this.MoveOutstandingCoroutines(false);
this.tickingCoroutines.RemoveAll(c => {
if (c.Tick(deltaSeconds)) {
@ -81,6 +95,7 @@ namespace Coroutine {
return false;
});
}
}
/// <summary>
/// Ticks this coroutine handler, causing all time-based <see cref="Wait"/>s to be ticked.
@ -96,6 +111,7 @@ namespace Coroutine {
/// </summary>
/// <param name="evt">The event to raise</param>
public void RaiseEvent(Event evt) {
lock (this.lockObject) {
this.MoveOutstandingCoroutines(true);
var coroutines = this.GetEventCoroutines(evt, false);
if (coroutines != null) {
@ -113,12 +129,14 @@ namespace Coroutine {
}
}
}
}
/// <summary>
/// Returns a list of all currently active <see cref="ActiveCoroutine"/> objects under this handler.
/// </summary>
/// <returns>All active coroutines</returns>
public IEnumerable<ActiveCoroutine> GetActiveCoroutines() {
lock (this.lockObject)
return this.tickingCoroutines.Concat(this.eventCoroutines.Values.SelectMany(c => c));
}

View file

@ -11,8 +11,8 @@ There are two predefined ways to pause a coroutine:
Additionally, Coroutine provides the following features:
- Creation of custom events to wait for
- Creation of custom wait conditions
- No multi-threading, which allows for any kind of process to be executed in a coroutine, including rendering
- Thread-safety, which allows for coroutines to be started from different threads
# How to Use
## Setting up the CoroutineHandler