From 3f042c8886c0e2985d51e053fbc3ebeb009bb974 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Thu, 26 Oct 2023 18:11:25 +0900 Subject: [PATCH 01/13] Add cancel immediate flag --- .../Linq/UnityExtensions/EveryUpdate.cs | 25 ++- .../Linq/UnityExtensions/EveryValueChanged.cs | 49 ++++-- .../Runtime/Linq/UnityExtensions/Timer.cs | 68 ++++--- .../Plugins/UniTask/Runtime/UniTask.Delay.cs | 166 +++++++++++++----- .../UniTask/Runtime/UniTask.WaitUntil.cs | 83 +++++++-- ...cExtensions.AssetBundleRequestAllAssets.cs | 23 ++- .../UnityAsyncExtensions.AsyncGPUReadback.cs | 23 ++- .../UniTask/Runtime/UnityAsyncExtensions.cs | 48 ++++- 8 files changed, 375 insertions(+), 110 deletions(-) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/EveryUpdate.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/EveryUpdate.cs index 585fff96..09c5cf90 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/EveryUpdate.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/EveryUpdate.cs @@ -4,38 +4,50 @@ namespace Cysharp.Threading.Tasks.Linq { public static partial class UniTaskAsyncEnumerable { - public static IUniTaskAsyncEnumerable EveryUpdate(PlayerLoopTiming updateTiming = PlayerLoopTiming.Update) + public static IUniTaskAsyncEnumerable EveryUpdate(PlayerLoopTiming updateTiming = PlayerLoopTiming.Update, bool cancelImmediately = false) { - return new EveryUpdate(updateTiming); + return new EveryUpdate(updateTiming, cancelImmediately); } } internal class EveryUpdate : IUniTaskAsyncEnumerable { readonly PlayerLoopTiming updateTiming; + readonly bool cancelImmediately; - public EveryUpdate(PlayerLoopTiming updateTiming) + public EveryUpdate(PlayerLoopTiming updateTiming, bool cancelImmediately) { this.updateTiming = updateTiming; + this.cancelImmediately = cancelImmediately; } public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) { - return new _EveryUpdate(updateTiming, cancellationToken); + return new _EveryUpdate(updateTiming, cancellationToken, cancelImmediately); } class _EveryUpdate : MoveNextSource, IUniTaskAsyncEnumerator, IPlayerLoopItem { readonly PlayerLoopTiming updateTiming; - CancellationToken cancellationToken; + readonly CancellationToken cancellationToken; + readonly CancellationTokenRegistration cancellationTokenRegistration; bool disposed; - public _EveryUpdate(PlayerLoopTiming updateTiming, CancellationToken cancellationToken) + public _EveryUpdate(PlayerLoopTiming updateTiming, CancellationToken cancellationToken, bool cancelImmediately) { this.updateTiming = updateTiming; this.cancellationToken = cancellationToken; + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (_EveryUpdate)state; + source.completionSource.TrySetCanceled(source.cancellationToken); + }, this); + } + TaskTracker.TrackActiveTask(this, 2); PlayerLoopHelper.AddAction(updateTiming, this); } @@ -55,6 +67,7 @@ public UniTask DisposeAsync() { if (!disposed) { + cancellationTokenRegistration.Dispose(); disposed = true; TaskTracker.RemoveTracking(this); } diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/EveryValueChanged.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/EveryValueChanged.cs index f678e7aa..8b19f647 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/EveryValueChanged.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/EveryValueChanged.cs @@ -7,7 +7,7 @@ namespace Cysharp.Threading.Tasks.Linq { public static partial class UniTaskAsyncEnumerable { - public static IUniTaskAsyncEnumerable EveryValueChanged(TTarget target, Func propertySelector, PlayerLoopTiming monitorTiming = PlayerLoopTiming.Update, IEqualityComparer equalityComparer = null) + public static IUniTaskAsyncEnumerable EveryValueChanged(TTarget target, Func propertySelector, PlayerLoopTiming monitorTiming = PlayerLoopTiming.Update, IEqualityComparer equalityComparer = null, bool cancelImmediately = false) where TTarget : class { var unityObject = target as UnityEngine.Object; @@ -15,11 +15,11 @@ public static IUniTaskAsyncEnumerable EveryValueChanged(target, propertySelector, equalityComparer ?? UnityEqualityComparer.GetDefault(), monitorTiming); + return new EveryValueChangedUnityObject(target, propertySelector, equalityComparer ?? UnityEqualityComparer.GetDefault(), monitorTiming, cancelImmediately); } else { - return new EveryValueChangedStandardObject(target, propertySelector, equalityComparer ?? UnityEqualityComparer.GetDefault(), monitorTiming); + return new EveryValueChangedStandardObject(target, propertySelector, equalityComparer ?? UnityEqualityComparer.GetDefault(), monitorTiming, cancelImmediately); } } } @@ -30,18 +30,20 @@ internal sealed class EveryValueChangedUnityObject : IUniTas readonly Func propertySelector; readonly IEqualityComparer equalityComparer; readonly PlayerLoopTiming monitorTiming; + readonly bool cancelImmediately; - public EveryValueChangedUnityObject(TTarget target, Func propertySelector, IEqualityComparer equalityComparer, PlayerLoopTiming monitorTiming) + public EveryValueChangedUnityObject(TTarget target, Func propertySelector, IEqualityComparer equalityComparer, PlayerLoopTiming monitorTiming, bool cancelImmediately) { this.target = target; this.propertySelector = propertySelector; this.equalityComparer = equalityComparer; this.monitorTiming = monitorTiming; + this.cancelImmediately = cancelImmediately; } public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) { - return new _EveryValueChanged(target, propertySelector, equalityComparer, monitorTiming, cancellationToken); + return new _EveryValueChanged(target, propertySelector, equalityComparer, monitorTiming, cancellationToken, cancelImmediately); } sealed class _EveryValueChanged : MoveNextSource, IUniTaskAsyncEnumerator, IPlayerLoopItem @@ -50,13 +52,14 @@ sealed class _EveryValueChanged : MoveNextSource, IUniTaskAsyncEnumerator equalityComparer; readonly Func propertySelector; - CancellationToken cancellationToken; + readonly CancellationToken cancellationToken; + readonly CancellationTokenRegistration cancellationTokenRegistration; bool first; TProperty currentValue; bool disposed; - public _EveryValueChanged(TTarget target, Func propertySelector, IEqualityComparer equalityComparer, PlayerLoopTiming monitorTiming, CancellationToken cancellationToken) + public _EveryValueChanged(TTarget target, Func propertySelector, IEqualityComparer equalityComparer, PlayerLoopTiming monitorTiming, CancellationToken cancellationToken, bool cancelImmediately) { this.target = target; this.targetAsUnityObject = target as UnityEngine.Object; @@ -64,6 +67,16 @@ public _EveryValueChanged(TTarget target, Func propertySelec this.equalityComparer = equalityComparer; this.cancellationToken = cancellationToken; this.first = true; + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (_EveryValueChanged)state; + source.completionSource.TrySetCanceled(source.cancellationToken); + }, this); + } + TaskTracker.TrackActiveTask(this, 2); PlayerLoopHelper.AddAction(monitorTiming, this); } @@ -139,18 +152,20 @@ internal sealed class EveryValueChangedStandardObject : IUni readonly Func propertySelector; readonly IEqualityComparer equalityComparer; readonly PlayerLoopTiming monitorTiming; + readonly bool cancelImmediately; - public EveryValueChangedStandardObject(TTarget target, Func propertySelector, IEqualityComparer equalityComparer, PlayerLoopTiming monitorTiming) + public EveryValueChangedStandardObject(TTarget target, Func propertySelector, IEqualityComparer equalityComparer, PlayerLoopTiming monitorTiming, bool cancelImmediately) { this.target = new WeakReference(target, false); this.propertySelector = propertySelector; this.equalityComparer = equalityComparer; this.monitorTiming = monitorTiming; + this.cancelImmediately = cancelImmediately; } public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) { - return new _EveryValueChanged(target, propertySelector, equalityComparer, monitorTiming, cancellationToken); + return new _EveryValueChanged(target, propertySelector, equalityComparer, monitorTiming, cancellationToken, cancelImmediately); } sealed class _EveryValueChanged : MoveNextSource, IUniTaskAsyncEnumerator, IPlayerLoopItem @@ -158,19 +173,30 @@ sealed class _EveryValueChanged : MoveNextSource, IUniTaskAsyncEnumerator target; readonly IEqualityComparer equalityComparer; readonly Func propertySelector; - CancellationToken cancellationToken; + readonly CancellationToken cancellationToken; + readonly CancellationTokenRegistration cancellationTokenRegistration; bool first; TProperty currentValue; bool disposed; - public _EveryValueChanged(WeakReference target, Func propertySelector, IEqualityComparer equalityComparer, PlayerLoopTiming monitorTiming, CancellationToken cancellationToken) + public _EveryValueChanged(WeakReference target, Func propertySelector, IEqualityComparer equalityComparer, PlayerLoopTiming monitorTiming, CancellationToken cancellationToken, bool cancelImmediately) { this.target = target; this.propertySelector = propertySelector; this.equalityComparer = equalityComparer; this.cancellationToken = cancellationToken; this.first = true; + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (_EveryValueChanged)state; + source.completionSource.TrySetCanceled(source.cancellationToken); + }, this); + } + TaskTracker.TrackActiveTask(this, 2); PlayerLoopHelper.AddAction(monitorTiming, this); } @@ -200,6 +226,7 @@ public UniTask DisposeAsync() { if (!disposed) { + cancellationTokenRegistration.Dispose(); disposed = true; TaskTracker.RemoveTracking(this); } diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/Timer.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/Timer.cs index 53ecfcd7..a3bab3c5 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/Timer.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/Timer.cs @@ -6,32 +6,32 @@ namespace Cysharp.Threading.Tasks.Linq { public static partial class UniTaskAsyncEnumerable { - public static IUniTaskAsyncEnumerable Timer(TimeSpan dueTime, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update, bool ignoreTimeScale = false) + public static IUniTaskAsyncEnumerable Timer(TimeSpan dueTime, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update, bool ignoreTimeScale = false, bool cancelImmediately = false) { - return new Timer(dueTime, null, updateTiming, ignoreTimeScale); + return new Timer(dueTime, null, updateTiming, ignoreTimeScale, cancelImmediately); } - public static IUniTaskAsyncEnumerable Timer(TimeSpan dueTime, TimeSpan period, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update, bool ignoreTimeScale = false) + public static IUniTaskAsyncEnumerable Timer(TimeSpan dueTime, TimeSpan period, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update, bool ignoreTimeScale = false, bool cancelImmediately = false) { - return new Timer(dueTime, period, updateTiming, ignoreTimeScale); + return new Timer(dueTime, period, updateTiming, ignoreTimeScale, cancelImmediately); } - public static IUniTaskAsyncEnumerable Interval(TimeSpan period, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update, bool ignoreTimeScale = false) + public static IUniTaskAsyncEnumerable Interval(TimeSpan period, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update, bool ignoreTimeScale = false, bool cancelImmediately = false) { - return new Timer(period, period, updateTiming, ignoreTimeScale); + return new Timer(period, period, updateTiming, ignoreTimeScale, cancelImmediately); } - public static IUniTaskAsyncEnumerable TimerFrame(int dueTimeFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update) + public static IUniTaskAsyncEnumerable TimerFrame(int dueTimeFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update, bool cancelImmediately = false) { if (dueTimeFrameCount < 0) { throw new ArgumentOutOfRangeException("Delay does not allow minus delayFrameCount. dueTimeFrameCount:" + dueTimeFrameCount); } - return new TimerFrame(dueTimeFrameCount, null, updateTiming); + return new TimerFrame(dueTimeFrameCount, null, updateTiming, cancelImmediately); } - public static IUniTaskAsyncEnumerable TimerFrame(int dueTimeFrameCount, int periodFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update) + public static IUniTaskAsyncEnumerable TimerFrame(int dueTimeFrameCount, int periodFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update, bool cancelImmediately = false) { if (dueTimeFrameCount < 0) { @@ -42,16 +42,16 @@ public static IUniTaskAsyncEnumerable TimerFrame(int dueTimeFrameCoun throw new ArgumentOutOfRangeException("Delay does not allow minus periodFrameCount. periodFrameCount:" + dueTimeFrameCount); } - return new TimerFrame(dueTimeFrameCount, periodFrameCount, updateTiming); + return new TimerFrame(dueTimeFrameCount, periodFrameCount, updateTiming, cancelImmediately); } - public static IUniTaskAsyncEnumerable IntervalFrame(int intervalFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update) + public static IUniTaskAsyncEnumerable IntervalFrame(int intervalFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update, bool cancelImmediately = false) { if (intervalFrameCount < 0) { throw new ArgumentOutOfRangeException("Delay does not allow minus intervalFrameCount. intervalFrameCount:" + intervalFrameCount); } - return new TimerFrame(intervalFrameCount, intervalFrameCount, updateTiming); + return new TimerFrame(intervalFrameCount, intervalFrameCount, updateTiming, cancelImmediately); } } @@ -61,18 +61,20 @@ internal class Timer : IUniTaskAsyncEnumerable readonly TimeSpan dueTime; readonly TimeSpan? period; readonly bool ignoreTimeScale; + readonly bool cancelImmediately; - public Timer(TimeSpan dueTime, TimeSpan? period, PlayerLoopTiming updateTiming, bool ignoreTimeScale) + public Timer(TimeSpan dueTime, TimeSpan? period, PlayerLoopTiming updateTiming, bool ignoreTimeScale, bool cancelImmediately) { this.updateTiming = updateTiming; this.dueTime = dueTime; this.period = period; this.ignoreTimeScale = ignoreTimeScale; + this.cancelImmediately = cancelImmediately; } public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) { - return new _Timer(dueTime, period, updateTiming, ignoreTimeScale, cancellationToken); + return new _Timer(dueTime, period, updateTiming, ignoreTimeScale, cancellationToken, cancelImmediately); } class _Timer : MoveNextSource, IUniTaskAsyncEnumerator, IPlayerLoopItem @@ -81,7 +83,8 @@ class _Timer : MoveNextSource, IUniTaskAsyncEnumerator, IPlayerLoopIt readonly float? period; readonly PlayerLoopTiming updateTiming; readonly bool ignoreTimeScale; - CancellationToken cancellationToken; + readonly CancellationToken cancellationToken; + readonly CancellationTokenRegistration cancellationTokenRegistration; int initialFrame; float elapsed; @@ -89,7 +92,7 @@ class _Timer : MoveNextSource, IUniTaskAsyncEnumerator, IPlayerLoopIt bool completed; bool disposed; - public _Timer(TimeSpan dueTime, TimeSpan? period, PlayerLoopTiming updateTiming, bool ignoreTimeScale, CancellationToken cancellationToken) + public _Timer(TimeSpan dueTime, TimeSpan? period, PlayerLoopTiming updateTiming, bool ignoreTimeScale, CancellationToken cancellationToken, bool cancelImmediately) { this.dueTime = (float)dueTime.TotalSeconds; this.period = (period == null) ? null : (float?)period.Value.TotalSeconds; @@ -105,6 +108,17 @@ public _Timer(TimeSpan dueTime, TimeSpan? period, PlayerLoopTiming updateTiming, this.updateTiming = updateTiming; this.ignoreTimeScale = ignoreTimeScale; this.cancellationToken = cancellationToken; + + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (_Timer)state; + source.completionSource.TrySetCanceled(source.cancellationToken); + }, this); + } + TaskTracker.TrackActiveTask(this, 2); PlayerLoopHelper.AddAction(updateTiming, this); } @@ -127,6 +141,7 @@ public UniTask DisposeAsync() { if (!disposed) { + cancellationTokenRegistration.Dispose(); disposed = true; TaskTracker.RemoveTracking(this); } @@ -187,24 +202,27 @@ internal class TimerFrame : IUniTaskAsyncEnumerable readonly PlayerLoopTiming updateTiming; readonly int dueTimeFrameCount; readonly int? periodFrameCount; + readonly bool cancelImmediately; - public TimerFrame(int dueTimeFrameCount, int? periodFrameCount, PlayerLoopTiming updateTiming) + public TimerFrame(int dueTimeFrameCount, int? periodFrameCount, PlayerLoopTiming updateTiming, bool cancelImmediately) { this.updateTiming = updateTiming; this.dueTimeFrameCount = dueTimeFrameCount; this.periodFrameCount = periodFrameCount; + this.cancelImmediately = cancelImmediately; } public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) { - return new _TimerFrame(dueTimeFrameCount, periodFrameCount, updateTiming, cancellationToken); + return new _TimerFrame(dueTimeFrameCount, periodFrameCount, updateTiming, cancellationToken, cancelImmediately); } class _TimerFrame : MoveNextSource, IUniTaskAsyncEnumerator, IPlayerLoopItem { readonly int dueTimeFrameCount; readonly int? periodFrameCount; - CancellationToken cancellationToken; + readonly CancellationToken cancellationToken; + readonly CancellationTokenRegistration cancellationTokenRegistration; int initialFrame; int currentFrame; @@ -212,7 +230,7 @@ class _TimerFrame : MoveNextSource, IUniTaskAsyncEnumerator, IPlayerL bool completed; bool disposed; - public _TimerFrame(int dueTimeFrameCount, int? periodFrameCount, PlayerLoopTiming updateTiming, CancellationToken cancellationToken) + public _TimerFrame(int dueTimeFrameCount, int? periodFrameCount, PlayerLoopTiming updateTiming, CancellationToken cancellationToken, bool cancelImmediately) { if (dueTimeFrameCount <= 0) dueTimeFrameCount = 0; if (periodFrameCount != null) @@ -225,6 +243,15 @@ public _TimerFrame(int dueTimeFrameCount, int? periodFrameCount, PlayerLoopTimin this.dueTimeFrameCount = dueTimeFrameCount; this.periodFrameCount = periodFrameCount; this.cancellationToken = cancellationToken; + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (_TimerFrame)state; + source.completionSource.TrySetCanceled(source.cancellationToken); + }, this); + } TaskTracker.TrackActiveTask(this, 2); PlayerLoopHelper.AddAction(updateTiming, this); @@ -249,6 +276,7 @@ public UniTask DisposeAsync() { if (!disposed) { + cancellationTokenRegistration.Dispose(); disposed = true; TaskTracker.RemoveTracking(this); } diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Delay.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Delay.cs index fadd63cb..7f02a1a1 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Delay.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Delay.cs @@ -33,14 +33,14 @@ public static YieldAwaitable Yield(PlayerLoopTiming timing) return new YieldAwaitable(timing); } - public static UniTask Yield(CancellationToken cancellationToken) + public static UniTask Yield(CancellationToken cancellationToken, bool cancelImmediately = false) { - return new UniTask(YieldPromise.Create(PlayerLoopTiming.Update, cancellationToken, out var token), token); + return new UniTask(YieldPromise.Create(PlayerLoopTiming.Update, cancellationToken, cancelImmediately, out var token), token); } - public static UniTask Yield(PlayerLoopTiming timing, CancellationToken cancellationToken) + public static UniTask Yield(PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately = false) { - return new UniTask(YieldPromise.Create(timing, cancellationToken, out var token), token); + return new UniTask(YieldPromise.Create(timing, cancellationToken, cancelImmediately, out var token), token); } /// @@ -48,7 +48,7 @@ public static UniTask Yield(PlayerLoopTiming timing, CancellationToken cancellat /// public static UniTask NextFrame() { - return new UniTask(NextFramePromise.Create(PlayerLoopTiming.Update, CancellationToken.None, out var token), token); + return new UniTask(NextFramePromise.Create(PlayerLoopTiming.Update, CancellationToken.None, false, out var token), token); } /// @@ -56,23 +56,23 @@ public static UniTask NextFrame() /// public static UniTask NextFrame(PlayerLoopTiming timing) { - return new UniTask(NextFramePromise.Create(timing, CancellationToken.None, out var token), token); + return new UniTask(NextFramePromise.Create(timing, CancellationToken.None, false, out var token), token); } /// /// Similar as UniTask.Yield but guaranteed run on next frame. /// - public static UniTask NextFrame(CancellationToken cancellationToken) + public static UniTask NextFrame(CancellationToken cancellationToken, bool cancelImmediately = false) { - return new UniTask(NextFramePromise.Create(PlayerLoopTiming.Update, cancellationToken, out var token), token); + return new UniTask(NextFramePromise.Create(PlayerLoopTiming.Update, cancellationToken, cancelImmediately, out var token), token); } /// /// Similar as UniTask.Yield but guaranteed run on next frame. /// - public static UniTask NextFrame(PlayerLoopTiming timing, CancellationToken cancellationToken) + public static UniTask NextFrame(PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately = false) { - return new UniTask(NextFramePromise.Create(timing, cancellationToken, out var token), token); + return new UniTask(NextFramePromise.Create(timing, cancellationToken, cancelImmediately, out var token), token); } #if UNITY_2023_1_OR_NEWER @@ -88,15 +88,21 @@ public static YieldAwaitable WaitForEndOfFrame() } [Obsolete("Use WaitForEndOfFrame(MonoBehaviour) instead or UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate). Equivalent for coroutine's WaitForEndOfFrame requires MonoBehaviour(runner of Coroutine).")] - public static UniTask WaitForEndOfFrame(CancellationToken cancellationToken) + public static UniTask WaitForEndOfFrame(CancellationToken cancellationToken, bool cancelImmediately = false) { - return UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate, cancellationToken); + return UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate, cancellationToken, cancelImmediately); } #endif - public static UniTask WaitForEndOfFrame(MonoBehaviour coroutineRunner, CancellationToken cancellationToken = default) + public static UniTask WaitForEndOfFrame(MonoBehaviour coroutineRunner) { - var source = WaitForEndOfFramePromise.Create(coroutineRunner, cancellationToken, out var token); + var source = WaitForEndOfFramePromise.Create(coroutineRunner, CancellationToken.None, false, out var token); + return new UniTask(source, token); + } + + public static UniTask WaitForEndOfFrame(MonoBehaviour coroutineRunner, CancellationToken cancellationToken, bool cancelImmediately = false) + { + var source = WaitForEndOfFramePromise.Create(coroutineRunner, cancellationToken, cancelImmediately, out var token); return new UniTask(source, token); } @@ -113,50 +119,50 @@ public static YieldAwaitable WaitForFixedUpdate() /// /// Same as UniTask.Yield(PlayerLoopTiming.LastFixedUpdate, cancellationToken). /// - public static UniTask WaitForFixedUpdate(CancellationToken cancellationToken) + public static UniTask WaitForFixedUpdate(CancellationToken cancellationToken, bool cancelImmediately = false) { - return UniTask.Yield(PlayerLoopTiming.LastFixedUpdate, cancellationToken); + return UniTask.Yield(PlayerLoopTiming.LastFixedUpdate, cancellationToken, cancelImmediately); } - public static UniTask WaitForSeconds(float duration, bool ignoreTimeScale = false, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask WaitForSeconds(float duration, bool ignoreTimeScale = false, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { - return Delay(Mathf.RoundToInt(1000 * duration), ignoreTimeScale, delayTiming, cancellationToken); + return Delay(Mathf.RoundToInt(1000 * duration), ignoreTimeScale, delayTiming, cancellationToken, cancelImmediately); } - public static UniTask WaitForSeconds(int duration, bool ignoreTimeScale = false, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask WaitForSeconds(int duration, bool ignoreTimeScale = false, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { - return Delay(1000 * duration, ignoreTimeScale, delayTiming, cancellationToken); + return Delay(1000 * duration, ignoreTimeScale, delayTiming, cancellationToken, cancelImmediately); } - public static UniTask DelayFrame(int delayFrameCount, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask DelayFrame(int delayFrameCount, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { if (delayFrameCount < 0) { throw new ArgumentOutOfRangeException("Delay does not allow minus delayFrameCount. delayFrameCount:" + delayFrameCount); } - return new UniTask(DelayFramePromise.Create(delayFrameCount, delayTiming, cancellationToken, out var token), token); + return new UniTask(DelayFramePromise.Create(delayFrameCount, delayTiming, cancellationToken, cancelImmediately, out var token), token); } - public static UniTask Delay(int millisecondsDelay, bool ignoreTimeScale = false, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask Delay(int millisecondsDelay, bool ignoreTimeScale = false, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { var delayTimeSpan = TimeSpan.FromMilliseconds(millisecondsDelay); - return Delay(delayTimeSpan, ignoreTimeScale, delayTiming, cancellationToken); + return Delay(delayTimeSpan, ignoreTimeScale, delayTiming, cancellationToken, cancelImmediately); } - public static UniTask Delay(TimeSpan delayTimeSpan, bool ignoreTimeScale = false, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask Delay(TimeSpan delayTimeSpan, bool ignoreTimeScale = false, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { var delayType = ignoreTimeScale ? DelayType.UnscaledDeltaTime : DelayType.DeltaTime; - return Delay(delayTimeSpan, delayType, delayTiming, cancellationToken); + return Delay(delayTimeSpan, delayType, delayTiming, cancellationToken, cancelImmediately); } - public static UniTask Delay(int millisecondsDelay, DelayType delayType, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask Delay(int millisecondsDelay, DelayType delayType, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { var delayTimeSpan = TimeSpan.FromMilliseconds(millisecondsDelay); - return Delay(delayTimeSpan, delayType, delayTiming, cancellationToken); + return Delay(delayTimeSpan, delayType, delayTiming, cancellationToken, cancelImmediately); } - public static UniTask Delay(TimeSpan delayTimeSpan, DelayType delayType, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask Delay(TimeSpan delayTimeSpan, DelayType delayType, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { if (delayTimeSpan < TimeSpan.Zero) { @@ -175,16 +181,16 @@ public static UniTask WaitForFixedUpdate(CancellationToken cancellationToken) { case DelayType.UnscaledDeltaTime: { - return new UniTask(DelayIgnoreTimeScalePromise.Create(delayTimeSpan, delayTiming, cancellationToken, out var token), token); + return new UniTask(DelayIgnoreTimeScalePromise.Create(delayTimeSpan, delayTiming, cancellationToken, cancelImmediately, out var token), token); } case DelayType.Realtime: { - return new UniTask(DelayRealtimePromise.Create(delayTimeSpan, delayTiming, cancellationToken, out var token), token); + return new UniTask(DelayRealtimePromise.Create(delayTimeSpan, delayTiming, cancellationToken, cancelImmediately, out var token), token); } case DelayType.DeltaTime: default: { - return new UniTask(DelayPromise.Create(delayTimeSpan, delayTiming, cancellationToken, out var token), token); + return new UniTask(DelayPromise.Create(delayTimeSpan, delayTiming, cancellationToken, cancelImmediately, out var token), token); } } } @@ -201,13 +207,14 @@ static YieldPromise() } CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; YieldPromise() { } - public static IUniTaskSource Create(PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -219,8 +226,16 @@ public static IUniTaskSource Create(PlayerLoopTiming timing, CancellationToken c result = new YieldPromise(); } - result.cancellationToken = cancellationToken; + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (YieldPromise)state; + promise.core.TrySetCanceled(promise.cancellationToken); + }, result); + } TaskTracker.TrackActiveTask(result, 3); @@ -274,6 +289,7 @@ bool TryReturn() TaskTracker.RemoveTracking(this); core.Reset(); cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } @@ -290,14 +306,15 @@ static NextFramePromise() } int frameCount; - CancellationToken cancellationToken; UniTaskCompletionSourceCore core; + CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; NextFramePromise() { } - public static IUniTaskSource Create(PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -312,6 +329,15 @@ public static IUniTaskSource Create(PlayerLoopTiming timing, CancellationToken c result.frameCount = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1; result.cancellationToken = cancellationToken; + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (NextFramePromise)state; + promise.core.TrySetCanceled(promise.cancellationToken); + }, result); + } + TaskTracker.TrackActiveTask(result, 3); PlayerLoopHelper.AddAction(timing, result); @@ -369,6 +395,7 @@ bool TryReturn() TaskTracker.RemoveTracking(this); core.Reset(); cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } @@ -384,14 +411,15 @@ static WaitForEndOfFramePromise() TaskPool.RegisterSizeGetter(typeof(WaitForEndOfFramePromise), () => pool.Size); } - CancellationToken cancellationToken; UniTaskCompletionSourceCore core; + CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; WaitForEndOfFramePromise() { } - public static IUniTaskSource Create(MonoBehaviour coroutineRunner, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(MonoBehaviour coroutineRunner, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -405,6 +433,15 @@ public static IUniTaskSource Create(MonoBehaviour coroutineRunner, CancellationT result.cancellationToken = cancellationToken; + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (WaitForEndOfFramePromise)state; + promise.core.TrySetCanceled(promise.cancellationToken); + }, result); + } + TaskTracker.TrackActiveTask(result, 3); coroutineRunner.StartCoroutine(result); @@ -446,6 +483,7 @@ bool TryReturn() core.Reset(); Reset(); // Reset Enumerator cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } @@ -494,6 +532,7 @@ static DelayFramePromise() int initialFrame; int delayFrameCount; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; int currentFrameCount; UniTaskCompletionSourceCore core; @@ -502,7 +541,7 @@ static DelayFramePromise() { } - public static IUniTaskSource Create(int delayFrameCount, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(int delayFrameCount, PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -518,6 +557,15 @@ public static IUniTaskSource Create(int delayFrameCount, PlayerLoopTiming timing result.cancellationToken = cancellationToken; result.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1; + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (DelayFramePromise)state; + promise.core.TrySetCanceled(promise.cancellationToken); + }, result); + } + TaskTracker.TrackActiveTask(result, 3); PlayerLoopHelper.AddAction(timing, result); @@ -604,6 +652,7 @@ bool TryReturn() currentFrameCount = default; delayFrameCount = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } @@ -623,6 +672,7 @@ static DelayPromise() float delayTimeSpan; float elapsed; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; @@ -630,7 +680,7 @@ static DelayPromise() { } - public static IUniTaskSource Create(TimeSpan delayTimeSpan, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(TimeSpan delayTimeSpan, PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -647,6 +697,15 @@ public static IUniTaskSource Create(TimeSpan delayTimeSpan, PlayerLoopTiming tim result.cancellationToken = cancellationToken; result.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1; + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (DelayPromise)state; + promise.core.TrySetCanceled(promise.cancellationToken); + }, result); + } + TaskTracker.TrackActiveTask(result, 3); PlayerLoopHelper.AddAction(timing, result); @@ -715,6 +774,7 @@ bool TryReturn() delayTimeSpan = default; elapsed = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } @@ -734,6 +794,7 @@ static DelayIgnoreTimeScalePromise() float elapsed; int initialFrame; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; @@ -741,7 +802,7 @@ static DelayIgnoreTimeScalePromise() { } - public static IUniTaskSource Create(TimeSpan delayFrameTimeSpan, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(TimeSpan delayFrameTimeSpan, PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -758,6 +819,15 @@ public static IUniTaskSource Create(TimeSpan delayFrameTimeSpan, PlayerLoopTimin result.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1; result.cancellationToken = cancellationToken; + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (DelayIgnoreTimeScalePromise)state; + promise.core.TrySetCanceled(promise.cancellationToken); + }, result); + } + TaskTracker.TrackActiveTask(result, 3); PlayerLoopHelper.AddAction(timing, result); @@ -826,6 +896,7 @@ bool TryReturn() delayFrameTimeSpan = default; elapsed = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } @@ -844,6 +915,7 @@ static DelayRealtimePromise() long delayTimeSpanTicks; ValueStopwatch stopwatch; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; @@ -851,7 +923,7 @@ static DelayRealtimePromise() { } - public static IUniTaskSource Create(TimeSpan delayTimeSpan, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(TimeSpan delayTimeSpan, PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -867,6 +939,15 @@ public static IUniTaskSource Create(TimeSpan delayTimeSpan, PlayerLoopTiming tim result.delayTimeSpanTicks = delayTimeSpan.Ticks; result.cancellationToken = cancellationToken; + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (DelayRealtimePromise)state; + promise.core.TrySetCanceled(promise.cancellationToken); + }, result); + } + TaskTracker.TrackActiveTask(result, 3); PlayerLoopHelper.AddAction(timing, result); @@ -931,6 +1012,7 @@ bool TryReturn() core.Reset(); stopwatch = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.WaitUntil.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.WaitUntil.cs index 0a09fe09..b28a529e 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.WaitUntil.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.WaitUntil.cs @@ -9,30 +9,30 @@ namespace Cysharp.Threading.Tasks { public partial struct UniTask { - public static UniTask WaitUntil(Func predicate, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask WaitUntil(Func predicate, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { - return new UniTask(WaitUntilPromise.Create(predicate, timing, cancellationToken, out var token), token); + return new UniTask(WaitUntilPromise.Create(predicate, timing, cancellationToken, cancelImmediately, out var token), token); } - public static UniTask WaitWhile(Func predicate, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask WaitWhile(Func predicate, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { - return new UniTask(WaitWhilePromise.Create(predicate, timing, cancellationToken, out var token), token); + return new UniTask(WaitWhilePromise.Create(predicate, timing, cancellationToken, cancelImmediately, out var token), token); } - public static UniTask WaitUntilCanceled(CancellationToken cancellationToken, PlayerLoopTiming timing = PlayerLoopTiming.Update) + public static UniTask WaitUntilCanceled(CancellationToken cancellationToken, PlayerLoopTiming timing = PlayerLoopTiming.Update, bool completeImmediately = false) { - return new UniTask(WaitUntilCanceledPromise.Create(cancellationToken, timing, out var token), token); + return new UniTask(WaitUntilCanceledPromise.Create(cancellationToken, timing, completeImmediately, out var token), token); } - public static UniTask WaitUntilValueChanged(T target, Func monitorFunction, PlayerLoopTiming monitorTiming = PlayerLoopTiming.Update, IEqualityComparer equalityComparer = null, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask WaitUntilValueChanged(T target, Func monitorFunction, PlayerLoopTiming monitorTiming = PlayerLoopTiming.Update, IEqualityComparer equalityComparer = null, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) where T : class { var unityObject = target as UnityEngine.Object; var isUnityObject = target is UnityEngine.Object; // don't use (unityObject == null) return new UniTask(isUnityObject - ? WaitUntilValueChangedUnityObjectPromise.Create(target, monitorFunction, equalityComparer, monitorTiming, cancellationToken, out var token) - : WaitUntilValueChangedStandardObjectPromise.Create(target, monitorFunction, equalityComparer, monitorTiming, cancellationToken, out token), token); + ? WaitUntilValueChangedUnityObjectPromise.Create(target, monitorFunction, equalityComparer, monitorTiming, cancellationToken, cancelImmediately, out var token) + : WaitUntilValueChangedStandardObjectPromise.Create(target, monitorFunction, equalityComparer, monitorTiming, cancellationToken, cancelImmediately, out token), token); } sealed class WaitUntilPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode @@ -48,6 +48,7 @@ static WaitUntilPromise() Func predicate; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; @@ -55,7 +56,7 @@ static WaitUntilPromise() { } - public static IUniTaskSource Create(Func predicate, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(Func predicate, PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -70,6 +71,15 @@ public static IUniTaskSource Create(Func predicate, PlayerLoopTiming timin result.predicate = predicate; result.cancellationToken = cancellationToken; + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (WaitUntilPromise)state; + promise.core.TrySetCanceled(promise.cancellationToken); + }, result); + } + TaskTracker.TrackActiveTask(result, 3); PlayerLoopHelper.AddAction(timing, result); @@ -136,6 +146,7 @@ bool TryReturn() core.Reset(); predicate = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } @@ -153,6 +164,7 @@ static WaitWhilePromise() Func predicate; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; @@ -160,7 +172,7 @@ static WaitWhilePromise() { } - public static IUniTaskSource Create(Func predicate, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(Func predicate, PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -174,6 +186,15 @@ public static IUniTaskSource Create(Func predicate, PlayerLoopTiming timin result.predicate = predicate; result.cancellationToken = cancellationToken; + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (WaitWhilePromise)state; + promise.core.TrySetCanceled(promise.cancellationToken); + }, result); + } TaskTracker.TrackActiveTask(result, 3); @@ -241,6 +262,7 @@ bool TryReturn() core.Reset(); predicate = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } @@ -257,6 +279,7 @@ static WaitUntilCanceledPromise() } CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; @@ -264,7 +287,7 @@ static WaitUntilCanceledPromise() { } - public static IUniTaskSource Create(CancellationToken cancellationToken, PlayerLoopTiming timing, out short token) + public static IUniTaskSource Create(CancellationToken cancellationToken, PlayerLoopTiming timing, bool completeImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -278,6 +301,15 @@ public static IUniTaskSource Create(CancellationToken cancellationToken, PlayerL result.cancellationToken = cancellationToken; + if (completeImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (WaitUntilCanceledPromise)state; + promise.core.TrySetResult(null); + }, result); + } + TaskTracker.TrackActiveTask(result, 3); PlayerLoopHelper.AddAction(timing, result); @@ -329,6 +361,7 @@ bool TryReturn() TaskTracker.RemoveTracking(this); core.Reset(); cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } @@ -351,6 +384,7 @@ static WaitUntilValueChangedUnityObjectPromise() Func monitorFunction; IEqualityComparer equalityComparer; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; @@ -358,7 +392,7 @@ static WaitUntilValueChangedUnityObjectPromise() { } - public static IUniTaskSource Create(T target, Func monitorFunction, IEqualityComparer equalityComparer, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(T target, Func monitorFunction, IEqualityComparer equalityComparer, PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -376,6 +410,15 @@ public static IUniTaskSource Create(T target, Func monitorFunction, IEq result.currentValue = monitorFunction(target); result.equalityComparer = equalityComparer ?? UnityEqualityComparer.GetDefault(); result.cancellationToken = cancellationToken; + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (WaitUntilValueChangedUnityObjectPromise)state; + promise.core.TrySetCanceled(promise.cancellationToken); + }, result); + } TaskTracker.TrackActiveTask(result, 3); @@ -453,6 +496,7 @@ bool TryReturn() monitorFunction = default; equalityComparer = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } @@ -474,6 +518,7 @@ static WaitUntilValueChangedStandardObjectPromise() Func monitorFunction; IEqualityComparer equalityComparer; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; @@ -481,7 +526,7 @@ static WaitUntilValueChangedStandardObjectPromise() { } - public static IUniTaskSource Create(T target, Func monitorFunction, IEqualityComparer equalityComparer, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(T target, Func monitorFunction, IEqualityComparer equalityComparer, PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -498,6 +543,15 @@ public static IUniTaskSource Create(T target, Func monitorFunction, IEq result.currentValue = monitorFunction(target); result.equalityComparer = equalityComparer ?? UnityEqualityComparer.GetDefault(); result.cancellationToken = cancellationToken; + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (WaitUntilValueChangedStandardObjectPromise)state; + promise.core.TrySetCanceled(promise.cancellationToken); + }, result); + } TaskTracker.TrackActiveTask(result, 3); @@ -575,6 +629,7 @@ bool TryReturn() monitorFunction = default; equalityComparer = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs index 1a1e011f..95f00b26 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs @@ -24,12 +24,17 @@ public static AssetBundleRequestAllAssetsAwaiter AwaitForAllAssets(this AssetBun return AwaitForAllAssets(asyncOperation, null, PlayerLoopTiming.Update, cancellationToken: cancellationToken); } - public static UniTask AwaitForAllAssets(this AssetBundleRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask AwaitForAllAssets(this AssetBundleRequest asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) + { + return AwaitForAllAssets(asyncOperation, null, PlayerLoopTiming.Update, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); + } + + public static UniTask AwaitForAllAssets(this AssetBundleRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.allAssets); - return new UniTask(AssetBundleRequestAllAssetsConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + return new UniTask(AssetBundleRequestAllAssetsConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); } public struct AssetBundleRequestAllAssetsAwaiter : ICriticalNotifyCompletion @@ -95,15 +100,15 @@ static AssetBundleRequestAllAssetsConfiguredSource() AssetBundleRequest asyncOperation; IProgress progress; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; AssetBundleRequestAllAssetsConfiguredSource() { - } - public static IUniTaskSource Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -118,6 +123,15 @@ static AssetBundleRequestAllAssetsConfiguredSource() result.asyncOperation = asyncOperation; result.progress = progress; result.cancellationToken = cancellationToken; + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (AssetBundleRequestAllAssetsConfiguredSource)state; + promise.core.TrySetCanceled(promise.cancellationToken); + }, result); + } TaskTracker.TrackActiveTask(result, 3); @@ -188,6 +202,7 @@ bool TryReturn() asyncOperation = default; progress = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AsyncGPUReadback.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AsyncGPUReadback.cs index 5805dbb1..0faf0be9 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AsyncGPUReadback.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AsyncGPUReadback.cs @@ -20,10 +20,15 @@ public static UniTask WithCancellation(this AsyncGPURea return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask ToUniTask(this AsyncGPUReadbackRequest asyncOperation, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask WithCancellation(this AsyncGPUReadbackRequest asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) + { + return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); + } + + public static UniTask ToUniTask(this AsyncGPUReadbackRequest asyncOperation, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { if (asyncOperation.done) return UniTask.FromResult(asyncOperation); - return new UniTask(AsyncGPUReadbackRequestAwaiterConfiguredSource.Create(asyncOperation, timing, cancellationToken, out var token), token); + return new UniTask(AsyncGPUReadbackRequestAwaiterConfiguredSource.Create(asyncOperation, timing, cancellationToken, cancelImmediately, out var token), token); } sealed class AsyncGPUReadbackRequestAwaiterConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode @@ -39,15 +44,15 @@ static AsyncGPUReadbackRequestAwaiterConfiguredSource() AsyncGPUReadbackRequest asyncOperation; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; AsyncGPUReadbackRequestAwaiterConfiguredSource() { - } - public static IUniTaskSource Create(AsyncGPUReadbackRequest asyncOperation, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(AsyncGPUReadbackRequest asyncOperation, PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -61,6 +66,15 @@ public static IUniTaskSource Create(AsyncGPUReadbackReq result.asyncOperation = asyncOperation; result.cancellationToken = cancellationToken; + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (AsyncGPUReadbackRequestAwaiterConfiguredSource)state; + promise.core.TrySetCanceled(promise.cancellationToken); + }, result); + } TaskTracker.TrackActiveTask(result, 3); @@ -131,6 +145,7 @@ bool TryReturn() core.Reset(); asyncOperation = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs index 00afe665..db225753 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs @@ -29,13 +29,18 @@ public static UniTask WithCancellation(this AsyncOperation asyncOperation, Cance { return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } + + public static UniTask WithCancellation(this AsyncOperation asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) + { + return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); + } - public static UniTask ToUniTask(this AsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask ToUniTask(this AsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.CompletedTask; - return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); } public struct AsyncOperationAwaiter : ICriticalNotifyCompletion @@ -92,15 +97,15 @@ static AsyncOperationConfiguredSource() AsyncOperation asyncOperation; IProgress progress; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; AsyncOperationConfiguredSource() { - } - public static IUniTaskSource Create(AsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(AsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -111,6 +116,16 @@ public static IUniTaskSource Create(AsyncOperation asyncOperation, PlayerLoopTim { result = new AsyncOperationConfiguredSource(); } + + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (AsyncOperationConfiguredSource)state; + promise.core.TrySetCanceled(promise.cancellationToken); + }, result); + } result.asyncOperation = asyncOperation; result.progress = progress; @@ -136,7 +151,6 @@ public void GetResult(short token) } } - public UniTaskStatus GetStatus(short token) { return core.GetStatus(token); @@ -181,6 +195,7 @@ bool TryReturn() asyncOperation = default; progress = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } @@ -739,7 +754,12 @@ public static UniTask WithCancellation(this UnityWebRequestAsyn return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask ToUniTask(this UnityWebRequestAsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask WithCancellation(this UnityWebRequestAsyncOperation asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) + { + return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); + } + + public static UniTask ToUniTask(this UnityWebRequestAsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); @@ -751,7 +771,7 @@ public static UniTask WithCancellation(this UnityWebRequestAsyn } return UniTask.FromResult(asyncOperation.webRequest); } - return new UniTask(UnityWebRequestAsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + return new UniTask(UnityWebRequestAsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); } public struct UnityWebRequestAsyncOperationAwaiter : ICriticalNotifyCompletion @@ -820,15 +840,15 @@ static UnityWebRequestAsyncOperationConfiguredSource() UnityWebRequestAsyncOperation asyncOperation; IProgress progress; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; UnityWebRequestAsyncOperationConfiguredSource() { - } - public static IUniTaskSource Create(UnityWebRequestAsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(UnityWebRequestAsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -843,6 +863,15 @@ public static IUniTaskSource Create(UnityWebRequestAsyncOperati result.asyncOperation = asyncOperation; result.progress = progress; result.cancellationToken = cancellationToken; + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (UnityWebRequestAsyncOperationConfiguredSource)state; + promise.core.TrySetCanceled(promise.cancellationToken); + }, result); + } TaskTracker.TrackActiveTask(result, 3); @@ -925,6 +954,7 @@ bool TryReturn() asyncOperation = default; progress = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } From 7f582e5e29228179cacee450da471d41ea125dd8 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 27 Oct 2023 14:51:52 +0900 Subject: [PATCH 02/13] Update README --- README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 487aec09..d1cacac9 100644 --- a/README.md +++ b/README.md @@ -336,6 +336,18 @@ if (isCanceled) Note: Only suppress throws if you call directly into the most source method. Otherwise, the return value will be converted, but the entire pipeline will not suppress throws. +Some features that use Unity's player loop, such as `UniTask.Yield` and `UniTask.Delay` etc, determines CancellationToken state on the player loop. +This means it does not cancel immediately upon `CancellationToken` fired. + +If you want to change this behaviour, the cancellation to be immediate, set the `cancelImmediately` flag as an argument. + +```csharp +await UniTask.Yield(cancellationToken, cancelImmediately: true); +``` + +Note: Setting `cancelImmediately` to true and detecting an immediate cancellation is more costly than the default behavior. +This is because it uses `CancellationToken.Register`; it is heavier than checking CancellationToken on the player loop. + Timeout handling --- Timeout is a variation of cancellation. You can set timeout by `CancellationTokenSouce.CancelAfterSlim(TimeSpan)` and pass CancellationToken to async methods. @@ -363,7 +375,7 @@ If you want to use timeout with other source of cancellation, use `CancellationT ```csharp var cancelToken = new CancellationTokenSource(); -cancelButton.onClick.AddListener(()=> +cancelButton.onClick.AddListener(() => { cancelToken.Cancel(); // cancel from button click. }); From 94be2e748b650baefa506f1def9a880e2a918c07 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 27 Oct 2023 15:06:12 +0900 Subject: [PATCH 03/13] Add cancelImmediately flag for addressable extensions --- .../AddressablesAsyncExtensions.cs | 44 ++++++++++++++++--- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs index f321bdba..a0ca8a1f 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/External/Addressables/AddressablesAsyncExtensions.cs @@ -25,7 +25,12 @@ public static UniTask WithCancellation(this AsyncOperationHandle handle, Cancell return ToUniTask(handle, cancellationToken: cancellationToken); } - public static UniTask ToUniTask(this AsyncOperationHandle handle, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask WithCancellation(this AsyncOperationHandle handle, CancellationToken cancellationToken, bool cancelImmediately) + { + return ToUniTask(handle, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); + } + + public static UniTask ToUniTask(this AsyncOperationHandle handle, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); @@ -44,7 +49,7 @@ public static UniTask WithCancellation(this AsyncOperationHandle handle, Cancell return UniTask.CompletedTask; } - return new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, timing, progress, cancellationToken, out var token), token); + return new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, timing, progress, cancellationToken, cancelImmediately, out var token), token); } public struct AsyncOperationHandleAwaiter : ICriticalNotifyCompletion @@ -106,6 +111,7 @@ static AsyncOperationHandleConfiguredSource() readonly Action continuationAction; AsyncOperationHandle handle; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; IProgress progress; bool completed; @@ -116,7 +122,7 @@ static AsyncOperationHandleConfiguredSource() continuationAction = Continuation; } - public static IUniTaskSource Create(AsyncOperationHandle handle, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(AsyncOperationHandle handle, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -132,6 +138,15 @@ public static IUniTaskSource Create(AsyncOperationHandle handle, PlayerLoopTimin result.progress = progress; result.cancellationToken = cancellationToken; result.completed = false; + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (AsyncOperationHandleConfiguredSource)state; + promise.core.TrySetCanceled(promise.cancellationToken); + }, result); + } TaskTracker.TrackActiveTask(result, 3); @@ -219,6 +234,7 @@ bool TryReturn() handle = default; progress = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } @@ -237,7 +253,12 @@ public static UniTask WithCancellation(this AsyncOperationHandle handle return ToUniTask(handle, cancellationToken: cancellationToken); } - public static UniTask ToUniTask(this AsyncOperationHandle handle, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask WithCancellation(this AsyncOperationHandle handle, CancellationToken cancellationToken, bool cancelImmediately) + { + return ToUniTask(handle, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); + } + + public static UniTask ToUniTask(this AsyncOperationHandle handle, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); @@ -255,7 +276,7 @@ public static UniTask WithCancellation(this AsyncOperationHandle handle return UniTask.FromResult(handle.Result); } - return new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, timing, progress, cancellationToken, out var token), token); + return new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, timing, progress, cancellationToken, cancelImmediately, out var token), token); } sealed class AsyncOperationHandleConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode> @@ -272,6 +293,7 @@ static AsyncOperationHandleConfiguredSource() readonly Action> continuationAction; AsyncOperationHandle handle; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; IProgress progress; bool completed; @@ -282,7 +304,7 @@ static AsyncOperationHandleConfiguredSource() continuationAction = Continuation; } - public static IUniTaskSource Create(AsyncOperationHandle handle, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(AsyncOperationHandle handle, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -298,6 +320,15 @@ public static IUniTaskSource Create(AsyncOperationHandle handle, PlayerLoo result.cancellationToken = cancellationToken; result.completed = false; result.progress = progress; + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var promise = (AsyncOperationHandleConfiguredSource)state; + promise.core.TrySetCanceled(promise.cancellationToken); + }, result); + } TaskTracker.TrackActiveTask(result, 3); @@ -390,6 +421,7 @@ bool TryReturn() handle = default; progress = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } From 2cf06af4331b0ac59b46a9c5d13c946b6309a3c1 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 27 Oct 2023 16:45:41 +0900 Subject: [PATCH 04/13] Change AsyncEnumerable factory to use OperationCanceledException --- .../Linq/UnityExtensions/EveryUpdate.cs | 18 ++++++-- .../Linq/UnityExtensions/EveryValueChanged.cs | 41 +++++++++++++++---- .../Runtime/Linq/UnityExtensions/Timer.cs | 29 +++++++++---- 3 files changed, 69 insertions(+), 19 deletions(-) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/EveryUpdate.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/EveryUpdate.cs index 09c5cf90..8f091100 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/EveryUpdate.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/EveryUpdate.cs @@ -56,10 +56,14 @@ public _EveryUpdate(PlayerLoopTiming updateTiming, CancellationToken cancellatio public UniTask MoveNextAsync() { - // return false instead of throw - if (disposed || cancellationToken.IsCancellationRequested) return CompletedTasks.False; - + if (disposed) return CompletedTasks.False; + completionSource.Reset(); + + if (cancellationToken.IsCancellationRequested) + { + completionSource.TrySetCanceled(cancellationToken); + } return new UniTask(this, completionSource.Version); } @@ -76,7 +80,13 @@ public UniTask DisposeAsync() public bool MoveNext() { - if (disposed || cancellationToken.IsCancellationRequested) + if (cancellationToken.IsCancellationRequested) + { + completionSource.TrySetCanceled(cancellationToken); + return false; + } + + if (disposed) { completionSource.TrySetResult(false); return false; diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/EveryValueChanged.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/EveryValueChanged.cs index 8b19f647..ef5739c7 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/EveryValueChanged.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/EveryValueChanged.cs @@ -85,8 +85,15 @@ public _EveryValueChanged(TTarget target, Func propertySelec public UniTask MoveNextAsync() { - // return false instead of throw - if (disposed || cancellationToken.IsCancellationRequested) return CompletedTasks.False; + if (disposed) return CompletedTasks.False; + + completionSource.Reset(); + + if (cancellationToken.IsCancellationRequested) + { + completionSource.TrySetCanceled(cancellationToken); + return new UniTask(this, completionSource.Version); + } if (first) { @@ -99,7 +106,6 @@ public UniTask MoveNextAsync() return CompletedTasks.True; } - completionSource.Reset(); return new UniTask(this, completionSource.Version); } @@ -107,6 +113,7 @@ public UniTask DisposeAsync() { if (!disposed) { + cancellationTokenRegistration.Dispose(); disposed = true; TaskTracker.RemoveTracking(this); } @@ -115,13 +122,18 @@ public UniTask DisposeAsync() public bool MoveNext() { - if (disposed || cancellationToken.IsCancellationRequested || targetAsUnityObject == null) // destroyed = cancel. + if (disposed || targetAsUnityObject == null) { completionSource.TrySetResult(false); DisposeAsync().Forget(); return false; } - + + if (cancellationToken.IsCancellationRequested) + { + completionSource.TrySetCanceled(cancellationToken); + return false; + } TProperty nextValue = default(TProperty); try { @@ -205,8 +217,16 @@ public _EveryValueChanged(WeakReference target, Func MoveNextAsync() { - if (disposed || cancellationToken.IsCancellationRequested) return CompletedTasks.False; + if (disposed) return CompletedTasks.False; + completionSource.Reset(); + + if (cancellationToken.IsCancellationRequested) + { + completionSource.TrySetCanceled(cancellationToken); + return new UniTask(this, completionSource.Version); + } + if (first) { first = false; @@ -218,7 +238,6 @@ public UniTask MoveNextAsync() return CompletedTasks.True; } - completionSource.Reset(); return new UniTask(this, completionSource.Version); } @@ -235,13 +254,19 @@ public UniTask DisposeAsync() public bool MoveNext() { - if (disposed || cancellationToken.IsCancellationRequested || !target.TryGetTarget(out var t)) + if (disposed || !target.TryGetTarget(out var t)) { completionSource.TrySetResult(false); DisposeAsync().Forget(); return false; } + if (cancellationToken.IsCancellationRequested) + { + completionSource.TrySetCanceled(cancellationToken); + return false; + } + TProperty nextValue = default(TProperty); try { diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/Timer.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/Timer.cs index a3bab3c5..b8aabf23 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/Timer.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/UnityExtensions/Timer.cs @@ -109,7 +109,6 @@ public _Timer(TimeSpan dueTime, TimeSpan? period, PlayerLoopTiming updateTiming, this.ignoreTimeScale = ignoreTimeScale; this.cancellationToken = cancellationToken; - if (cancelImmediately && cancellationToken.CanBeCanceled) { cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => @@ -128,12 +127,16 @@ public _Timer(TimeSpan dueTime, TimeSpan? period, PlayerLoopTiming updateTiming, public UniTask MoveNextAsync() { // return false instead of throw - if (disposed || cancellationToken.IsCancellationRequested || completed) return CompletedTasks.False; + if (disposed || completed) return CompletedTasks.False; // reset value here. this.elapsed = 0; completionSource.Reset(); + if (cancellationToken.IsCancellationRequested) + { + completionSource.TrySetCanceled(cancellationToken); + } return new UniTask(this, completionSource.Version); } @@ -150,11 +153,16 @@ public UniTask DisposeAsync() public bool MoveNext() { - if (disposed || cancellationToken.IsCancellationRequested) + if (disposed) { completionSource.TrySetResult(false); return false; } + if (cancellationToken.IsCancellationRequested) + { + completionSource.TrySetCanceled(cancellationToken); + return false; + } if (dueTimePhase) { @@ -261,13 +269,15 @@ public _TimerFrame(int dueTimeFrameCount, int? periodFrameCount, PlayerLoopTimin public UniTask MoveNextAsync() { - // return false instead of throw - if (disposed || cancellationToken.IsCancellationRequested || completed) return CompletedTasks.False; + if (disposed || completed) return CompletedTasks.False; + if (cancellationToken.IsCancellationRequested) + { + completionSource.TrySetCanceled(cancellationToken); + } // reset value here. this.currentFrame = 0; - completionSource.Reset(); return new UniTask(this, completionSource.Version); } @@ -285,7 +295,12 @@ public UniTask DisposeAsync() public bool MoveNext() { - if (disposed || cancellationToken.IsCancellationRequested) + if (cancellationToken.IsCancellationRequested) + { + completionSource.TrySetCanceled(cancellationToken); + return false; + } + if (disposed) { completionSource.TrySetResult(false); return false; From 24afc4f3ebe7f00388ad1e576aebbdf3cc074225 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 27 Oct 2023 17:48:17 +0900 Subject: [PATCH 05/13] Add cancelImmediately flag for assetbundle extensions --- .../UniTask/Runtime/UnityAsyncExtensions.cs | 69 +++++++++++++++---- 1 file changed, 57 insertions(+), 12 deletions(-) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs index db225753..235faa05 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs @@ -215,12 +215,17 @@ public static ResourceRequestAwaiter GetAwaiter(this ResourceRequest asyncOperat return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask ToUniTask(this ResourceRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask WithCancellation(this ResourceRequest asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) + { + return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); + } + + public static UniTask ToUniTask(this ResourceRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); - return new UniTask(ResourceRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + return new UniTask(ResourceRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); } public struct ResourceRequestAwaiter : ICriticalNotifyCompletion @@ -281,15 +286,15 @@ static ResourceRequestConfiguredSource() ResourceRequest asyncOperation; IProgress progress; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; ResourceRequestConfiguredSource() { - } - public static IUniTaskSource Create(ResourceRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(ResourceRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -304,6 +309,15 @@ static ResourceRequestConfiguredSource() result.asyncOperation = asyncOperation; result.progress = progress; result.cancellationToken = cancellationToken; + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (ResourceRequestConfiguredSource)state; + source.core.TrySetCanceled(source.cancellationToken); + }, result); + } TaskTracker.TrackActiveTask(result, 3); @@ -374,6 +388,7 @@ bool TryReturn() asyncOperation = default; progress = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } @@ -394,12 +409,17 @@ public static AssetBundleRequestAwaiter GetAwaiter(this AssetBundleRequest async return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask ToUniTask(this AssetBundleRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask WithCancellation(this AssetBundleRequest asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) + { + return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); + } + + public static UniTask ToUniTask(this AssetBundleRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); - return new UniTask(AssetBundleRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + return new UniTask(AssetBundleRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); } public struct AssetBundleRequestAwaiter : ICriticalNotifyCompletion @@ -460,15 +480,15 @@ static AssetBundleRequestConfiguredSource() AssetBundleRequest asyncOperation; IProgress progress; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; AssetBundleRequestConfiguredSource() { - } - public static IUniTaskSource Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -483,6 +503,15 @@ static AssetBundleRequestConfiguredSource() result.asyncOperation = asyncOperation; result.progress = progress; result.cancellationToken = cancellationToken; + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (AssetBundleRequestConfiguredSource)state; + source.core.TrySetCanceled(source.cancellationToken); + }, result); + } TaskTracker.TrackActiveTask(result, 3); @@ -553,6 +582,7 @@ bool TryReturn() asyncOperation = default; progress = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } @@ -574,12 +604,17 @@ public static UniTask WithCancellation(this AssetBundleCreateReques return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask ToUniTask(this AssetBundleCreateRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask WithCancellation(this AssetBundleCreateRequest asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) + { + return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); + } + + public static UniTask ToUniTask(this AssetBundleCreateRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.assetBundle); - return new UniTask(AssetBundleCreateRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + return new UniTask(AssetBundleCreateRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); } public struct AssetBundleCreateRequestAwaiter : ICriticalNotifyCompletion @@ -640,15 +675,15 @@ static AssetBundleCreateRequestConfiguredSource() AssetBundleCreateRequest asyncOperation; IProgress progress; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; AssetBundleCreateRequestConfiguredSource() { - } - public static IUniTaskSource Create(AssetBundleCreateRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(AssetBundleCreateRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -663,6 +698,15 @@ public static IUniTaskSource Create(AssetBundleCreateRequest asyncO result.asyncOperation = asyncOperation; result.progress = progress; result.cancellationToken = cancellationToken; + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (AssetBundleCreateRequestConfiguredSource)state; + source.core.TrySetCanceled(source.cancellationToken); + }, result); + } TaskTracker.TrackActiveTask(result, 3); @@ -733,6 +777,7 @@ bool TryReturn() asyncOperation = default; progress = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } From f0adf36633ea17ec3a97c46918536c05184e804a Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 27 Oct 2023 19:38:37 +0900 Subject: [PATCH 06/13] Use AsyncOperation.completed handler with new optional argument --- ...cExtensions.AssetBundleRequestAllAssets.cs | 130 +++- .../UniTask/Runtime/UnityAsyncExtensions.cs | 685 +++++++++++++++--- .../UniTask/Runtime/UnityAsyncExtensions.tt | 148 +++- 3 files changed, 855 insertions(+), 108 deletions(-) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs index 95f00b26..f6fccc04 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs @@ -24,17 +24,24 @@ public static AssetBundleRequestAllAssetsAwaiter AwaitForAllAssets(this AssetBun return AwaitForAllAssets(asyncOperation, null, PlayerLoopTiming.Update, cancellationToken: cancellationToken); } - public static UniTask AwaitForAllAssets(this AssetBundleRequest asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) + public static UniTask AwaitForAllAssets(this AssetBundleRequest asyncOperation, bool handleImmediately, CancellationToken cancellationToken) { - return AwaitForAllAssets(asyncOperation, null, PlayerLoopTiming.Update, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); + if (handleImmediately) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); + if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.allAssets); + return new UniTask(AssetBundleRequestAllAssetsCallbackHandlerSource.Create(asyncOperation, cancellationToken, out var token), token); + } + return AwaitForAllAssets(asyncOperation, progress: null, cancellationToken: cancellationToken); } - public static UniTask AwaitForAllAssets(this AssetBundleRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) + public static UniTask AwaitForAllAssets(this AssetBundleRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.allAssets); - return new UniTask(AssetBundleRequestAllAssetsConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); + return new UniTask(AssetBundleRequestAllAssetsConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); } public struct AssetBundleRequestAllAssetsAwaiter : ICriticalNotifyCompletion @@ -85,6 +92,108 @@ public void UnsafeOnCompleted(Action continuation) asyncOperation.completed += continuationAction; } } + + sealed class AssetBundleRequestAllAssetsCallbackHandlerSource : IUniTaskSource, ITaskPoolNode + { + static TaskPool pool; + AssetBundleRequestAllAssetsCallbackHandlerSource nextNode; + public ref AssetBundleRequestAllAssetsCallbackHandlerSource NextNode => ref nextNode; + + static AssetBundleRequestAllAssetsCallbackHandlerSource() + { + TaskPool.RegisterSizeGetter(typeof(AssetBundleRequestConfiguredSource), () => pool.Size); + } + + AssetBundleRequest asyncOperation; + CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; + + UniTaskCompletionSourceCore core; + + AssetBundleRequestAllAssetsCallbackHandlerSource() + { + } + + public static IUniTaskSource Create(AssetBundleRequest asyncOperation, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new AssetBundleRequestAllAssetsCallbackHandlerSource(); + } + + result.asyncOperation = asyncOperation; + result.cancellationToken = cancellationToken; + + asyncOperation.completed += result.AsyncOperationCompletedHandler; + + if (cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (AssetBundleRequestAllAssetsCallbackHandlerSource)state; + source.core.TrySetCanceled(source.cancellationToken); + }, result); + } + + TaskTracker.TrackActiveTask(result, 3); + + token = result.core.Version; + return result; + } + + public UnityEngine.Object[] GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + asyncOperation.completed -= AsyncOperationCompletedHandler; + asyncOperation = default; + cancellationToken = default; + cancellationTokenRegistration.Dispose(); + return pool.TryPush(this); + } + + void AsyncOperationCompletedHandler(AsyncOperation _) + { + core.TrySetResult(asyncOperation.allAssets); + } + } sealed class AssetBundleRequestAllAssetsConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode { @@ -100,7 +209,6 @@ static AssetBundleRequestAllAssetsConfiguredSource() AssetBundleRequest asyncOperation; IProgress progress; CancellationToken cancellationToken; - CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; @@ -108,7 +216,7 @@ static AssetBundleRequestAllAssetsConfiguredSource() { } - public static IUniTaskSource Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) + public static IUniTaskSource Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -123,15 +231,6 @@ static AssetBundleRequestAllAssetsConfiguredSource() result.asyncOperation = asyncOperation; result.progress = progress; result.cancellationToken = cancellationToken; - - if (cancelImmediately && cancellationToken.CanBeCanceled) - { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var promise = (AssetBundleRequestAllAssetsConfiguredSource)state; - promise.core.TrySetCanceled(promise.cancellationToken); - }, result); - } TaskTracker.TrackActiveTask(result, 3); @@ -202,7 +301,6 @@ bool TryReturn() asyncOperation = default; progress = default; cancellationToken = default; - cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs index 235faa05..069ee5a6 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs @@ -15,32 +15,35 @@ public static partial class UnityAsyncExtensions { #region AsyncOperation -#if !UNITY_2023_1_OR_NEWER - // from Unity2023.1.0a15, AsyncOperationAwaitableExtensions.GetAwaiter is defined in UnityEngine. - public static AsyncOperationAwaiter GetAwaiter(this AsyncOperation asyncOperation) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); return new AsyncOperationAwaiter(asyncOperation); } -#endif public static UniTask WithCancellation(this AsyncOperation asyncOperation, CancellationToken cancellationToken) { return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - - public static UniTask WithCancellation(this AsyncOperation asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) + + public static UniTask WithCancellation(this AsyncOperation asyncOperation, bool handleImmediately, CancellationToken cancellationToken) { - return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); + if (handleImmediately) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); + if (asyncOperation.isDone) return UniTask.CompletedTask; + return new UniTask(AsyncOperationCallbackHandlerSource.Create(asyncOperation, cancellationToken, out var token), token); + } + return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask ToUniTask(this AsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) + public static UniTask ToUniTask(this AsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.CompletedTask; - return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); + return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); } public struct AsyncOperationAwaiter : ICriticalNotifyCompletion @@ -83,6 +86,106 @@ public void UnsafeOnCompleted(Action continuation) } } + sealed class AsyncOperationCallbackHandlerSource : IUniTaskSource, ITaskPoolNode + { + static TaskPool pool; + AsyncOperationCallbackHandlerSource nextNode; + public ref AsyncOperationCallbackHandlerSource NextNode => ref nextNode; + + static AsyncOperationCallbackHandlerSource() + { + TaskPool.RegisterSizeGetter(typeof(AsyncOperationCallbackHandlerSource), () => pool.Size); + } + + AsyncOperation asyncOperation; + IProgress progress; + CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; + + UniTaskCompletionSourceCore core; + + AsyncOperationCallbackHandlerSource() + { + } + + public static IUniTaskSource Create(AsyncOperation asyncOperation, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new AsyncOperationCallbackHandlerSource(); + } + + result.asyncOperation = asyncOperation; + result.cancellationToken = cancellationToken; + + asyncOperation.completed += result.AsyncOperationCompletedHandler; + + if (cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (AsyncOperationCallbackHandlerSource)state; + source.core.TrySetCanceled(source.cancellationToken); + }, result); + } + + TaskTracker.TrackActiveTask(result, 3); + + token = result.core.Version; + return result; + } + + public void GetResult(short token) + { + try + { + core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + asyncOperation.completed -= AsyncOperationCompletedHandler; + asyncOperation = default; + progress = default; + cancellationToken = default; + cancellationTokenRegistration.Dispose(); + return pool.TryPush(this); + } + + void AsyncOperationCompletedHandler(AsyncOperation _) + { + core.TrySetResult(AsyncUnit.Default); + } + } + sealed class AsyncOperationConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode { static TaskPool pool; @@ -97,7 +200,6 @@ static AsyncOperationConfiguredSource() AsyncOperation asyncOperation; IProgress progress; CancellationToken cancellationToken; - CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; @@ -105,7 +207,7 @@ static AsyncOperationConfiguredSource() { } - public static IUniTaskSource Create(AsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) + public static IUniTaskSource Create(AsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -116,16 +218,6 @@ public static IUniTaskSource Create(AsyncOperation asyncOperation, PlayerLoopTim { result = new AsyncOperationConfiguredSource(); } - - - if (cancelImmediately && cancellationToken.CanBeCanceled) - { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var promise = (AsyncOperationConfiguredSource)state; - promise.core.TrySetCanceled(promise.cancellationToken); - }, result); - } result.asyncOperation = asyncOperation; result.progress = progress; @@ -151,6 +243,7 @@ public void GetResult(short token) } } + public UniTaskStatus GetStatus(short token) { return core.GetStatus(token); @@ -195,7 +288,6 @@ bool TryReturn() asyncOperation = default; progress = default; cancellationToken = default; - cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } @@ -215,17 +307,24 @@ public static ResourceRequestAwaiter GetAwaiter(this ResourceRequest asyncOperat return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask WithCancellation(this ResourceRequest asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) + public static UniTask WithCancellation(this ResourceRequest asyncOperation, bool handleImmediately, CancellationToken cancellationToken) { - return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); + if (handleImmediately) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); + if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); + return new UniTask(ResourceRequestCallbackHandlerSource.Create(asyncOperation, cancellationToken, out var token), token); + } + return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask ToUniTask(this ResourceRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) + public static UniTask ToUniTask(this ResourceRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); - return new UniTask(ResourceRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); + return new UniTask(ResourceRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); } public struct ResourceRequestAwaiter : ICriticalNotifyCompletion @@ -272,6 +371,110 @@ public void UnsafeOnCompleted(Action continuation) } } + sealed class ResourceRequestCallbackHandlerSource : IUniTaskSource, ITaskPoolNode + { + static TaskPool pool; + ResourceRequestCallbackHandlerSource nextNode; + public ref ResourceRequestCallbackHandlerSource NextNode => ref nextNode; + + static ResourceRequestCallbackHandlerSource() + { + TaskPool.RegisterSizeGetter(typeof(ResourceRequestCallbackHandlerSource), () => pool.Size); + } + + ResourceRequest asyncOperation; + IProgress progress; + CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; + + UniTaskCompletionSourceCore core; + + ResourceRequestCallbackHandlerSource() + { + } + + public static IUniTaskSource Create(ResourceRequest asyncOperation, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new ResourceRequestCallbackHandlerSource(); + } + + result.asyncOperation = asyncOperation; + result.cancellationToken = cancellationToken; + + asyncOperation.completed += result.AsyncOperationCompletedHandler; + + if (cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (ResourceRequestCallbackHandlerSource)state; + source.core.TrySetCanceled(source.cancellationToken); + }, result); + } + + TaskTracker.TrackActiveTask(result, 3); + + token = result.core.Version; + return result; + } + + public UnityEngine.Object GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + asyncOperation.completed -= AsyncOperationCompletedHandler; + asyncOperation = default; + progress = default; + cancellationToken = default; + cancellationTokenRegistration.Dispose(); + return pool.TryPush(this); + } + + void AsyncOperationCompletedHandler(AsyncOperation _) + { + core.TrySetResult(asyncOperation.asset); + } + } + sealed class ResourceRequestConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode { static TaskPool pool; @@ -286,7 +489,6 @@ static ResourceRequestConfiguredSource() ResourceRequest asyncOperation; IProgress progress; CancellationToken cancellationToken; - CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; @@ -294,7 +496,7 @@ static ResourceRequestConfiguredSource() { } - public static IUniTaskSource Create(ResourceRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) + public static IUniTaskSource Create(ResourceRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -309,15 +511,6 @@ static ResourceRequestConfiguredSource() result.asyncOperation = asyncOperation; result.progress = progress; result.cancellationToken = cancellationToken; - - if (cancelImmediately && cancellationToken.CanBeCanceled) - { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var source = (ResourceRequestConfiguredSource)state; - source.core.TrySetCanceled(source.cancellationToken); - }, result); - } TaskTracker.TrackActiveTask(result, 3); @@ -388,7 +581,6 @@ bool TryReturn() asyncOperation = default; progress = default; cancellationToken = default; - cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } @@ -409,17 +601,24 @@ public static AssetBundleRequestAwaiter GetAwaiter(this AssetBundleRequest async return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask WithCancellation(this AssetBundleRequest asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) + public static UniTask WithCancellation(this AssetBundleRequest asyncOperation, bool handleImmediately, CancellationToken cancellationToken) { - return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); + if (handleImmediately) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); + if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); + return new UniTask(AssetBundleRequestCallbackHandlerSource.Create(asyncOperation, cancellationToken, out var token), token); + } + return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask ToUniTask(this AssetBundleRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) + public static UniTask ToUniTask(this AssetBundleRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); - return new UniTask(AssetBundleRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); + return new UniTask(AssetBundleRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); } public struct AssetBundleRequestAwaiter : ICriticalNotifyCompletion @@ -466,6 +665,110 @@ public void UnsafeOnCompleted(Action continuation) } } + sealed class AssetBundleRequestCallbackHandlerSource : IUniTaskSource, ITaskPoolNode + { + static TaskPool pool; + AssetBundleRequestCallbackHandlerSource nextNode; + public ref AssetBundleRequestCallbackHandlerSource NextNode => ref nextNode; + + static AssetBundleRequestCallbackHandlerSource() + { + TaskPool.RegisterSizeGetter(typeof(AssetBundleRequestCallbackHandlerSource), () => pool.Size); + } + + AssetBundleRequest asyncOperation; + IProgress progress; + CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; + + UniTaskCompletionSourceCore core; + + AssetBundleRequestCallbackHandlerSource() + { + } + + public static IUniTaskSource Create(AssetBundleRequest asyncOperation, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new AssetBundleRequestCallbackHandlerSource(); + } + + result.asyncOperation = asyncOperation; + result.cancellationToken = cancellationToken; + + asyncOperation.completed += result.AsyncOperationCompletedHandler; + + if (cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (AssetBundleRequestCallbackHandlerSource)state; + source.core.TrySetCanceled(source.cancellationToken); + }, result); + } + + TaskTracker.TrackActiveTask(result, 3); + + token = result.core.Version; + return result; + } + + public UnityEngine.Object GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + asyncOperation.completed -= AsyncOperationCompletedHandler; + asyncOperation = default; + progress = default; + cancellationToken = default; + cancellationTokenRegistration.Dispose(); + return pool.TryPush(this); + } + + void AsyncOperationCompletedHandler(AsyncOperation _) + { + core.TrySetResult(asyncOperation.asset); + } + } + sealed class AssetBundleRequestConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode { static TaskPool pool; @@ -480,7 +783,6 @@ static AssetBundleRequestConfiguredSource() AssetBundleRequest asyncOperation; IProgress progress; CancellationToken cancellationToken; - CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; @@ -488,7 +790,7 @@ static AssetBundleRequestConfiguredSource() { } - public static IUniTaskSource Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) + public static IUniTaskSource Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -503,15 +805,6 @@ static AssetBundleRequestConfiguredSource() result.asyncOperation = asyncOperation; result.progress = progress; result.cancellationToken = cancellationToken; - - if (cancelImmediately && cancellationToken.CanBeCanceled) - { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var source = (AssetBundleRequestConfiguredSource)state; - source.core.TrySetCanceled(source.cancellationToken); - }, result); - } TaskTracker.TrackActiveTask(result, 3); @@ -582,7 +875,6 @@ bool TryReturn() asyncOperation = default; progress = default; cancellationToken = default; - cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } @@ -604,17 +896,24 @@ public static UniTask WithCancellation(this AssetBundleCreateReques return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask WithCancellation(this AssetBundleCreateRequest asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) + public static UniTask WithCancellation(this AssetBundleCreateRequest asyncOperation, bool handleImmediately, CancellationToken cancellationToken) { - return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); + if (handleImmediately) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); + if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.assetBundle); + return new UniTask(AssetBundleCreateRequestCallbackHandlerSource.Create(asyncOperation, cancellationToken, out var token), token); + } + return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask ToUniTask(this AssetBundleCreateRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) + public static UniTask ToUniTask(this AssetBundleCreateRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.assetBundle); - return new UniTask(AssetBundleCreateRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); + return new UniTask(AssetBundleCreateRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); } public struct AssetBundleCreateRequestAwaiter : ICriticalNotifyCompletion @@ -661,6 +960,110 @@ public void UnsafeOnCompleted(Action continuation) } } + sealed class AssetBundleCreateRequestCallbackHandlerSource : IUniTaskSource, ITaskPoolNode + { + static TaskPool pool; + AssetBundleCreateRequestCallbackHandlerSource nextNode; + public ref AssetBundleCreateRequestCallbackHandlerSource NextNode => ref nextNode; + + static AssetBundleCreateRequestCallbackHandlerSource() + { + TaskPool.RegisterSizeGetter(typeof(AssetBundleCreateRequestCallbackHandlerSource), () => pool.Size); + } + + AssetBundleCreateRequest asyncOperation; + IProgress progress; + CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; + + UniTaskCompletionSourceCore core; + + AssetBundleCreateRequestCallbackHandlerSource() + { + } + + public static IUniTaskSource Create(AssetBundleCreateRequest asyncOperation, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new AssetBundleCreateRequestCallbackHandlerSource(); + } + + result.asyncOperation = asyncOperation; + result.cancellationToken = cancellationToken; + + asyncOperation.completed += result.AsyncOperationCompletedHandler; + + if (cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (AssetBundleCreateRequestCallbackHandlerSource)state; + source.core.TrySetCanceled(source.cancellationToken); + }, result); + } + + TaskTracker.TrackActiveTask(result, 3); + + token = result.core.Version; + return result; + } + + public AssetBundle GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + asyncOperation.completed -= AsyncOperationCompletedHandler; + asyncOperation = default; + progress = default; + cancellationToken = default; + cancellationTokenRegistration.Dispose(); + return pool.TryPush(this); + } + + void AsyncOperationCompletedHandler(AsyncOperation _) + { + core.TrySetResult(asyncOperation.assetBundle); + } + } + sealed class AssetBundleCreateRequestConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode { static TaskPool pool; @@ -675,7 +1078,6 @@ static AssetBundleCreateRequestConfiguredSource() AssetBundleCreateRequest asyncOperation; IProgress progress; CancellationToken cancellationToken; - CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; @@ -683,7 +1085,7 @@ static AssetBundleCreateRequestConfiguredSource() { } - public static IUniTaskSource Create(AssetBundleCreateRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) + public static IUniTaskSource Create(AssetBundleCreateRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -698,15 +1100,6 @@ public static IUniTaskSource Create(AssetBundleCreateRequest asyncO result.asyncOperation = asyncOperation; result.progress = progress; result.cancellationToken = cancellationToken; - - if (cancelImmediately && cancellationToken.CanBeCanceled) - { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var source = (AssetBundleCreateRequestConfiguredSource)state; - source.core.TrySetCanceled(source.cancellationToken); - }, result); - } TaskTracker.TrackActiveTask(result, 3); @@ -777,7 +1170,6 @@ bool TryReturn() asyncOperation = default; progress = default; cancellationToken = default; - cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } @@ -799,12 +1191,26 @@ public static UniTask WithCancellation(this UnityWebRequestAsyn return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask WithCancellation(this UnityWebRequestAsyncOperation asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) + public static UniTask WithCancellation(this UnityWebRequestAsyncOperation asyncOperation, bool handleImmediately, CancellationToken cancellationToken) { - return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); + if (handleImmediately) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); + if (asyncOperation.isDone) + { + if (asyncOperation.webRequest.IsError()) + { + return UniTask.FromException(new UnityWebRequestException(asyncOperation.webRequest)); + } + return UniTask.FromResult(asyncOperation.webRequest); + } + return new UniTask(UnityWebRequestAsyncOperationCallbackHandlerSource.Create(asyncOperation, cancellationToken, out var token), token); + } + return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask ToUniTask(this UnityWebRequestAsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) + public static UniTask ToUniTask(this UnityWebRequestAsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); @@ -816,7 +1222,7 @@ public static UniTask WithCancellation(this UnityWebRequestAsyn } return UniTask.FromResult(asyncOperation.webRequest); } - return new UniTask(UnityWebRequestAsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); + return new UniTask(UnityWebRequestAsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); } public struct UnityWebRequestAsyncOperationAwaiter : ICriticalNotifyCompletion @@ -871,6 +1277,118 @@ public void UnsafeOnCompleted(Action continuation) } } + sealed class UnityWebRequestAsyncOperationCallbackHandlerSource : IUniTaskSource, ITaskPoolNode + { + static TaskPool pool; + UnityWebRequestAsyncOperationCallbackHandlerSource nextNode; + public ref UnityWebRequestAsyncOperationCallbackHandlerSource NextNode => ref nextNode; + + static UnityWebRequestAsyncOperationCallbackHandlerSource() + { + TaskPool.RegisterSizeGetter(typeof(UnityWebRequestAsyncOperationCallbackHandlerSource), () => pool.Size); + } + + UnityWebRequestAsyncOperation asyncOperation; + IProgress progress; + CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; + + UniTaskCompletionSourceCore core; + + UnityWebRequestAsyncOperationCallbackHandlerSource() + { + } + + public static IUniTaskSource Create(UnityWebRequestAsyncOperation asyncOperation, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new UnityWebRequestAsyncOperationCallbackHandlerSource(); + } + + result.asyncOperation = asyncOperation; + result.cancellationToken = cancellationToken; + + asyncOperation.completed += result.AsyncOperationCompletedHandler; + + if (cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (UnityWebRequestAsyncOperationCallbackHandlerSource)state; + source.asyncOperation.webRequest.Abort(); + source.core.TrySetCanceled(source.cancellationToken); + }, result); + } + + TaskTracker.TrackActiveTask(result, 3); + + token = result.core.Version; + return result; + } + + public UnityWebRequest GetResult(short token) + { + try + { + return core.GetResult(token); + } + finally + { + TryReturn(); + } + } + + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + asyncOperation.completed -= AsyncOperationCompletedHandler; + asyncOperation = default; + progress = default; + cancellationToken = default; + cancellationTokenRegistration.Dispose(); + return pool.TryPush(this); + } + + void AsyncOperationCompletedHandler(AsyncOperation _) + { + if (asyncOperation.webRequest.IsError()) + { + core.TrySetException(new UnityWebRequestException(asyncOperation.webRequest)); + } + else + { + core.TrySetResult(asyncOperation.webRequest); + } + } + } + sealed class UnityWebRequestAsyncOperationConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode { static TaskPool pool; @@ -885,7 +1403,6 @@ static UnityWebRequestAsyncOperationConfiguredSource() UnityWebRequestAsyncOperation asyncOperation; IProgress progress; CancellationToken cancellationToken; - CancellationTokenRegistration cancellationTokenRegistration; UniTaskCompletionSourceCore core; @@ -893,7 +1410,7 @@ static UnityWebRequestAsyncOperationConfiguredSource() { } - public static IUniTaskSource Create(UnityWebRequestAsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) + public static IUniTaskSource Create(UnityWebRequestAsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -908,15 +1425,6 @@ public static IUniTaskSource Create(UnityWebRequestAsyncOperati result.asyncOperation = asyncOperation; result.progress = progress; result.cancellationToken = cancellationToken; - - if (cancelImmediately && cancellationToken.CanBeCanceled) - { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var promise = (UnityWebRequestAsyncOperationConfiguredSource)state; - promise.core.TrySetCanceled(promise.cancellationToken); - }, result); - } TaskTracker.TrackActiveTask(result, 3); @@ -974,11 +1482,7 @@ public bool MoveNext() if (asyncOperation.isDone) { - if (asyncOperation.webRequest == null) - { - core.TrySetException(new ObjectDisposedException("The webRequest has been destroyed.")); - } - else if (asyncOperation.webRequest.IsError()) + if (asyncOperation.webRequest.IsError()) { core.TrySetException(new UnityWebRequestException(asyncOperation.webRequest)); } @@ -999,7 +1503,6 @@ bool TryReturn() asyncOperation = default; progress = default; cancellationToken = default; - cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } } diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt index 65dac9e8..f74fa0b1 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt @@ -54,6 +54,29 @@ namespace Cysharp.Threading.Tasks return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } + public static <#= ToUniTaskReturnType(t.returnType) #> WithCancellation(this <#= t.typeName #> asyncOperation, bool handleImmediately, CancellationToken cancellationToken) + { + if (handleImmediately) + { + Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); + if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<#= IsVoid(t) ? "" : "<" + t.returnType + ">" #>(cancellationToken); +<# if(IsUnityWebRequest(t)) { #> + if (asyncOperation.isDone) + { + if (asyncOperation.webRequest.IsError()) + { + return UniTask.FromException(new UnityWebRequestException(asyncOperation.webRequest)); + } + return UniTask.FromResult(asyncOperation.webRequest); + } +<# } else { #> + if (asyncOperation.isDone) return <#= IsVoid(t) ? "UniTask.CompletedTask" : $"UniTask.FromResult(asyncOperation.{t.returnField})" #>; +<# } #> + return new <#= ToUniTaskReturnType(t.returnType) #>(<#= t.typeName #>CallbackHandlerSource.Create(asyncOperation, cancellationToken, out var token), token); + } + return ToUniTask(asyncOperation, cancellationToken: cancellationToken); + } + public static <#= ToUniTaskReturnType(t.returnType) #> ToUniTask(this <#= t.typeName #> asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); @@ -137,6 +160,130 @@ namespace Cysharp.Threading.Tasks } } + sealed class <#= t.typeName #>CallbackHandlerSource : <#= ToIUniTaskSourceReturnType(t.returnType) #>, ITaskPoolNode<<#= t.typeName #>CallbackHandlerSource> + { + static TaskPool<<#= t.typeName #>CallbackHandlerSource> pool; + <#= t.typeName #>CallbackHandlerSource nextNode; + public ref <#= t.typeName #>CallbackHandlerSource NextNode => ref nextNode; + + static <#= t.typeName #>CallbackHandlerSource() + { + TaskPool.RegisterSizeGetter(typeof(<#= t.typeName #>CallbackHandlerSource), () => pool.Size); + } + + <#= t.typeName #> asyncOperation; + IProgress progress; + CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; + + UniTaskCompletionSourceCore<<#= IsVoid(t) ? "AsyncUnit" : t.returnType #>> core; + + <#= t.typeName #>CallbackHandlerSource() + { + } + + public static <#= ToIUniTaskSourceReturnType(t.returnType) #> Create(<#= t.typeName #> asyncOperation, CancellationToken cancellationToken, out short token) + { + if (cancellationToken.IsCancellationRequested) + { + return AutoResetUniTaskCompletionSource<#= IsVoid(t) ? "" : $"<{t.returnType}>" #>.CreateFromCanceled(cancellationToken, out token); + } + + if (!pool.TryPop(out var result)) + { + result = new <#= t.typeName #>CallbackHandlerSource(); + } + + result.asyncOperation = asyncOperation; + result.cancellationToken = cancellationToken; + + asyncOperation.completed += result.AsyncOperationCompletedHandler; + + if (cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (<#= t.typeName #>CallbackHandlerSource)state; +<# if(IsUnityWebRequest(t)) { #> + source.asyncOperation.webRequest.Abort(); +<# } #> + source.core.TrySetCanceled(source.cancellationToken); + }, result); + } + + TaskTracker.TrackActiveTask(result, 3); + + token = result.core.Version; + return result; + } + + public <#= t.returnType #> GetResult(short token) + { + try + { +<# if (!IsVoid(t)) { #> + return core.GetResult(token); +<# } else { #> + core.GetResult(token); +<# } #> + } + finally + { + TryReturn(); + } + } + +<# if (!IsVoid(t)) { #> + void IUniTaskSource.GetResult(short token) + { + GetResult(token); + } +<# } #> + + public UniTaskStatus GetStatus(short token) + { + return core.GetStatus(token); + } + + public UniTaskStatus UnsafeGetStatus() + { + return core.UnsafeGetStatus(); + } + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + asyncOperation.completed -= AsyncOperationCompletedHandler; + asyncOperation = default; + progress = default; + cancellationToken = default; + cancellationTokenRegistration.Dispose(); + return pool.TryPush(this); + } + + void AsyncOperationCompletedHandler(AsyncOperation _) + { +<# if(IsUnityWebRequest(t)) { #> + if (asyncOperation.webRequest.IsError()) + { + core.TrySetException(new UnityWebRequestException(asyncOperation.webRequest)); + } + else + { + core.TrySetResult(asyncOperation.webRequest); + } +<# } else { #> + core.TrySetResult(<#= IsVoid(t) ? "AsyncUnit.Default" : $"asyncOperation.{t.returnField}" #>); +<# } #> + } + } + sealed class <#= t.typeName #>ConfiguredSource : <#= ToIUniTaskSourceReturnType(t.returnType) #>, IPlayerLoopItem, ITaskPoolNode<<#= t.typeName #>ConfiguredSource> { static TaskPool<<#= t.typeName #>ConfiguredSource> pool; @@ -156,7 +303,6 @@ namespace Cysharp.Threading.Tasks <#= t.typeName #>ConfiguredSource() { - } public static <#= ToIUniTaskSourceReturnType(t.returnType) #> Create(<#= t.typeName #> asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) From 55be4dba829f9bcc4617c2371764f123d6c26d27 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 27 Oct 2023 20:11:20 +0900 Subject: [PATCH 07/13] Add callback handler to AsyncOperationConfiguredSource instead to create other sources --- .../UniTask/Runtime/UnityAsyncExtensions.cs | 845 +++++------------- .../UniTask/Runtime/UnityAsyncExtensions.tt | 206 ++--- 2 files changed, 307 insertions(+), 744 deletions(-) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs index 069ee5a6..dc24ad07 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs @@ -28,22 +28,15 @@ public static UniTask WithCancellation(this AsyncOperation asyncOperation, Cance public static UniTask WithCancellation(this AsyncOperation asyncOperation, bool handleImmediately, CancellationToken cancellationToken) { - if (handleImmediately) - { - Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); - if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); - if (asyncOperation.isDone) return UniTask.CompletedTask; - return new UniTask(AsyncOperationCallbackHandlerSource.Create(asyncOperation, cancellationToken, out var token), token); - } - return ToUniTask(asyncOperation, cancellationToken: cancellationToken); + return ToUniTask(asyncOperation, handleImmediately: handleImmediately, cancellationToken: cancellationToken); } - public static UniTask ToUniTask(this AsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask ToUniTask(this AsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, bool handleImmediately = false, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.CompletedTask; - return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, handleImmediately, cancellationToken, out var token), token); } public struct AsyncOperationAwaiter : ICriticalNotifyCompletion @@ -86,106 +79,6 @@ public void UnsafeOnCompleted(Action continuation) } } - sealed class AsyncOperationCallbackHandlerSource : IUniTaskSource, ITaskPoolNode - { - static TaskPool pool; - AsyncOperationCallbackHandlerSource nextNode; - public ref AsyncOperationCallbackHandlerSource NextNode => ref nextNode; - - static AsyncOperationCallbackHandlerSource() - { - TaskPool.RegisterSizeGetter(typeof(AsyncOperationCallbackHandlerSource), () => pool.Size); - } - - AsyncOperation asyncOperation; - IProgress progress; - CancellationToken cancellationToken; - CancellationTokenRegistration cancellationTokenRegistration; - - UniTaskCompletionSourceCore core; - - AsyncOperationCallbackHandlerSource() - { - } - - public static IUniTaskSource Create(AsyncOperation asyncOperation, CancellationToken cancellationToken, out short token) - { - if (cancellationToken.IsCancellationRequested) - { - return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); - } - - if (!pool.TryPop(out var result)) - { - result = new AsyncOperationCallbackHandlerSource(); - } - - result.asyncOperation = asyncOperation; - result.cancellationToken = cancellationToken; - - asyncOperation.completed += result.AsyncOperationCompletedHandler; - - if (cancellationToken.CanBeCanceled) - { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var source = (AsyncOperationCallbackHandlerSource)state; - source.core.TrySetCanceled(source.cancellationToken); - }, result); - } - - TaskTracker.TrackActiveTask(result, 3); - - token = result.core.Version; - return result; - } - - public void GetResult(short token) - { - try - { - core.GetResult(token); - } - finally - { - TryReturn(); - } - } - - - public UniTaskStatus GetStatus(short token) - { - return core.GetStatus(token); - } - - public UniTaskStatus UnsafeGetStatus() - { - return core.UnsafeGetStatus(); - } - - public void OnCompleted(Action continuation, object state, short token) - { - core.OnCompleted(continuation, state, token); - } - - bool TryReturn() - { - TaskTracker.RemoveTracking(this); - core.Reset(); - asyncOperation.completed -= AsyncOperationCompletedHandler; - asyncOperation = default; - progress = default; - cancellationToken = default; - cancellationTokenRegistration.Dispose(); - return pool.TryPush(this); - } - - void AsyncOperationCompletedHandler(AsyncOperation _) - { - core.TrySetResult(AsyncUnit.Default); - } - } - sealed class AsyncOperationConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode { static TaskPool pool; @@ -200,14 +93,18 @@ static AsyncOperationConfiguredSource() AsyncOperation asyncOperation; IProgress progress; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; + bool completed; UniTaskCompletionSourceCore core; + Action continuationAction; + AsyncOperationConfiguredSource() { } - public static IUniTaskSource Create(AsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(AsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -222,6 +119,22 @@ public static IUniTaskSource Create(AsyncOperation asyncOperation, PlayerLoopTim result.asyncOperation = asyncOperation; result.progress = progress; result.cancellationToken = cancellationToken; + result.completed = false; + + if (handleImmediately) + { + result.continuationAction = result.Continuation; + asyncOperation.completed += result.continuationAction; + + if (cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (AsyncOperationConfiguredSource)state; + source.core.TrySetCanceled(source.cancellationToken); + }, result); + } + } TaskTracker.TrackActiveTask(result, 3); @@ -285,11 +198,33 @@ bool TryReturn() { TaskTracker.RemoveTracking(this); core.Reset(); + asyncOperation.completed -= continuationAction; asyncOperation = default; progress = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } + + void Continuation(AsyncOperation _) + { + if (completed) + { + TryReturn(); + } + else + { + completed = true; + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + } + else + { + core.TrySetResult(AsyncUnit.Default); + } + } + } } #endregion @@ -309,22 +244,15 @@ public static ResourceRequestAwaiter GetAwaiter(this ResourceRequest asyncOperat public static UniTask WithCancellation(this ResourceRequest asyncOperation, bool handleImmediately, CancellationToken cancellationToken) { - if (handleImmediately) - { - Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); - if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); - if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); - return new UniTask(ResourceRequestCallbackHandlerSource.Create(asyncOperation, cancellationToken, out var token), token); - } - return ToUniTask(asyncOperation, cancellationToken: cancellationToken); + return ToUniTask(asyncOperation, handleImmediately: handleImmediately, cancellationToken: cancellationToken); } - public static UniTask ToUniTask(this ResourceRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask ToUniTask(this ResourceRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, bool handleImmediately = false, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); - return new UniTask(ResourceRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + return new UniTask(ResourceRequestConfiguredSource.Create(asyncOperation, timing, progress, handleImmediately, cancellationToken, out var token), token); } public struct ResourceRequestAwaiter : ICriticalNotifyCompletion @@ -371,110 +299,6 @@ public void UnsafeOnCompleted(Action continuation) } } - sealed class ResourceRequestCallbackHandlerSource : IUniTaskSource, ITaskPoolNode - { - static TaskPool pool; - ResourceRequestCallbackHandlerSource nextNode; - public ref ResourceRequestCallbackHandlerSource NextNode => ref nextNode; - - static ResourceRequestCallbackHandlerSource() - { - TaskPool.RegisterSizeGetter(typeof(ResourceRequestCallbackHandlerSource), () => pool.Size); - } - - ResourceRequest asyncOperation; - IProgress progress; - CancellationToken cancellationToken; - CancellationTokenRegistration cancellationTokenRegistration; - - UniTaskCompletionSourceCore core; - - ResourceRequestCallbackHandlerSource() - { - } - - public static IUniTaskSource Create(ResourceRequest asyncOperation, CancellationToken cancellationToken, out short token) - { - if (cancellationToken.IsCancellationRequested) - { - return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); - } - - if (!pool.TryPop(out var result)) - { - result = new ResourceRequestCallbackHandlerSource(); - } - - result.asyncOperation = asyncOperation; - result.cancellationToken = cancellationToken; - - asyncOperation.completed += result.AsyncOperationCompletedHandler; - - if (cancellationToken.CanBeCanceled) - { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var source = (ResourceRequestCallbackHandlerSource)state; - source.core.TrySetCanceled(source.cancellationToken); - }, result); - } - - TaskTracker.TrackActiveTask(result, 3); - - token = result.core.Version; - return result; - } - - public UnityEngine.Object GetResult(short token) - { - try - { - return core.GetResult(token); - } - finally - { - TryReturn(); - } - } - - void IUniTaskSource.GetResult(short token) - { - GetResult(token); - } - - public UniTaskStatus GetStatus(short token) - { - return core.GetStatus(token); - } - - public UniTaskStatus UnsafeGetStatus() - { - return core.UnsafeGetStatus(); - } - - public void OnCompleted(Action continuation, object state, short token) - { - core.OnCompleted(continuation, state, token); - } - - bool TryReturn() - { - TaskTracker.RemoveTracking(this); - core.Reset(); - asyncOperation.completed -= AsyncOperationCompletedHandler; - asyncOperation = default; - progress = default; - cancellationToken = default; - cancellationTokenRegistration.Dispose(); - return pool.TryPush(this); - } - - void AsyncOperationCompletedHandler(AsyncOperation _) - { - core.TrySetResult(asyncOperation.asset); - } - } - sealed class ResourceRequestConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode { static TaskPool pool; @@ -489,14 +313,18 @@ static ResourceRequestConfiguredSource() ResourceRequest asyncOperation; IProgress progress; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; + bool completed; UniTaskCompletionSourceCore core; + Action continuationAction; + ResourceRequestConfiguredSource() { } - public static IUniTaskSource Create(ResourceRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(ResourceRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -511,6 +339,22 @@ static ResourceRequestConfiguredSource() result.asyncOperation = asyncOperation; result.progress = progress; result.cancellationToken = cancellationToken; + result.completed = false; + + if (handleImmediately) + { + result.continuationAction = result.Continuation; + asyncOperation.completed += result.continuationAction; + + if (cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (ResourceRequestConfiguredSource)state; + source.core.TrySetCanceled(source.cancellationToken); + }, result); + } + } TaskTracker.TrackActiveTask(result, 3); @@ -578,11 +422,33 @@ bool TryReturn() { TaskTracker.RemoveTracking(this); core.Reset(); + asyncOperation.completed -= continuationAction; asyncOperation = default; progress = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } + + void Continuation(AsyncOperation _) + { + if (completed) + { + TryReturn(); + } + else + { + completed = true; + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + } + else + { + core.TrySetResult(asyncOperation.asset); + } + } + } } #endregion @@ -603,22 +469,15 @@ public static AssetBundleRequestAwaiter GetAwaiter(this AssetBundleRequest async public static UniTask WithCancellation(this AssetBundleRequest asyncOperation, bool handleImmediately, CancellationToken cancellationToken) { - if (handleImmediately) - { - Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); - if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); - if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); - return new UniTask(AssetBundleRequestCallbackHandlerSource.Create(asyncOperation, cancellationToken, out var token), token); - } - return ToUniTask(asyncOperation, cancellationToken: cancellationToken); + return ToUniTask(asyncOperation, handleImmediately: handleImmediately, cancellationToken: cancellationToken); } - public static UniTask ToUniTask(this AssetBundleRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask ToUniTask(this AssetBundleRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, bool handleImmediately = false, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); - return new UniTask(AssetBundleRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + return new UniTask(AssetBundleRequestConfiguredSource.Create(asyncOperation, timing, progress, handleImmediately, cancellationToken, out var token), token); } public struct AssetBundleRequestAwaiter : ICriticalNotifyCompletion @@ -665,29 +524,32 @@ public void UnsafeOnCompleted(Action continuation) } } - sealed class AssetBundleRequestCallbackHandlerSource : IUniTaskSource, ITaskPoolNode + sealed class AssetBundleRequestConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode { - static TaskPool pool; - AssetBundleRequestCallbackHandlerSource nextNode; - public ref AssetBundleRequestCallbackHandlerSource NextNode => ref nextNode; + static TaskPool pool; + AssetBundleRequestConfiguredSource nextNode; + public ref AssetBundleRequestConfiguredSource NextNode => ref nextNode; - static AssetBundleRequestCallbackHandlerSource() + static AssetBundleRequestConfiguredSource() { - TaskPool.RegisterSizeGetter(typeof(AssetBundleRequestCallbackHandlerSource), () => pool.Size); + TaskPool.RegisterSizeGetter(typeof(AssetBundleRequestConfiguredSource), () => pool.Size); } AssetBundleRequest asyncOperation; IProgress progress; CancellationToken cancellationToken; CancellationTokenRegistration cancellationTokenRegistration; + bool completed; UniTaskCompletionSourceCore core; - AssetBundleRequestCallbackHandlerSource() + Action continuationAction; + + AssetBundleRequestConfiguredSource() { } - public static IUniTaskSource Create(AssetBundleRequest asyncOperation, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -696,25 +558,33 @@ static AssetBundleRequestCallbackHandlerSource() if (!pool.TryPop(out var result)) { - result = new AssetBundleRequestCallbackHandlerSource(); + result = new AssetBundleRequestConfiguredSource(); } result.asyncOperation = asyncOperation; + result.progress = progress; result.cancellationToken = cancellationToken; + result.completed = false; - asyncOperation.completed += result.AsyncOperationCompletedHandler; - - if (cancellationToken.CanBeCanceled) + if (handleImmediately) { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + result.continuationAction = result.Continuation; + asyncOperation.completed += result.continuationAction; + + if (cancellationToken.CanBeCanceled) { - var source = (AssetBundleRequestCallbackHandlerSource)state; - source.core.TrySetCanceled(source.cancellationToken); - }, result); + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (AssetBundleRequestConfiguredSource)state; + source.core.TrySetCanceled(source.cancellationToken); + }, result); + } } TaskTracker.TrackActiveTask(result, 3); + PlayerLoopHelper.AddAction(timing, result); + token = result.core.Version; return result; } @@ -751,108 +621,13 @@ public void OnCompleted(Action continuation, object state, short token) core.OnCompleted(continuation, state, token); } - bool TryReturn() + public bool MoveNext() { - TaskTracker.RemoveTracking(this); - core.Reset(); - asyncOperation.completed -= AsyncOperationCompletedHandler; - asyncOperation = default; - progress = default; - cancellationToken = default; - cancellationTokenRegistration.Dispose(); - return pool.TryPush(this); - } - - void AsyncOperationCompletedHandler(AsyncOperation _) - { - core.TrySetResult(asyncOperation.asset); - } - } - - sealed class AssetBundleRequestConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode - { - static TaskPool pool; - AssetBundleRequestConfiguredSource nextNode; - public ref AssetBundleRequestConfiguredSource NextNode => ref nextNode; - - static AssetBundleRequestConfiguredSource() - { - TaskPool.RegisterSizeGetter(typeof(AssetBundleRequestConfiguredSource), () => pool.Size); - } - - AssetBundleRequest asyncOperation; - IProgress progress; - CancellationToken cancellationToken; - - UniTaskCompletionSourceCore core; - - AssetBundleRequestConfiguredSource() - { - } - - public static IUniTaskSource Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) - { - if (cancellationToken.IsCancellationRequested) - { - return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); - } - - if (!pool.TryPop(out var result)) - { - result = new AssetBundleRequestConfiguredSource(); - } - - result.asyncOperation = asyncOperation; - result.progress = progress; - result.cancellationToken = cancellationToken; - - TaskTracker.TrackActiveTask(result, 3); - - PlayerLoopHelper.AddAction(timing, result); - - token = result.core.Version; - return result; - } - - public UnityEngine.Object GetResult(short token) - { - try - { - return core.GetResult(token); - } - finally - { - TryReturn(); - } - } - - void IUniTaskSource.GetResult(short token) - { - GetResult(token); - } - - public UniTaskStatus GetStatus(short token) - { - return core.GetStatus(token); - } - - public UniTaskStatus UnsafeGetStatus() - { - return core.UnsafeGetStatus(); - } - - public void OnCompleted(Action continuation, object state, short token) - { - core.OnCompleted(continuation, state, token); - } - - public bool MoveNext() - { - if (cancellationToken.IsCancellationRequested) - { - core.TrySetCanceled(cancellationToken); - return false; - } + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + return false; + } if (progress != null) { @@ -872,11 +647,33 @@ bool TryReturn() { TaskTracker.RemoveTracking(this); core.Reset(); + asyncOperation.completed -= continuationAction; asyncOperation = default; progress = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } + + void Continuation(AsyncOperation _) + { + if (completed) + { + TryReturn(); + } + else + { + completed = true; + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + } + else + { + core.TrySetResult(asyncOperation.asset); + } + } + } } #endregion @@ -898,22 +695,15 @@ public static UniTask WithCancellation(this AssetBundleCreateReques public static UniTask WithCancellation(this AssetBundleCreateRequest asyncOperation, bool handleImmediately, CancellationToken cancellationToken) { - if (handleImmediately) - { - Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); - if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); - if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.assetBundle); - return new UniTask(AssetBundleCreateRequestCallbackHandlerSource.Create(asyncOperation, cancellationToken, out var token), token); - } - return ToUniTask(asyncOperation, cancellationToken: cancellationToken); + return ToUniTask(asyncOperation, handleImmediately: handleImmediately, cancellationToken: cancellationToken); } - public static UniTask ToUniTask(this AssetBundleCreateRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask ToUniTask(this AssetBundleCreateRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, bool handleImmediately = false, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.assetBundle); - return new UniTask(AssetBundleCreateRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + return new UniTask(AssetBundleCreateRequestConfiguredSource.Create(asyncOperation, timing, progress, handleImmediately, cancellationToken, out var token), token); } public struct AssetBundleCreateRequestAwaiter : ICriticalNotifyCompletion @@ -960,110 +750,6 @@ public void UnsafeOnCompleted(Action continuation) } } - sealed class AssetBundleCreateRequestCallbackHandlerSource : IUniTaskSource, ITaskPoolNode - { - static TaskPool pool; - AssetBundleCreateRequestCallbackHandlerSource nextNode; - public ref AssetBundleCreateRequestCallbackHandlerSource NextNode => ref nextNode; - - static AssetBundleCreateRequestCallbackHandlerSource() - { - TaskPool.RegisterSizeGetter(typeof(AssetBundleCreateRequestCallbackHandlerSource), () => pool.Size); - } - - AssetBundleCreateRequest asyncOperation; - IProgress progress; - CancellationToken cancellationToken; - CancellationTokenRegistration cancellationTokenRegistration; - - UniTaskCompletionSourceCore core; - - AssetBundleCreateRequestCallbackHandlerSource() - { - } - - public static IUniTaskSource Create(AssetBundleCreateRequest asyncOperation, CancellationToken cancellationToken, out short token) - { - if (cancellationToken.IsCancellationRequested) - { - return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); - } - - if (!pool.TryPop(out var result)) - { - result = new AssetBundleCreateRequestCallbackHandlerSource(); - } - - result.asyncOperation = asyncOperation; - result.cancellationToken = cancellationToken; - - asyncOperation.completed += result.AsyncOperationCompletedHandler; - - if (cancellationToken.CanBeCanceled) - { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var source = (AssetBundleCreateRequestCallbackHandlerSource)state; - source.core.TrySetCanceled(source.cancellationToken); - }, result); - } - - TaskTracker.TrackActiveTask(result, 3); - - token = result.core.Version; - return result; - } - - public AssetBundle GetResult(short token) - { - try - { - return core.GetResult(token); - } - finally - { - TryReturn(); - } - } - - void IUniTaskSource.GetResult(short token) - { - GetResult(token); - } - - public UniTaskStatus GetStatus(short token) - { - return core.GetStatus(token); - } - - public UniTaskStatus UnsafeGetStatus() - { - return core.UnsafeGetStatus(); - } - - public void OnCompleted(Action continuation, object state, short token) - { - core.OnCompleted(continuation, state, token); - } - - bool TryReturn() - { - TaskTracker.RemoveTracking(this); - core.Reset(); - asyncOperation.completed -= AsyncOperationCompletedHandler; - asyncOperation = default; - progress = default; - cancellationToken = default; - cancellationTokenRegistration.Dispose(); - return pool.TryPush(this); - } - - void AsyncOperationCompletedHandler(AsyncOperation _) - { - core.TrySetResult(asyncOperation.assetBundle); - } - } - sealed class AssetBundleCreateRequestConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode { static TaskPool pool; @@ -1078,14 +764,18 @@ static AssetBundleCreateRequestConfiguredSource() AssetBundleCreateRequest asyncOperation; IProgress progress; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; + bool completed; UniTaskCompletionSourceCore core; + Action continuationAction; + AssetBundleCreateRequestConfiguredSource() { } - public static IUniTaskSource Create(AssetBundleCreateRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(AssetBundleCreateRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -1100,6 +790,22 @@ public static IUniTaskSource Create(AssetBundleCreateRequest asyncO result.asyncOperation = asyncOperation; result.progress = progress; result.cancellationToken = cancellationToken; + result.completed = false; + + if (handleImmediately) + { + result.continuationAction = result.Continuation; + asyncOperation.completed += result.continuationAction; + + if (cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (AssetBundleCreateRequestConfiguredSource)state; + source.core.TrySetCanceled(source.cancellationToken); + }, result); + } + } TaskTracker.TrackActiveTask(result, 3); @@ -1167,11 +873,33 @@ bool TryReturn() { TaskTracker.RemoveTracking(this); core.Reset(); + asyncOperation.completed -= continuationAction; asyncOperation = default; progress = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } + + void Continuation(AsyncOperation _) + { + if (completed) + { + TryReturn(); + } + else + { + completed = true; + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + } + else + { + core.TrySetResult(asyncOperation.assetBundle); + } + } + } } #endregion @@ -1193,24 +921,10 @@ public static UniTask WithCancellation(this UnityWebRequestAsyn public static UniTask WithCancellation(this UnityWebRequestAsyncOperation asyncOperation, bool handleImmediately, CancellationToken cancellationToken) { - if (handleImmediately) - { - Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); - if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); - if (asyncOperation.isDone) - { - if (asyncOperation.webRequest.IsError()) - { - return UniTask.FromException(new UnityWebRequestException(asyncOperation.webRequest)); - } - return UniTask.FromResult(asyncOperation.webRequest); - } - return new UniTask(UnityWebRequestAsyncOperationCallbackHandlerSource.Create(asyncOperation, cancellationToken, out var token), token); - } - return ToUniTask(asyncOperation, cancellationToken: cancellationToken); + return ToUniTask(asyncOperation, handleImmediately: handleImmediately, cancellationToken: cancellationToken); } - public static UniTask ToUniTask(this UnityWebRequestAsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask ToUniTask(this UnityWebRequestAsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, bool handleImmediately = false, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); @@ -1222,7 +936,7 @@ public static UniTask WithCancellation(this UnityWebRequestAsyn } return UniTask.FromResult(asyncOperation.webRequest); } - return new UniTask(UnityWebRequestAsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + return new UniTask(UnityWebRequestAsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, handleImmediately, cancellationToken, out var token), token); } public struct UnityWebRequestAsyncOperationAwaiter : ICriticalNotifyCompletion @@ -1277,118 +991,6 @@ public void UnsafeOnCompleted(Action continuation) } } - sealed class UnityWebRequestAsyncOperationCallbackHandlerSource : IUniTaskSource, ITaskPoolNode - { - static TaskPool pool; - UnityWebRequestAsyncOperationCallbackHandlerSource nextNode; - public ref UnityWebRequestAsyncOperationCallbackHandlerSource NextNode => ref nextNode; - - static UnityWebRequestAsyncOperationCallbackHandlerSource() - { - TaskPool.RegisterSizeGetter(typeof(UnityWebRequestAsyncOperationCallbackHandlerSource), () => pool.Size); - } - - UnityWebRequestAsyncOperation asyncOperation; - IProgress progress; - CancellationToken cancellationToken; - CancellationTokenRegistration cancellationTokenRegistration; - - UniTaskCompletionSourceCore core; - - UnityWebRequestAsyncOperationCallbackHandlerSource() - { - } - - public static IUniTaskSource Create(UnityWebRequestAsyncOperation asyncOperation, CancellationToken cancellationToken, out short token) - { - if (cancellationToken.IsCancellationRequested) - { - return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); - } - - if (!pool.TryPop(out var result)) - { - result = new UnityWebRequestAsyncOperationCallbackHandlerSource(); - } - - result.asyncOperation = asyncOperation; - result.cancellationToken = cancellationToken; - - asyncOperation.completed += result.AsyncOperationCompletedHandler; - - if (cancellationToken.CanBeCanceled) - { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var source = (UnityWebRequestAsyncOperationCallbackHandlerSource)state; - source.asyncOperation.webRequest.Abort(); - source.core.TrySetCanceled(source.cancellationToken); - }, result); - } - - TaskTracker.TrackActiveTask(result, 3); - - token = result.core.Version; - return result; - } - - public UnityWebRequest GetResult(short token) - { - try - { - return core.GetResult(token); - } - finally - { - TryReturn(); - } - } - - void IUniTaskSource.GetResult(short token) - { - GetResult(token); - } - - public UniTaskStatus GetStatus(short token) - { - return core.GetStatus(token); - } - - public UniTaskStatus UnsafeGetStatus() - { - return core.UnsafeGetStatus(); - } - - public void OnCompleted(Action continuation, object state, short token) - { - core.OnCompleted(continuation, state, token); - } - - bool TryReturn() - { - TaskTracker.RemoveTracking(this); - core.Reset(); - asyncOperation.completed -= AsyncOperationCompletedHandler; - asyncOperation = default; - progress = default; - cancellationToken = default; - cancellationTokenRegistration.Dispose(); - return pool.TryPush(this); - } - - void AsyncOperationCompletedHandler(AsyncOperation _) - { - if (asyncOperation.webRequest.IsError()) - { - core.TrySetException(new UnityWebRequestException(asyncOperation.webRequest)); - } - else - { - core.TrySetResult(asyncOperation.webRequest); - } - } - } - sealed class UnityWebRequestAsyncOperationConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode { static TaskPool pool; @@ -1403,14 +1005,18 @@ static UnityWebRequestAsyncOperationConfiguredSource() UnityWebRequestAsyncOperation asyncOperation; IProgress progress; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; + bool completed; UniTaskCompletionSourceCore core; + Action continuationAction; + UnityWebRequestAsyncOperationConfiguredSource() { } - public static IUniTaskSource Create(UnityWebRequestAsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(UnityWebRequestAsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -1425,6 +1031,23 @@ public static IUniTaskSource Create(UnityWebRequestAsyncOperati result.asyncOperation = asyncOperation; result.progress = progress; result.cancellationToken = cancellationToken; + result.completed = false; + + if (handleImmediately) + { + result.continuationAction = result.Continuation; + asyncOperation.completed += result.continuationAction; + + if (cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (UnityWebRequestAsyncOperationConfiguredSource)state; + source.asyncOperation.webRequest.Abort(); + source.core.TrySetCanceled(source.cancellationToken); + }, result); + } + } TaskTracker.TrackActiveTask(result, 3); @@ -1500,11 +1123,37 @@ bool TryReturn() { TaskTracker.RemoveTracking(this); core.Reset(); + asyncOperation.completed -= continuationAction; asyncOperation = default; progress = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } + + void Continuation(AsyncOperation _) + { + if (completed) + { + TryReturn(); + } + else + { + completed = true; + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + } + else if (asyncOperation.webRequest.IsError()) + { + core.TrySetException(new UnityWebRequestException(asyncOperation.webRequest)); + } + else + { + core.TrySetResult(asyncOperation.webRequest); + } + } + } } #endregion diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt index f74fa0b1..105893dd 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt @@ -56,28 +56,10 @@ namespace Cysharp.Threading.Tasks public static <#= ToUniTaskReturnType(t.returnType) #> WithCancellation(this <#= t.typeName #> asyncOperation, bool handleImmediately, CancellationToken cancellationToken) { - if (handleImmediately) - { - Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); - if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<#= IsVoid(t) ? "" : "<" + t.returnType + ">" #>(cancellationToken); -<# if(IsUnityWebRequest(t)) { #> - if (asyncOperation.isDone) - { - if (asyncOperation.webRequest.IsError()) - { - return UniTask.FromException(new UnityWebRequestException(asyncOperation.webRequest)); - } - return UniTask.FromResult(asyncOperation.webRequest); - } -<# } else { #> - if (asyncOperation.isDone) return <#= IsVoid(t) ? "UniTask.CompletedTask" : $"UniTask.FromResult(asyncOperation.{t.returnField})" #>; -<# } #> - return new <#= ToUniTaskReturnType(t.returnType) #>(<#= t.typeName #>CallbackHandlerSource.Create(asyncOperation, cancellationToken, out var token), token); - } - return ToUniTask(asyncOperation, cancellationToken: cancellationToken); + return ToUniTask(asyncOperation, handleImmediately: handleImmediately, cancellationToken: cancellationToken); } - public static <#= ToUniTaskReturnType(t.returnType) #> ToUniTask(this <#= t.typeName #> asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static <#= ToUniTaskReturnType(t.returnType) #> ToUniTask(this <#= t.typeName #> asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, bool handleImmediately = false, CancellationToken cancellationToken = default(CancellationToken)) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<#= IsVoid(t) ? "" : "<" + t.returnType + ">" #>(cancellationToken); @@ -93,7 +75,7 @@ namespace Cysharp.Threading.Tasks <# } else { #> if (asyncOperation.isDone) return <#= IsVoid(t) ? "UniTask.CompletedTask" : $"UniTask.FromResult(asyncOperation.{t.returnField})" #>; <# } #> - return new <#= ToUniTaskReturnType(t.returnType) #>(<#= t.typeName #>ConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + return new <#= ToUniTaskReturnType(t.returnType) #>(<#= t.typeName #>ConfiguredSource.Create(asyncOperation, timing, progress, handleImmediately, cancellationToken, out var token), token); } public struct <#= t.typeName #>Awaiter : ICriticalNotifyCompletion @@ -160,130 +142,6 @@ namespace Cysharp.Threading.Tasks } } - sealed class <#= t.typeName #>CallbackHandlerSource : <#= ToIUniTaskSourceReturnType(t.returnType) #>, ITaskPoolNode<<#= t.typeName #>CallbackHandlerSource> - { - static TaskPool<<#= t.typeName #>CallbackHandlerSource> pool; - <#= t.typeName #>CallbackHandlerSource nextNode; - public ref <#= t.typeName #>CallbackHandlerSource NextNode => ref nextNode; - - static <#= t.typeName #>CallbackHandlerSource() - { - TaskPool.RegisterSizeGetter(typeof(<#= t.typeName #>CallbackHandlerSource), () => pool.Size); - } - - <#= t.typeName #> asyncOperation; - IProgress progress; - CancellationToken cancellationToken; - CancellationTokenRegistration cancellationTokenRegistration; - - UniTaskCompletionSourceCore<<#= IsVoid(t) ? "AsyncUnit" : t.returnType #>> core; - - <#= t.typeName #>CallbackHandlerSource() - { - } - - public static <#= ToIUniTaskSourceReturnType(t.returnType) #> Create(<#= t.typeName #> asyncOperation, CancellationToken cancellationToken, out short token) - { - if (cancellationToken.IsCancellationRequested) - { - return AutoResetUniTaskCompletionSource<#= IsVoid(t) ? "" : $"<{t.returnType}>" #>.CreateFromCanceled(cancellationToken, out token); - } - - if (!pool.TryPop(out var result)) - { - result = new <#= t.typeName #>CallbackHandlerSource(); - } - - result.asyncOperation = asyncOperation; - result.cancellationToken = cancellationToken; - - asyncOperation.completed += result.AsyncOperationCompletedHandler; - - if (cancellationToken.CanBeCanceled) - { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var source = (<#= t.typeName #>CallbackHandlerSource)state; -<# if(IsUnityWebRequest(t)) { #> - source.asyncOperation.webRequest.Abort(); -<# } #> - source.core.TrySetCanceled(source.cancellationToken); - }, result); - } - - TaskTracker.TrackActiveTask(result, 3); - - token = result.core.Version; - return result; - } - - public <#= t.returnType #> GetResult(short token) - { - try - { -<# if (!IsVoid(t)) { #> - return core.GetResult(token); -<# } else { #> - core.GetResult(token); -<# } #> - } - finally - { - TryReturn(); - } - } - -<# if (!IsVoid(t)) { #> - void IUniTaskSource.GetResult(short token) - { - GetResult(token); - } -<# } #> - - public UniTaskStatus GetStatus(short token) - { - return core.GetStatus(token); - } - - public UniTaskStatus UnsafeGetStatus() - { - return core.UnsafeGetStatus(); - } - - public void OnCompleted(Action continuation, object state, short token) - { - core.OnCompleted(continuation, state, token); - } - - bool TryReturn() - { - TaskTracker.RemoveTracking(this); - core.Reset(); - asyncOperation.completed -= AsyncOperationCompletedHandler; - asyncOperation = default; - progress = default; - cancellationToken = default; - cancellationTokenRegistration.Dispose(); - return pool.TryPush(this); - } - - void AsyncOperationCompletedHandler(AsyncOperation _) - { -<# if(IsUnityWebRequest(t)) { #> - if (asyncOperation.webRequest.IsError()) - { - core.TrySetException(new UnityWebRequestException(asyncOperation.webRequest)); - } - else - { - core.TrySetResult(asyncOperation.webRequest); - } -<# } else { #> - core.TrySetResult(<#= IsVoid(t) ? "AsyncUnit.Default" : $"asyncOperation.{t.returnField}" #>); -<# } #> - } - } - sealed class <#= t.typeName #>ConfiguredSource : <#= ToIUniTaskSourceReturnType(t.returnType) #>, IPlayerLoopItem, ITaskPoolNode<<#= t.typeName #>ConfiguredSource> { static TaskPool<<#= t.typeName #>ConfiguredSource> pool; @@ -298,14 +156,18 @@ namespace Cysharp.Threading.Tasks <#= t.typeName #> asyncOperation; IProgress progress; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; + bool completed; UniTaskCompletionSourceCore<<#= IsVoid(t) ? "AsyncUnit" : t.returnType #>> core; + Action continuationAction; + <#= t.typeName #>ConfiguredSource() { } - public static <#= ToIUniTaskSourceReturnType(t.returnType) #> Create(<#= t.typeName #> asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + public static <#= ToIUniTaskSourceReturnType(t.returnType) #> Create(<#= t.typeName #> asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -320,6 +182,25 @@ namespace Cysharp.Threading.Tasks result.asyncOperation = asyncOperation; result.progress = progress; result.cancellationToken = cancellationToken; + result.completed = false; + + if (handleImmediately) + { + result.continuationAction = result.Continuation; + asyncOperation.completed += result.continuationAction; + + if (cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (<#= t.typeName #>ConfiguredSource)state; +<# if(IsUnityWebRequest(t)) { #> + source.asyncOperation.webRequest.Abort(); +<# } #> + source.core.TrySetCanceled(source.cancellationToken); + }, result); + } + } TaskTracker.TrackActiveTask(result, 3); @@ -407,11 +288,44 @@ namespace Cysharp.Threading.Tasks { TaskTracker.RemoveTracking(this); core.Reset(); + asyncOperation.completed -= continuationAction; asyncOperation = default; progress = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } + + void Continuation(AsyncOperation _) + { + if (completed) + { + TryReturn(); + } + else + { + completed = true; + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + } +<# if(IsUnityWebRequest(t)) { #> + else if (asyncOperation.webRequest.IsError()) + { + core.TrySetException(new UnityWebRequestException(asyncOperation.webRequest)); + } + else + { + core.TrySetResult(asyncOperation.webRequest); + } +<# } else { #> + else + { + core.TrySetResult(<#= IsVoid(t) ? "AsyncUnit.Default" : $"asyncOperation.{t.returnField}" #>); + } +<# } #> + } + } } #endregion From 0a203c8db9fc0bf9b68a3c71a9b1c6cd545672fe Mon Sep 17 00:00:00 2001 From: hadashiA Date: Thu, 2 Nov 2023 09:58:52 +0900 Subject: [PATCH 08/13] Cache Action in advance --- .../Plugins/UniTask/Runtime/UnityAsyncExtensions.cs | 10 +++++----- .../Plugins/UniTask/Runtime/UnityAsyncExtensions.tt | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs index dc24ad07..915d55d2 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs @@ -102,6 +102,7 @@ static AsyncOperationConfiguredSource() AsyncOperationConfiguredSource() { + continuationAction = Continuation; } public static IUniTaskSource Create(AsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) @@ -123,7 +124,6 @@ public static IUniTaskSource Create(AsyncOperation asyncOperation, PlayerLoopTim if (handleImmediately) { - result.continuationAction = result.Continuation; asyncOperation.completed += result.continuationAction; if (cancellationToken.CanBeCanceled) @@ -322,6 +322,7 @@ static ResourceRequestConfiguredSource() ResourceRequestConfiguredSource() { + continuationAction = Continuation; } public static IUniTaskSource Create(ResourceRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) @@ -343,7 +344,6 @@ static ResourceRequestConfiguredSource() if (handleImmediately) { - result.continuationAction = result.Continuation; asyncOperation.completed += result.continuationAction; if (cancellationToken.CanBeCanceled) @@ -547,6 +547,7 @@ static AssetBundleRequestConfiguredSource() AssetBundleRequestConfiguredSource() { + continuationAction = Continuation; } public static IUniTaskSource Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) @@ -568,7 +569,6 @@ static AssetBundleRequestConfiguredSource() if (handleImmediately) { - result.continuationAction = result.Continuation; asyncOperation.completed += result.continuationAction; if (cancellationToken.CanBeCanceled) @@ -773,6 +773,7 @@ static AssetBundleCreateRequestConfiguredSource() AssetBundleCreateRequestConfiguredSource() { + continuationAction = Continuation; } public static IUniTaskSource Create(AssetBundleCreateRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) @@ -794,7 +795,6 @@ public static IUniTaskSource Create(AssetBundleCreateRequest asyncO if (handleImmediately) { - result.continuationAction = result.Continuation; asyncOperation.completed += result.continuationAction; if (cancellationToken.CanBeCanceled) @@ -1014,6 +1014,7 @@ static UnityWebRequestAsyncOperationConfiguredSource() UnityWebRequestAsyncOperationConfiguredSource() { + continuationAction = Continuation; } public static IUniTaskSource Create(UnityWebRequestAsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) @@ -1035,7 +1036,6 @@ public static IUniTaskSource Create(UnityWebRequestAsyncOperati if (handleImmediately) { - result.continuationAction = result.Continuation; asyncOperation.completed += result.continuationAction; if (cancellationToken.CanBeCanceled) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt index 105893dd..e3e57d43 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt @@ -165,6 +165,7 @@ namespace Cysharp.Threading.Tasks <#= t.typeName #>ConfiguredSource() { + continuationAction = Continuation; } public static <#= ToIUniTaskSourceReturnType(t.returnType) #> Create(<#= t.typeName #> asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) @@ -186,7 +187,6 @@ namespace Cysharp.Threading.Tasks if (handleImmediately) { - result.continuationAction = result.Continuation; asyncOperation.completed += result.continuationAction; if (cancellationToken.CanBeCanceled) From 39cf81d2ab57fa616716515b9ba4b1fcda9915cf Mon Sep 17 00:00:00 2001 From: hadashiA Date: Thu, 2 Nov 2023 10:29:42 +0900 Subject: [PATCH 09/13] Add test for unity AsyncOperation --- .../Assets/Tests/AsyncOperationTest.cs | 42 ++++ .../Assets/Tests/AsyncOperationTest.cs.meta | 3 + src/UniTask/Assets/Tests/Resources.meta | 8 + .../Assets/Tests/Resources/sample_texture.png | Bin 0 -> 16432 bytes .../Tests/Resources/sample_texture.png.meta | 208 ++++++++++++++++++ 5 files changed, 261 insertions(+) create mode 100644 src/UniTask/Assets/Tests/AsyncOperationTest.cs create mode 100644 src/UniTask/Assets/Tests/AsyncOperationTest.cs.meta create mode 100644 src/UniTask/Assets/Tests/Resources.meta create mode 100644 src/UniTask/Assets/Tests/Resources/sample_texture.png create mode 100644 src/UniTask/Assets/Tests/Resources/sample_texture.png.meta diff --git a/src/UniTask/Assets/Tests/AsyncOperationTest.cs b/src/UniTask/Assets/Tests/AsyncOperationTest.cs new file mode 100644 index 00000000..fb292183 --- /dev/null +++ b/src/UniTask/Assets/Tests/AsyncOperationTest.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Threading; +using Cysharp.Threading.Tasks; +using FluentAssertions; +using NUnit.Framework; +using UnityEngine; +using UnityEngine.TestTools; + +#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6)) +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +namespace Cysharp.Threading.TasksTests +{ + public class AsyncOperationTest + { + [UnityTest] + public IEnumerator ResourcesLoad_CancelOnPlayerLoop() => UniTask.ToCoroutine(async () => + { + var cts = new CancellationTokenSource(); + var task = Resources.LoadAsync("sample_texture").ToUniTask(cancellationToken: cts.Token, handleImmediately: false); + + cts.Cancel(); + task.Status.Should().Be(UniTaskStatus.Pending); + + await UniTask.NextFrame(); + task.Status.Should().Be(UniTaskStatus.Canceled); + }); + + [Test] + public void ResourcesLoad_CancelImmediately() + { + { + var cts = new CancellationTokenSource(); + var task = Resources.LoadAsync("sample_texture").ToUniTask(cancellationToken: cts.Token, handleImmediately: true); + + cts.Cancel(); + task.Status.Should().Be(UniTaskStatus.Canceled); + } + } + } +} +#endif diff --git a/src/UniTask/Assets/Tests/AsyncOperationTest.cs.meta b/src/UniTask/Assets/Tests/AsyncOperationTest.cs.meta new file mode 100644 index 00000000..fed3f0d8 --- /dev/null +++ b/src/UniTask/Assets/Tests/AsyncOperationTest.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 295d574a16494d6aa4d02fcb32179e39 +timeCreated: 1698887128 \ No newline at end of file diff --git a/src/UniTask/Assets/Tests/Resources.meta b/src/UniTask/Assets/Tests/Resources.meta new file mode 100644 index 00000000..d5685591 --- /dev/null +++ b/src/UniTask/Assets/Tests/Resources.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 8d82913edf6ac48aca30f66ae9ba42d6 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/UniTask/Assets/Tests/Resources/sample_texture.png b/src/UniTask/Assets/Tests/Resources/sample_texture.png new file mode 100644 index 0000000000000000000000000000000000000000..2da89097b331de4063b57842c7a775cb47399716 GIT binary patch literal 16432 zcmX|o2|Uw({QqoNIfe?ki$p18&Iy$}MQ-IRnH+_Txos);5&FuU-&o`brCf7N2g;C1 zh_P?VZEjPp|9ky@|6h-XM-LzG<8{8@ujBbnxMpt5%O%PMfk1doO$@9c5U2wL!e)Gc z6MTt8sPKXR*aI(_+8hA?A`aY+2frWmH@O}Nfp8yU{jgDKQ9IzHIL6QcW9^5=U~k^{ zfLy%ocgy3Lk=K0>tlxc~z+?K>>NCx+lOd2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d?eXNdfBY?KC#?G?FT92lK296XwmE|5PM*yL# zIRFPcz!E%2N>cy-SC!!?oZ(+WOd%C$jwB_pupf8gOHYsvqTQ^CRXF Date: Thu, 2 Nov 2023 12:01:51 +0900 Subject: [PATCH 10/13] Use AsyncOperation.completed callback usually --- .../UniTask/Runtime/UnityAsyncExtensions.cs | 162 ++++++++++-------- .../UniTask/Runtime/UnityAsyncExtensions.tt | 34 ++-- 2 files changed, 104 insertions(+), 92 deletions(-) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs index 915d55d2..fb84fca2 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs @@ -26,17 +26,17 @@ public static UniTask WithCancellation(this AsyncOperation asyncOperation, Cance return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask WithCancellation(this AsyncOperation asyncOperation, bool handleImmediately, CancellationToken cancellationToken) + public static UniTask WithCancellation(this AsyncOperation asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) { - return ToUniTask(asyncOperation, handleImmediately: handleImmediately, cancellationToken: cancellationToken); + return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); } - public static UniTask ToUniTask(this AsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, bool handleImmediately = false, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask ToUniTask(this AsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.CompletedTask; - return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, handleImmediately, cancellationToken, out var token), token); + return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); } public struct AsyncOperationAwaiter : ICriticalNotifyCompletion @@ -105,7 +105,7 @@ static AsyncOperationConfiguredSource() continuationAction = Continuation; } - public static IUniTaskSource Create(AsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(AsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -121,19 +121,16 @@ public static IUniTaskSource Create(AsyncOperation asyncOperation, PlayerLoopTim result.progress = progress; result.cancellationToken = cancellationToken; result.completed = false; + + asyncOperation.completed += result.continuationAction; - if (handleImmediately) + if (cancelImmediately && cancellationToken.CanBeCanceled) { - asyncOperation.completed += result.continuationAction; - - if (cancellationToken.CanBeCanceled) + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var source = (AsyncOperationConfiguredSource)state; - source.core.TrySetCanceled(source.cancellationToken); - }, result); - } + var source = (AsyncOperationConfiguredSource)state; + source.core.TrySetCanceled(source.cancellationToken); + }, result); } TaskTracker.TrackActiveTask(result, 3); @@ -180,6 +177,11 @@ public bool MoveNext() return false; } + if (asyncOperation == null) + { + return false; + } + if (progress != null) { progress.Report(asyncOperation.progress); @@ -242,17 +244,17 @@ public static ResourceRequestAwaiter GetAwaiter(this ResourceRequest asyncOperat return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask WithCancellation(this ResourceRequest asyncOperation, bool handleImmediately, CancellationToken cancellationToken) + public static UniTask WithCancellation(this ResourceRequest asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) { - return ToUniTask(asyncOperation, handleImmediately: handleImmediately, cancellationToken: cancellationToken); + return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); } - public static UniTask ToUniTask(this ResourceRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, bool handleImmediately = false, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask ToUniTask(this ResourceRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); - return new UniTask(ResourceRequestConfiguredSource.Create(asyncOperation, timing, progress, handleImmediately, cancellationToken, out var token), token); + return new UniTask(ResourceRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); } public struct ResourceRequestAwaiter : ICriticalNotifyCompletion @@ -325,7 +327,7 @@ static ResourceRequestConfiguredSource() continuationAction = Continuation; } - public static IUniTaskSource Create(ResourceRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(ResourceRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -341,19 +343,16 @@ static ResourceRequestConfiguredSource() result.progress = progress; result.cancellationToken = cancellationToken; result.completed = false; + + asyncOperation.completed += result.continuationAction; - if (handleImmediately) + if (cancelImmediately && cancellationToken.CanBeCanceled) { - asyncOperation.completed += result.continuationAction; - - if (cancellationToken.CanBeCanceled) + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var source = (ResourceRequestConfiguredSource)state; - source.core.TrySetCanceled(source.cancellationToken); - }, result); - } + var source = (ResourceRequestConfiguredSource)state; + source.core.TrySetCanceled(source.cancellationToken); + }, result); } TaskTracker.TrackActiveTask(result, 3); @@ -404,6 +403,11 @@ public bool MoveNext() return false; } + if (asyncOperation == null) + { + return false; + } + if (progress != null) { progress.Report(asyncOperation.progress); @@ -467,17 +471,17 @@ public static AssetBundleRequestAwaiter GetAwaiter(this AssetBundleRequest async return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask WithCancellation(this AssetBundleRequest asyncOperation, bool handleImmediately, CancellationToken cancellationToken) + public static UniTask WithCancellation(this AssetBundleRequest asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) { - return ToUniTask(asyncOperation, handleImmediately: handleImmediately, cancellationToken: cancellationToken); + return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); } - public static UniTask ToUniTask(this AssetBundleRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, bool handleImmediately = false, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask ToUniTask(this AssetBundleRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset); - return new UniTask(AssetBundleRequestConfiguredSource.Create(asyncOperation, timing, progress, handleImmediately, cancellationToken, out var token), token); + return new UniTask(AssetBundleRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); } public struct AssetBundleRequestAwaiter : ICriticalNotifyCompletion @@ -550,7 +554,7 @@ static AssetBundleRequestConfiguredSource() continuationAction = Continuation; } - public static IUniTaskSource Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -566,19 +570,16 @@ static AssetBundleRequestConfiguredSource() result.progress = progress; result.cancellationToken = cancellationToken; result.completed = false; + + asyncOperation.completed += result.continuationAction; - if (handleImmediately) + if (cancelImmediately && cancellationToken.CanBeCanceled) { - asyncOperation.completed += result.continuationAction; - - if (cancellationToken.CanBeCanceled) + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var source = (AssetBundleRequestConfiguredSource)state; - source.core.TrySetCanceled(source.cancellationToken); - }, result); - } + var source = (AssetBundleRequestConfiguredSource)state; + source.core.TrySetCanceled(source.cancellationToken); + }, result); } TaskTracker.TrackActiveTask(result, 3); @@ -629,6 +630,11 @@ public bool MoveNext() return false; } + if (asyncOperation == null) + { + return false; + } + if (progress != null) { progress.Report(asyncOperation.progress); @@ -693,17 +699,17 @@ public static UniTask WithCancellation(this AssetBundleCreateReques return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask WithCancellation(this AssetBundleCreateRequest asyncOperation, bool handleImmediately, CancellationToken cancellationToken) + public static UniTask WithCancellation(this AssetBundleCreateRequest asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) { - return ToUniTask(asyncOperation, handleImmediately: handleImmediately, cancellationToken: cancellationToken); + return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); } - public static UniTask ToUniTask(this AssetBundleCreateRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, bool handleImmediately = false, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask ToUniTask(this AssetBundleCreateRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.assetBundle); - return new UniTask(AssetBundleCreateRequestConfiguredSource.Create(asyncOperation, timing, progress, handleImmediately, cancellationToken, out var token), token); + return new UniTask(AssetBundleCreateRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); } public struct AssetBundleCreateRequestAwaiter : ICriticalNotifyCompletion @@ -776,7 +782,7 @@ static AssetBundleCreateRequestConfiguredSource() continuationAction = Continuation; } - public static IUniTaskSource Create(AssetBundleCreateRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(AssetBundleCreateRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -792,19 +798,16 @@ public static IUniTaskSource Create(AssetBundleCreateRequest asyncO result.progress = progress; result.cancellationToken = cancellationToken; result.completed = false; + + asyncOperation.completed += result.continuationAction; - if (handleImmediately) + if (cancelImmediately && cancellationToken.CanBeCanceled) { - asyncOperation.completed += result.continuationAction; - - if (cancellationToken.CanBeCanceled) + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var source = (AssetBundleCreateRequestConfiguredSource)state; - source.core.TrySetCanceled(source.cancellationToken); - }, result); - } + var source = (AssetBundleCreateRequestConfiguredSource)state; + source.core.TrySetCanceled(source.cancellationToken); + }, result); } TaskTracker.TrackActiveTask(result, 3); @@ -855,6 +858,11 @@ public bool MoveNext() return false; } + if (asyncOperation == null) + { + return false; + } + if (progress != null) { progress.Report(asyncOperation.progress); @@ -919,12 +927,12 @@ public static UniTask WithCancellation(this UnityWebRequestAsyn return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static UniTask WithCancellation(this UnityWebRequestAsyncOperation asyncOperation, bool handleImmediately, CancellationToken cancellationToken) + public static UniTask WithCancellation(this UnityWebRequestAsyncOperation asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) { - return ToUniTask(asyncOperation, handleImmediately: handleImmediately, cancellationToken: cancellationToken); + return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); } - public static UniTask ToUniTask(this UnityWebRequestAsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, bool handleImmediately = false, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask ToUniTask(this UnityWebRequestAsyncOperation asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); @@ -936,7 +944,7 @@ public static UniTask WithCancellation(this UnityWebRequestAsyn } return UniTask.FromResult(asyncOperation.webRequest); } - return new UniTask(UnityWebRequestAsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, handleImmediately, cancellationToken, out var token), token); + return new UniTask(UnityWebRequestAsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); } public struct UnityWebRequestAsyncOperationAwaiter : ICriticalNotifyCompletion @@ -1017,7 +1025,7 @@ static UnityWebRequestAsyncOperationConfiguredSource() continuationAction = Continuation; } - public static IUniTaskSource Create(UnityWebRequestAsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(UnityWebRequestAsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -1033,20 +1041,17 @@ public static IUniTaskSource Create(UnityWebRequestAsyncOperati result.progress = progress; result.cancellationToken = cancellationToken; result.completed = false; + + asyncOperation.completed += result.continuationAction; - if (handleImmediately) + if (cancelImmediately && cancellationToken.CanBeCanceled) { - asyncOperation.completed += result.continuationAction; - - if (cancellationToken.CanBeCanceled) + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var source = (UnityWebRequestAsyncOperationConfiguredSource)state; - source.asyncOperation.webRequest.Abort(); - source.core.TrySetCanceled(source.cancellationToken); - }, result); - } + var source = (UnityWebRequestAsyncOperationConfiguredSource)state; + source.asyncOperation.webRequest.Abort(); + source.core.TrySetCanceled(source.cancellationToken); + }, result); } TaskTracker.TrackActiveTask(result, 3); @@ -1098,6 +1103,11 @@ public bool MoveNext() return false; } + if (asyncOperation == null) + { + return false; + } + if (progress != null) { progress.Report(asyncOperation.progress); diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt index e3e57d43..86dd72af 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt @@ -54,12 +54,12 @@ namespace Cysharp.Threading.Tasks return ToUniTask(asyncOperation, cancellationToken: cancellationToken); } - public static <#= ToUniTaskReturnType(t.returnType) #> WithCancellation(this <#= t.typeName #> asyncOperation, bool handleImmediately, CancellationToken cancellationToken) + public static <#= ToUniTaskReturnType(t.returnType) #> WithCancellation(this <#= t.typeName #> asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) { - return ToUniTask(asyncOperation, handleImmediately: handleImmediately, cancellationToken: cancellationToken); + return ToUniTask(asyncOperation, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); } - public static <#= ToUniTaskReturnType(t.returnType) #> ToUniTask(this <#= t.typeName #> asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, bool handleImmediately = false, CancellationToken cancellationToken = default(CancellationToken)) + public static <#= ToUniTaskReturnType(t.returnType) #> ToUniTask(this <#= t.typeName #> asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<#= IsVoid(t) ? "" : "<" + t.returnType + ">" #>(cancellationToken); @@ -75,7 +75,7 @@ namespace Cysharp.Threading.Tasks <# } else { #> if (asyncOperation.isDone) return <#= IsVoid(t) ? "UniTask.CompletedTask" : $"UniTask.FromResult(asyncOperation.{t.returnField})" #>; <# } #> - return new <#= ToUniTaskReturnType(t.returnType) #>(<#= t.typeName #>ConfiguredSource.Create(asyncOperation, timing, progress, handleImmediately, cancellationToken, out var token), token); + return new <#= ToUniTaskReturnType(t.returnType) #>(<#= t.typeName #>ConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); } public struct <#= t.typeName #>Awaiter : ICriticalNotifyCompletion @@ -168,7 +168,7 @@ namespace Cysharp.Threading.Tasks continuationAction = Continuation; } - public static <#= ToIUniTaskSourceReturnType(t.returnType) #> Create(<#= t.typeName #> asyncOperation, PlayerLoopTiming timing, IProgress progress, bool handleImmediately, CancellationToken cancellationToken, out short token) + public static <#= ToIUniTaskSourceReturnType(t.returnType) #> Create(<#= t.typeName #> asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -184,22 +184,19 @@ namespace Cysharp.Threading.Tasks result.progress = progress; result.cancellationToken = cancellationToken; result.completed = false; + + asyncOperation.completed += result.continuationAction; - if (handleImmediately) + if (cancelImmediately && cancellationToken.CanBeCanceled) { - asyncOperation.completed += result.continuationAction; - - if (cancellationToken.CanBeCanceled) + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var source = (<#= t.typeName #>ConfiguredSource)state; + var source = (<#= t.typeName #>ConfiguredSource)state; <# if(IsUnityWebRequest(t)) { #> - source.asyncOperation.webRequest.Abort(); + source.asyncOperation.webRequest.Abort(); <# } #> - source.core.TrySetCanceled(source.cancellationToken); - }, result); - } + source.core.TrySetCanceled(source.cancellationToken); + }, result); } TaskTracker.TrackActiveTask(result, 3); @@ -259,6 +256,11 @@ namespace Cysharp.Threading.Tasks return false; } + if (asyncOperation == null) + { + return false; + } + if (progress != null) { progress.Report(asyncOperation.progress); From 1b76f7760855c62307a0dc3b7371ab72a1028568 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Thu, 2 Nov 2023 12:02:04 +0900 Subject: [PATCH 11/13] Add test --- .../Assets/Tests/AsyncOperationTest.cs | 52 ++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/src/UniTask/Assets/Tests/AsyncOperationTest.cs b/src/UniTask/Assets/Tests/AsyncOperationTest.cs index fb292183..0eaa1888 100644 --- a/src/UniTask/Assets/Tests/AsyncOperationTest.cs +++ b/src/UniTask/Assets/Tests/AsyncOperationTest.cs @@ -4,6 +4,7 @@ using FluentAssertions; using NUnit.Framework; using UnityEngine; +using UnityEngine.Networking; using UnityEngine.TestTools; #if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6)) @@ -13,11 +14,20 @@ namespace Cysharp.Threading.TasksTests { public class AsyncOperationTest { + [UnityTest] + public IEnumerator ResourcesLoad_Completed() => UniTask.ToCoroutine(async () => + { + var asyncOperation = Resources.LoadAsync("sample_texture"); + await asyncOperation.ToUniTask(); + asyncOperation.isDone.Should().BeTrue(); + asyncOperation.asset.GetType().Should().Be(typeof(Texture2D)); + }); + [UnityTest] public IEnumerator ResourcesLoad_CancelOnPlayerLoop() => UniTask.ToCoroutine(async () => { var cts = new CancellationTokenSource(); - var task = Resources.LoadAsync("sample_texture").ToUniTask(cancellationToken: cts.Token, handleImmediately: false); + var task = Resources.LoadAsync("sample_texture").ToUniTask(cancellationToken: cts.Token, cancelImmediately: false); cts.Cancel(); task.Status.Should().Be(UniTaskStatus.Pending); @@ -31,12 +41,50 @@ public void ResourcesLoad_CancelImmediately() { { var cts = new CancellationTokenSource(); - var task = Resources.LoadAsync("sample_texture").ToUniTask(cancellationToken: cts.Token, handleImmediately: true); + var task = Resources.LoadAsync("sample_texture").ToUniTask(cancellationToken: cts.Token, cancelImmediately: true); cts.Cancel(); task.Status.Should().Be(UniTaskStatus.Canceled); } } + +#if ENABLE_UNITYWEBREQUEST && (!UNITY_2019_1_OR_NEWER || UNITASK_WEBREQUEST_SUPPORT) + [UnityTest] + public IEnumerator UnityWebRequest_Completed() => UniTask.ToCoroutine(async () => + { + var filePath = System.IO.Path.Combine(Application.dataPath, "Tests", "Resources", "sample_texture.png"); + var asyncOperation = UnityWebRequest.Get($"file://{filePath}").SendWebRequest(); + await asyncOperation.ToUniTask(); + + asyncOperation.isDone.Should().BeTrue(); + asyncOperation.webRequest.result.Should().Be(UnityWebRequest.Result.Success); + }); + + [UnityTest] + public IEnumerator UnityWebRequest_CancelOnPlayerLoop() => UniTask.ToCoroutine(async () => + { + var cts = new CancellationTokenSource(); + var filePath = System.IO.Path.Combine(Application.dataPath, "Tests", "Resources", "sample_texture.png"); + var task = UnityWebRequest.Get($"file://{filePath}").SendWebRequest().ToUniTask(cancellationToken: cts.Token); + + cts.Cancel(); + task.Status.Should().Be(UniTaskStatus.Pending); + + await UniTask.NextFrame(); + task.Status.Should().Be(UniTaskStatus.Canceled); + }); + + [Test] + public void UnityWebRequest_CancelImmediately() + { + var cts = new CancellationTokenSource(); + cts.Cancel(); + var filePath = System.IO.Path.Combine(Application.dataPath, "Tests", "Resources", "sample_texture.png"); + var task = UnityWebRequest.Get($"file://{filePath}").SendWebRequest().ToUniTask(cancellationToken: cts.Token, cancelImmediately: true); + + task.Status.Should().Be(UniTaskStatus.Canceled); + } +#endif } } #endif From 370425578f40d5728078f560982546f51b3f8aec Mon Sep 17 00:00:00 2001 From: hadashiA Date: Thu, 2 Nov 2023 12:21:59 +0900 Subject: [PATCH 12/13] Fix AssetBundleRequestAll --- ...cExtensions.AssetBundleRequestAllAssets.cs | 163 ++++++------------ .../UnityAsyncExtensions.AsyncGPUReadback.cs | 2 +- .../UniTask/Runtime/UnityAsyncExtensions.cs | 37 ++-- .../UniTask/Runtime/UnityAsyncExtensions.tt | 11 +- 4 files changed, 77 insertions(+), 136 deletions(-) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs index f6fccc04..5d34692d 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AssetBundleRequestAllAssets.cs @@ -24,24 +24,17 @@ public static AssetBundleRequestAllAssetsAwaiter AwaitForAllAssets(this AssetBun return AwaitForAllAssets(asyncOperation, null, PlayerLoopTiming.Update, cancellationToken: cancellationToken); } - public static UniTask AwaitForAllAssets(this AssetBundleRequest asyncOperation, bool handleImmediately, CancellationToken cancellationToken) + public static UniTask AwaitForAllAssets(this AssetBundleRequest asyncOperation, CancellationToken cancellationToken, bool cancelImmediately) { - if (handleImmediately) - { - Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); - if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); - if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.allAssets); - return new UniTask(AssetBundleRequestAllAssetsCallbackHandlerSource.Create(asyncOperation, cancellationToken, out var token), token); - } - return AwaitForAllAssets(asyncOperation, progress: null, cancellationToken: cancellationToken); + return AwaitForAllAssets(asyncOperation, progress: null, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately); } - public static UniTask AwaitForAllAssets(this AssetBundleRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) + public static UniTask AwaitForAllAssets(this AssetBundleRequest asyncOperation, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.allAssets); - return new UniTask(AssetBundleRequestAllAssetsConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token); + return new UniTask(AssetBundleRequestAllAssetsConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token); } public struct AssetBundleRequestAllAssetsAwaiter : ICriticalNotifyCompletion @@ -92,108 +85,6 @@ public void UnsafeOnCompleted(Action continuation) asyncOperation.completed += continuationAction; } } - - sealed class AssetBundleRequestAllAssetsCallbackHandlerSource : IUniTaskSource, ITaskPoolNode - { - static TaskPool pool; - AssetBundleRequestAllAssetsCallbackHandlerSource nextNode; - public ref AssetBundleRequestAllAssetsCallbackHandlerSource NextNode => ref nextNode; - - static AssetBundleRequestAllAssetsCallbackHandlerSource() - { - TaskPool.RegisterSizeGetter(typeof(AssetBundleRequestConfiguredSource), () => pool.Size); - } - - AssetBundleRequest asyncOperation; - CancellationToken cancellationToken; - CancellationTokenRegistration cancellationTokenRegistration; - - UniTaskCompletionSourceCore core; - - AssetBundleRequestAllAssetsCallbackHandlerSource() - { - } - - public static IUniTaskSource Create(AssetBundleRequest asyncOperation, CancellationToken cancellationToken, out short token) - { - if (cancellationToken.IsCancellationRequested) - { - return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); - } - - if (!pool.TryPop(out var result)) - { - result = new AssetBundleRequestAllAssetsCallbackHandlerSource(); - } - - result.asyncOperation = asyncOperation; - result.cancellationToken = cancellationToken; - - asyncOperation.completed += result.AsyncOperationCompletedHandler; - - if (cancellationToken.CanBeCanceled) - { - result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => - { - var source = (AssetBundleRequestAllAssetsCallbackHandlerSource)state; - source.core.TrySetCanceled(source.cancellationToken); - }, result); - } - - TaskTracker.TrackActiveTask(result, 3); - - token = result.core.Version; - return result; - } - - public UnityEngine.Object[] GetResult(short token) - { - try - { - return core.GetResult(token); - } - finally - { - TryReturn(); - } - } - - void IUniTaskSource.GetResult(short token) - { - GetResult(token); - } - - public UniTaskStatus GetStatus(short token) - { - return core.GetStatus(token); - } - - public UniTaskStatus UnsafeGetStatus() - { - return core.UnsafeGetStatus(); - } - - public void OnCompleted(Action continuation, object state, short token) - { - core.OnCompleted(continuation, state, token); - } - - bool TryReturn() - { - TaskTracker.RemoveTracking(this); - core.Reset(); - asyncOperation.completed -= AsyncOperationCompletedHandler; - asyncOperation = default; - cancellationToken = default; - cancellationTokenRegistration.Dispose(); - return pool.TryPush(this); - } - - void AsyncOperationCompletedHandler(AsyncOperation _) - { - core.TrySetResult(asyncOperation.allAssets); - } - } sealed class AssetBundleRequestAllAssetsConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode { @@ -209,14 +100,19 @@ static AssetBundleRequestAllAssetsConfiguredSource() AssetBundleRequest asyncOperation; IProgress progress; CancellationToken cancellationToken; + CancellationTokenRegistration cancellationTokenRegistration; + bool completed; UniTaskCompletionSourceCore core; + Action continuationAction; + AssetBundleRequestAllAssetsConfiguredSource() { + continuationAction = Continuation; } - public static IUniTaskSource Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, out short token) + public static IUniTaskSource Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) { if (cancellationToken.IsCancellationRequested) { @@ -231,6 +127,18 @@ static AssetBundleRequestAllAssetsConfiguredSource() result.asyncOperation = asyncOperation; result.progress = progress; result.cancellationToken = cancellationToken; + result.completed = false; + + asyncOperation.completed += result.continuationAction; + + if (cancelImmediately && cancellationToken.CanBeCanceled) + { + result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => + { + var source = (AssetBundleRequestAllAssetsConfiguredSource)state; + source.core.TrySetCanceled(source.cancellationToken); + }, result); + } TaskTracker.TrackActiveTask(result, 3); @@ -274,6 +182,12 @@ public void OnCompleted(Action continuation, object state, short token) public bool MoveNext() { + // Already completed + if (completed || asyncOperation == null) + { + return false; + } + if (cancellationToken.IsCancellationRequested) { core.TrySetCanceled(cancellationToken); @@ -301,8 +215,29 @@ bool TryReturn() asyncOperation = default; progress = default; cancellationToken = default; + cancellationTokenRegistration.Dispose(); return pool.TryPush(this); } + + void Continuation(AsyncOperation _) + { + if (completed) + { + TryReturn(); + } + else + { + completed = true; + if (cancellationToken.IsCancellationRequested) + { + core.TrySetCanceled(cancellationToken); + } + else + { + core.TrySetResult(asyncOperation.allAssets); + } + } + } } } } diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AsyncGPUReadback.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AsyncGPUReadback.cs index 0faf0be9..5d73dc1a 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AsyncGPUReadback.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.AsyncGPUReadback.cs @@ -30,7 +30,7 @@ public static UniTask WithCancellation(this AsyncGPURea if (asyncOperation.done) return UniTask.FromResult(asyncOperation); return new UniTask(AsyncGPUReadbackRequestAwaiterConfiguredSource.Create(asyncOperation, timing, cancellationToken, cancelImmediately, out var token), token); } - + sealed class AsyncGPUReadbackRequestAwaiterConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode { static TaskPool pool; diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs index fb84fca2..7d863610 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs @@ -171,14 +171,15 @@ public void OnCompleted(Action continuation, object state, short token) public bool MoveNext() { - if (cancellationToken.IsCancellationRequested) + // Already completed + if (completed || asyncOperation == null) { - core.TrySetCanceled(cancellationToken); return false; } - if (asyncOperation == null) + if (cancellationToken.IsCancellationRequested) { + core.TrySetCanceled(cancellationToken); return false; } @@ -397,14 +398,15 @@ public void OnCompleted(Action continuation, object state, short token) public bool MoveNext() { - if (cancellationToken.IsCancellationRequested) + // Already completed + if (completed || asyncOperation == null) { - core.TrySetCanceled(cancellationToken); return false; } - if (asyncOperation == null) + if (cancellationToken.IsCancellationRequested) { + core.TrySetCanceled(cancellationToken); return false; } @@ -624,14 +626,15 @@ public void OnCompleted(Action continuation, object state, short token) public bool MoveNext() { - if (cancellationToken.IsCancellationRequested) + // Already completed + if (completed || asyncOperation == null) { - core.TrySetCanceled(cancellationToken); return false; } - if (asyncOperation == null) + if (cancellationToken.IsCancellationRequested) { + core.TrySetCanceled(cancellationToken); return false; } @@ -852,14 +855,15 @@ public void OnCompleted(Action continuation, object state, short token) public bool MoveNext() { - if (cancellationToken.IsCancellationRequested) + // Already completed + if (completed || asyncOperation == null) { - core.TrySetCanceled(cancellationToken); return false; } - if (asyncOperation == null) + if (cancellationToken.IsCancellationRequested) { + core.TrySetCanceled(cancellationToken); return false; } @@ -1096,15 +1100,16 @@ public void OnCompleted(Action continuation, object state, short token) public bool MoveNext() { - if (cancellationToken.IsCancellationRequested) + // Already completed + if (completed || asyncOperation == null) { - asyncOperation.webRequest.Abort(); - core.TrySetCanceled(cancellationToken); return false; } - if (asyncOperation == null) + if (cancellationToken.IsCancellationRequested) { + asyncOperation.webRequest.Abort(); + core.TrySetCanceled(cancellationToken); return false; } diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt index 86dd72af..6c6ff911 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt @@ -247,6 +247,12 @@ namespace Cysharp.Threading.Tasks public bool MoveNext() { + // Already completed + if (completed || asyncOperation == null) + { + return false; + } + if (cancellationToken.IsCancellationRequested) { <# if(IsUnityWebRequest(t)) { #> @@ -256,11 +262,6 @@ namespace Cysharp.Threading.Tasks return false; } - if (asyncOperation == null) - { - return false; - } - if (progress != null) { progress.Report(asyncOperation.progress); From ad23f7fb29b8c706dd8acc1b7858cdd4968ce7b4 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Thu, 2 Nov 2023 12:52:24 +0900 Subject: [PATCH 13/13] Fix `#if` directive that should be in .tt --- .../Plugins/UniTask/Runtime/UnityAsyncExtensions.cs | 3 +++ .../Plugins/UniTask/Runtime/UnityAsyncExtensions.tt | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs index 7d863610..b9cd1c9f 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs @@ -15,11 +15,14 @@ public static partial class UnityAsyncExtensions { #region AsyncOperation +#if !UNITY_2023_1_OR_NEWER + // from Unity2023.1.0a15, AsyncOperationAwaitableExtensions.GetAwaiter is defined in UnityEngine. public static AsyncOperationAwaiter GetAwaiter(this AsyncOperation asyncOperation) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); return new AsyncOperationAwaiter(asyncOperation); } +#endif public static UniTask WithCancellation(this AsyncOperation asyncOperation, CancellationToken cancellationToken) { diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt index 6c6ff911..0516fef1 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt @@ -16,6 +16,7 @@ Func ToUniTaskReturnType = x => (x == "void") ? "UniTask" : $"UniTask<{x}>"; Func ToIUniTaskSourceReturnType = x => (x == "void") ? "IUniTaskSource" : $"IUniTaskSource<{x}>"; + Func<(string typeName, string returnType, string returnField), bool> IsAsyncOperationBase = x => x.typeName == "AsyncOperation"; Func<(string typeName, string returnType, string returnField), bool> IsUnityWebRequest = x => x.returnType == "UnityWebRequest"; Func<(string typeName, string returnType, string returnField), bool> IsAssetBundleModule = x => x.typeName == "AssetBundleRequest" || x.typeName == "AssetBundleCreateRequest"; Func<(string typeName, string returnType, string returnField), bool> IsVoid = x => x.returnType == "void"; @@ -43,11 +44,18 @@ namespace Cysharp.Threading.Tasks <# } #> #region <#= t.typeName #> +<# if (IsAsyncOperationBase(t)) { #> +#if !UNITY_2023_1_OR_NEWER + // from Unity2023.1.0a15, AsyncOperationAwaitableExtensions.GetAwaiter is defined in UnityEngine. +<# } #> public static <#= t.typeName #>Awaiter GetAwaiter(this <#= t.typeName #> asyncOperation) { Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation)); return new <#= t.typeName #>Awaiter(asyncOperation); } +<# if (IsAsyncOperationBase(t)) { #> +#endif +<# } #> public static <#= ToUniTaskReturnType(t.returnType) #> WithCancellation(this <#= t.typeName #> asyncOperation, CancellationToken cancellationToken) {