Compare commits

..

No commits in common. "4f09e640393641bcb8fb27c601f96c7dd4ea23f5" and "6bbe37cbb38cb9ba9257e09a2c14e8575fb2491f" have entirely different histories.

3 changed files with 30 additions and 48 deletions

View file

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

View file

@ -16,26 +16,15 @@ namespace Coroutine {
private readonly HashSet<ActiveCoroutine> outstandingEventCoroutines = new HashSet<ActiveCoroutine>(); private readonly HashSet<ActiveCoroutine> outstandingEventCoroutines = new HashSet<ActiveCoroutine>();
private readonly HashSet<ActiveCoroutine> outstandingTickingCoroutines = new HashSet<ActiveCoroutine>(); private readonly HashSet<ActiveCoroutine> outstandingTickingCoroutines = new HashSet<ActiveCoroutine>();
private readonly Stopwatch stopwatch = new Stopwatch(); private readonly Stopwatch stopwatch = new Stopwatch();
private readonly object lockObject = new object();
/// <summary> /// <summary>
/// The amount of <see cref="ActiveCoroutine"/> instances that are currently waiting for a tick (waiting for time to pass) /// The amount of <see cref="ActiveCoroutine"/> instances that are currently waiting for a tick (waiting for time to pass)
/// </summary> /// </summary>
public int TickingCount { public int TickingCount => this.tickingCoroutines.Count;
get {
lock (this.lockObject)
return this.tickingCoroutines.Count;
}
}
/// <summary> /// <summary>
/// The amount of <see cref="ActiveCoroutine"/> instances that are currently waiting for an <see cref="Event"/> /// The amount of <see cref="ActiveCoroutine"/> instances that are currently waiting for an <see cref="Event"/>
/// </summary> /// </summary>
public int EventCount { public int EventCount => this.eventCoroutines.Sum(c => c.Value.Count);
get {
lock (this.lockObject)
return this.eventCoroutines.Sum(c => c.Value.Count);
}
}
/// <summary> /// <summary>
/// Starts the given coroutine, returning a <see cref="ActiveCoroutine"/> object for management. /// Starts the given coroutine, returning a <see cref="ActiveCoroutine"/> object for management.
@ -58,10 +47,8 @@ namespace Coroutine {
/// <returns>An active coroutine object representing this coroutine</returns> /// <returns>An active coroutine object representing this coroutine</returns>
public ActiveCoroutine Start(IEnumerator<Wait> coroutine, string name = "", int priority = 0) { public ActiveCoroutine Start(IEnumerator<Wait> coroutine, string name = "", int priority = 0) {
var inst = new ActiveCoroutine(coroutine, name, priority, this.stopwatch); var inst = new ActiveCoroutine(coroutine, name, priority, this.stopwatch);
if (inst.MoveNext()) { if (inst.MoveNext())
lock (this.lockObject) this.GetOutstandingCoroutines(inst.IsWaitingForEvent).Add(inst);
this.GetOutstandingCoroutines(inst.IsWaitingForEvent).Add(inst);
}
return inst; return inst;
} }
@ -83,18 +70,16 @@ namespace Coroutine {
/// </summary> /// </summary>
/// <param name="deltaSeconds">The amount of seconds that have passed since the last time this method was invoked</param> /// <param name="deltaSeconds">The amount of seconds that have passed since the last time this method was invoked</param>
public void Tick(double deltaSeconds) { public void Tick(double deltaSeconds) {
lock (this.lockObject) { this.MoveOutstandingCoroutines(false);
this.MoveOutstandingCoroutines(false); this.tickingCoroutines.RemoveAll(c => {
this.tickingCoroutines.RemoveAll(c => { if (c.Tick(deltaSeconds)) {
if (c.Tick(deltaSeconds)) { return true;
return true; } else if (c.IsWaitingForEvent) {
} else if (c.IsWaitingForEvent) { this.outstandingEventCoroutines.Add(c);
this.outstandingEventCoroutines.Add(c); return true;
return true; }
} return false;
return false; });
});
}
} }
/// <summary> /// <summary>
@ -111,21 +96,19 @@ namespace Coroutine {
/// </summary> /// </summary>
/// <param name="evt">The event to raise</param> /// <param name="evt">The event to raise</param>
public void RaiseEvent(Event evt) { public void RaiseEvent(Event evt) {
lock (this.lockObject) { this.MoveOutstandingCoroutines(true);
this.MoveOutstandingCoroutines(true); var coroutines = this.GetEventCoroutines(evt, false);
var coroutines = this.GetEventCoroutines(evt, false); if (coroutines != null) {
if (coroutines != null) { for (var i = 0; i < coroutines.Count; i++) {
for (var i = 0; i < coroutines.Count; i++) { var c = coroutines[i];
var c = coroutines[i]; var tup = (c.Event, c);
var tup = (c.Event, c); if (this.eventCoroutinesToRemove.Contains(tup))
if (this.eventCoroutinesToRemove.Contains(tup)) continue;
continue; if (c.OnEvent(evt)) {
if (c.OnEvent(evt)) { this.eventCoroutinesToRemove.Add(tup);
this.eventCoroutinesToRemove.Add(tup); } else if (!c.IsWaitingForEvent) {
} else if (!c.IsWaitingForEvent) { this.eventCoroutinesToRemove.Add(tup);
this.eventCoroutinesToRemove.Add(tup); this.outstandingTickingCoroutines.Add(c);
this.outstandingTickingCoroutines.Add(c);
}
} }
} }
} }
@ -136,8 +119,7 @@ namespace Coroutine {
/// </summary> /// </summary>
/// <returns>All active coroutines</returns> /// <returns>All active coroutines</returns>
public IEnumerable<ActiveCoroutine> GetActiveCoroutines() { public IEnumerable<ActiveCoroutine> GetActiveCoroutines() {
lock (this.lockObject) return this.tickingCoroutines.Concat(this.eventCoroutines.Values.SelectMany(c => c));
return this.tickingCoroutines.Concat(this.eventCoroutines.Values.SelectMany(c => c));
} }
private void MoveOutstandingCoroutines(bool evt) { private void MoveOutstandingCoroutines(bool evt) {

View file

@ -11,8 +11,8 @@ There are two predefined ways to pause a coroutine:
Additionally, Coroutine provides the following features: Additionally, Coroutine provides the following features:
- Creation of custom events to wait for - 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 - 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 # How to Use
## Setting up the CoroutineHandler ## Setting up the CoroutineHandler