From 082f3e7335dd6a7ff5df1dfb1ff762dd1d63015c Mon Sep 17 00:00:00 2001 From: Yoshifumi Kawai Date: Mon, 20 Apr 2020 08:35:06 +0900 Subject: [PATCH] more more --- Assets/UniRx.Async/AsyncLazy.cs | 65 +++ .../CancellationTokenExtensions.cs | 6 +- .../CompilerServices/MoveNextRunner.cs | 2 +- .../UniRx.Async/EnumeratorAsyncExtensions.cs | 137 +++--- .../{IAwaiter.cs => IUniTaskSource.cs} | 41 +- ...Awaiter.cs.meta => IUniTaskSource.cs.meta} | 0 Assets/UniRx.Async/Internal/LazyPromise.cs | 4 +- .../UniRx.Async/Internal/ReusablePromise.cs | 395 ------------------ .../Internal/ReusablePromise.cs.meta | 11 - Assets/UniRx.Async/Internal/StatePool.cs | 57 +++ Assets/UniRx.Async/Internal/TaskTracker.cs | 4 +- .../UniRx.Async/Triggers/AsyncTriggerBase.cs | 6 +- Assets/UniRx.Async/UniTask.Delay.cs | 30 +- Assets/UniRx.Async/UniTask.Factory.cs | 18 +- Assets/UniRx.Async/UniTask.WhenAll.cs | 8 +- Assets/UniRx.Async/UniTask.cs | 78 ++-- Assets/UniRx.Async/UniTaskCompletionSource.cs | 38 +- Assets/UniRx.Async/UniTaskExtensions.cs | 64 ++- .../UniRx.Async/UnityAsyncExtensions.Jobs.cs | 22 +- Assets/UniRx.Async/UnityAsyncExtensions.cs | 184 ++++---- .../UniRx.Async/UnityAsyncExtensions.uGUI.cs | 4 +- 21 files changed, 425 insertions(+), 749 deletions(-) create mode 100644 Assets/UniRx.Async/AsyncLazy.cs rename Assets/UniRx.Async/{IAwaiter.cs => IUniTaskSource.cs} (59%) rename Assets/UniRx.Async/{IAwaiter.cs.meta => IUniTaskSource.cs.meta} (100%) delete mode 100644 Assets/UniRx.Async/Internal/ReusablePromise.cs delete mode 100644 Assets/UniRx.Async/Internal/ReusablePromise.cs.meta create mode 100644 Assets/UniRx.Async/Internal/StatePool.cs diff --git a/Assets/UniRx.Async/AsyncLazy.cs b/Assets/UniRx.Async/AsyncLazy.cs new file mode 100644 index 0000000..1969f61 --- /dev/null +++ b/Assets/UniRx.Async/AsyncLazy.cs @@ -0,0 +1,65 @@ +#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 + +using System; +using System.Threading; + +namespace UniRx.Async +{ + public class AsyncLazy + { + Func> valueFactory; + UniTask target; + object syncLock; + bool initialized; + + public AsyncLazy(Func> valueFactory) + { + this.valueFactory = valueFactory; + this.target = default; + this.syncLock = new object(); + this.initialized = false; + } + + internal AsyncLazy(UniTask value) + { + this.valueFactory = null; + this.target = value; + this.syncLock = null; + this.initialized = true; + } + + public UniTask Task => EnsureInitialized(); + + public UniTask.Awaiter GetAwaiter() => EnsureInitialized().GetAwaiter(); + + UniTask EnsureInitialized() + { + if (Volatile.Read(ref initialized)) + { + return target; + } + + return EnsureInitializedCore(); + } + + UniTask EnsureInitializedCore() + { + lock (syncLock) + { + if (!Volatile.Read(ref initialized)) + { + var f = Interlocked.Exchange(ref valueFactory, null); + if (f != null) + { + target = f().Preserve(); // with preserve(allow multiple await). + Volatile.Write(ref initialized, true); + } + } + } + + return target; + } + } +} +#endif \ No newline at end of file diff --git a/Assets/UniRx.Async/CancellationTokenExtensions.cs b/Assets/UniRx.Async/CancellationTokenExtensions.cs index 8bfe4be..3f9a89b 100644 --- a/Assets/UniRx.Async/CancellationTokenExtensions.cs +++ b/Assets/UniRx.Async/CancellationTokenExtensions.cs @@ -17,14 +17,14 @@ namespace UniRx.Async return (UniTask.FromCanceled(cts), default(CancellationTokenRegistration)); } - var promise = new UniTaskCompletionSource(); + var promise = new UniTaskCompletionSource(); return (promise.Task, cts.RegisterWithoutCaptureExecutionContext(cancellationTokenCallback, promise)); } static void Callback(object state) { - var promise = (UniTaskCompletionSource)state; - promise.TrySetResult(AsyncUnit.Default); + var promise = (UniTaskCompletionSource)state; + promise.SetResult(); } public static CancellationTokenRegistration RegisterWithoutCaptureExecutionContext(this CancellationToken cancellationToken, Action callback) diff --git a/Assets/UniRx.Async/CompilerServices/MoveNextRunner.cs b/Assets/UniRx.Async/CompilerServices/MoveNextRunner.cs index 365ca79..89f03a8 100644 --- a/Assets/UniRx.Async/CompilerServices/MoveNextRunner.cs +++ b/Assets/UniRx.Async/CompilerServices/MoveNextRunner.cs @@ -15,7 +15,7 @@ namespace UniRx.Async.CompilerServices void Return(); } - internal class MoveNextRunner : IMoveNextRunner, IPromisePoolItem + internal sealed class MoveNextRunner : IMoveNextRunner, IPromisePoolItem where TStateMachine : IAsyncStateMachine { static PromisePool> pool = new PromisePool>(); diff --git a/Assets/UniRx.Async/EnumeratorAsyncExtensions.cs b/Assets/UniRx.Async/EnumeratorAsyncExtensions.cs index 0f6c5a6..d31388d 100644 --- a/Assets/UniRx.Async/EnumeratorAsyncExtensions.cs +++ b/Assets/UniRx.Async/EnumeratorAsyncExtensions.cs @@ -13,136 +13,117 @@ namespace UniRx.Async { public static class EnumeratorAsyncExtensions { - public static IAwaiter GetAwaiter(this IEnumerator enumerator) + public static UniTask.Awaiter GetAwaiter(this IEnumerator enumerator) { - var awaiter = new EnumeratorAwaiter(enumerator, CancellationToken.None); - if (!awaiter.IsCompleted) - { - PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, awaiter); - } - return awaiter; + return new UniTask(EnumeratorPromise.Create(enumerator, PlayerLoopTiming.Update, CancellationToken.None, out var token), token).GetAwaiter(); } public static UniTask ToUniTask(this IEnumerator enumerator) { - var awaiter = new EnumeratorAwaiter(enumerator, CancellationToken.None); - if (!awaiter.IsCompleted) - { - PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, awaiter); - } - return new UniTask(awaiter); + return new UniTask(EnumeratorPromise.Create(enumerator, PlayerLoopTiming.Update, CancellationToken.None, out var token), token); } public static UniTask ConfigureAwait(this IEnumerator enumerator, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { - var awaiter = new EnumeratorAwaiter(enumerator, cancellationToken); - if (!awaiter.IsCompleted) - { - PlayerLoopHelper.AddAction(timing, awaiter); - } - return new UniTask(awaiter); + return new UniTask(EnumeratorPromise.Create(enumerator, timing, cancellationToken, out var token), token); } - class EnumeratorAwaiter : IAwaiter, IPlayerLoopItem + class EnumeratorPromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem { + static readonly PromisePool pool = new PromisePool(); + IEnumerator innerEnumerator; CancellationToken cancellationToken; + Action continuation; - AwaiterStatus status; ExceptionDispatchInfo exception; - public EnumeratorAwaiter(IEnumerator innerEnumerator, CancellationToken cancellationToken) + UniTaskCompletionSourceCore core; + + EnumeratorPromise() + { + } + + public static IUniTaskSource Create(IEnumerator innerEnumerator, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token) { if (cancellationToken.IsCancellationRequested) { - status = AwaiterStatus.Canceled; - return; + return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); } - this.innerEnumerator = ConsumeEnumerator(innerEnumerator); - this.status = AwaiterStatus.Pending; - this.cancellationToken = cancellationToken; - this.continuation = null; + var result = pool.TryRent() ?? new EnumeratorPromise(); - TaskTracker.TrackActiveTask(this, 2); + result.innerEnumerator = ConsumeEnumerator(innerEnumerator); + result.cancellationToken = cancellationToken; + + TaskTracker2.TrackActiveTask(result, 3); + + PlayerLoopHelper.AddAction(timing, result); + + token = result.core.Version; + return result; } - public bool IsCompleted => status.IsCompleted(); - - public AwaiterStatus Status => status; - - public void GetResult() + public void GetResult(short token) { - switch (status) + try { - case AwaiterStatus.Succeeded: - break; - case AwaiterStatus.Pending: - Error.ThrowNotYetCompleted(); - break; - case AwaiterStatus.Faulted: - exception.Throw(); - break; - case AwaiterStatus.Canceled: - Error.ThrowOperationCanceledException(); - break; - default: - break; + TaskTracker2.RemoveTracking(this); + core.GetResult(token); } + finally + { + pool.TryReturn(this); + } + } + + 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) { - InvokeContinuation(AwaiterStatus.Canceled); + core.SetCanceled(cancellationToken); return false; } - var success = false; try { if (innerEnumerator.MoveNext()) { return true; } - else - { - success = true; - } } catch (Exception ex) { - exception = ExceptionDispatchInfo.Capture(ex); + core.SetException(ex); + return false; } - InvokeContinuation(success ? AwaiterStatus.Succeeded : AwaiterStatus.Faulted); + core.SetResult(null); return false; } - void InvokeContinuation(AwaiterStatus status) + public void Reset() { - this.status = status; - var cont = this.continuation; - - // cleanup - TaskTracker.RemoveTracking(this); - this.continuation = null; - this.cancellationToken = CancellationToken.None; - this.innerEnumerator = null; - - if (cont != null) cont.Invoke(); - } - - public void OnCompleted(Action continuation) - { - UnsafeOnCompleted(continuation); - } - - public void UnsafeOnCompleted(Action continuation) - { - Error.ThrowWhenContinuationIsAlreadyRegistered(this.continuation); - this.continuation = continuation; + core.Reset(); + innerEnumerator = default; + cancellationToken = default; + continuation = default; + exception = default; } // Unwrap YieldInstructions diff --git a/Assets/UniRx.Async/IAwaiter.cs b/Assets/UniRx.Async/IUniTaskSource.cs similarity index 59% rename from Assets/UniRx.Async/IAwaiter.cs rename to Assets/UniRx.Async/IUniTaskSource.cs index 46708d1..2aa12a4 100644 --- a/Assets/UniRx.Async/IAwaiter.cs +++ b/Assets/UniRx.Async/IUniTaskSource.cs @@ -6,9 +6,7 @@ using System.Runtime.CompilerServices; namespace UniRx.Async { - // TODO:rename to UniTaskStatus - - public enum AwaiterStatus + public enum UniTaskStatus { /// The operation has not yet completed. Pending = 0, @@ -20,16 +18,14 @@ namespace UniRx.Async Canceled = 3 } - - // similar as IValueTaskSource public interface IUniTaskSource { - AwaiterStatus GetStatus(short token); + UniTaskStatus GetStatus(short token); void OnCompleted(Action continuation, object state, short token); void GetResult(short token); - AwaiterStatus UnsafeGetStatus(); // only for debug use. + UniTaskStatus UnsafeGetStatus(); // only for debug use. } public interface IUniTaskSource : IUniTaskSource @@ -37,47 +33,34 @@ namespace UniRx.Async new T GetResult(short token); } - - public interface IAwaiter : ICriticalNotifyCompletion - { - AwaiterStatus Status { get; } - bool IsCompleted { get; } - void GetResult(); - } - - public interface IAwaiter : IAwaiter - { - new T GetResult(); - } - - public static class AwaiterStatusExtensions + public static class UniTaskStatusExtensions { /// != Pending. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsCompleted(this AwaiterStatus status) + public static bool IsCompleted(this UniTaskStatus status) { - return status != AwaiterStatus.Pending; + return status != UniTaskStatus.Pending; } /// == Succeeded. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsCompletedSuccessfully(this AwaiterStatus status) + public static bool IsCompletedSuccessfully(this UniTaskStatus status) { - return status == AwaiterStatus.Succeeded; + return status == UniTaskStatus.Succeeded; } /// == Canceled. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsCanceled(this AwaiterStatus status) + public static bool IsCanceled(this UniTaskStatus status) { - return status == AwaiterStatus.Canceled; + return status == UniTaskStatus.Canceled; } /// == Faulted. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsFaulted(this AwaiterStatus status) + public static bool IsFaulted(this UniTaskStatus status) { - return status == AwaiterStatus.Faulted; + return status == UniTaskStatus.Faulted; } } } diff --git a/Assets/UniRx.Async/IAwaiter.cs.meta b/Assets/UniRx.Async/IUniTaskSource.cs.meta similarity index 100% rename from Assets/UniRx.Async/IAwaiter.cs.meta rename to Assets/UniRx.Async/IUniTaskSource.cs.meta diff --git a/Assets/UniRx.Async/Internal/LazyPromise.cs b/Assets/UniRx.Async/Internal/LazyPromise.cs index e62fd59..dfea432 100644 --- a/Assets/UniRx.Async/Internal/LazyPromise.cs +++ b/Assets/UniRx.Async/Internal/LazyPromise.cs @@ -34,7 +34,7 @@ namespace UniRx.Async.Internal } } - public AwaiterStatus Status + public UniTaskStatus Status { get { @@ -94,7 +94,7 @@ namespace UniRx.Async.Internal } } - public AwaiterStatus Status + public UniTaskStatus Status { get { diff --git a/Assets/UniRx.Async/Internal/ReusablePromise.cs b/Assets/UniRx.Async/Internal/ReusablePromise.cs deleted file mode 100644 index 68ade0a..0000000 --- a/Assets/UniRx.Async/Internal/ReusablePromise.cs +++ /dev/null @@ -1,395 +0,0 @@ -#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 - -using System; -using System.Diagnostics; -using System.Runtime.ExceptionServices; -using System.Threading; - -namespace UniRx.Async.Internal -{ - // public for some types uses it. - - public abstract class ReusablePromise : IAwaiter - { - ExceptionDispatchInfo exception; - object continuation; // Action or Queue - AwaiterStatus status; - - public UniTask Task => new UniTask(this); - - // can override for control 'start/reset' timing. - public virtual bool IsCompleted => status.IsCompleted(); - - public virtual void GetResult() - { - switch (status) - { - case AwaiterStatus.Succeeded: - return; - case AwaiterStatus.Faulted: - exception.Throw(); - break; - case AwaiterStatus.Canceled: - throw new OperationCanceledException(); - default: - break; - } - - throw new InvalidOperationException("Invalid Status:" + status); - } - - public AwaiterStatus Status => status; - - void IAwaiter.GetResult() - { - GetResult(); - } - - public void ResetStatus(bool forceReset) - { - if (forceReset) - { - status = AwaiterStatus.Pending; - } - else if (status == AwaiterStatus.Succeeded) - { - status = AwaiterStatus.Pending; - } - } - - public virtual bool TrySetCanceled() - { - if (status == AwaiterStatus.Pending) - { - status = AwaiterStatus.Canceled; - TryInvokeContinuation(); - return true; - } - return false; - } - - public virtual bool TrySetException(Exception ex) - { - if (status == AwaiterStatus.Pending) - { - status = AwaiterStatus.Faulted; - exception = ExceptionDispatchInfo.Capture(ex); - TryInvokeContinuation(); - return true; - } - return false; - } - - public virtual bool TrySetResult() - { - if (status == AwaiterStatus.Pending) - { - status = AwaiterStatus.Succeeded; - TryInvokeContinuation(); - return true; - } - return false; - } - - void TryInvokeContinuation() - { - if (continuation == null) return; - - if (continuation is Action act) - { - continuation = null; - act(); - } - else - { - // reuse Queue(don't null clear) - var q = (MinimumQueue)continuation; - var size = q.Count; - for (int i = 0; i < size; i++) - { - q.Dequeue().Invoke(); - } - } - } - - public void OnCompleted(Action action) - { - UnsafeOnCompleted(action); - } - - public void UnsafeOnCompleted(Action action) - { - if (continuation == null) - { - continuation = action; - return; - } - else - { - if (continuation is Action act) - { - var q = new MinimumQueue(4); - q.Enqueue(act); - q.Enqueue(action); - continuation = q; - return; - } - else - { - ((MinimumQueue)continuation).Enqueue(action); - } - } - } - } - - public abstract class ReusablePromise : IAwaiter - { - T result; - ExceptionDispatchInfo exception; - object continuation; // Action or Queue - AwaiterStatus status; - - public UniTask Task => new UniTask(this); - - // can override for control 'start/reset' timing. - public virtual bool IsCompleted => status.IsCompleted(); - - protected T RawResult => result; - - protected void ForceSetResult(T result) - { - this.result = result; - } - - public virtual T GetResult() - { - switch (status) - { - case AwaiterStatus.Succeeded: - return result; - case AwaiterStatus.Faulted: - exception.Throw(); - break; - case AwaiterStatus.Canceled: - throw new OperationCanceledException(); - default: - break; - } - - throw new InvalidOperationException("Invalid Status:" + status); - } - - public AwaiterStatus Status => status; - - void IAwaiter.GetResult() - { - GetResult(); - } - - public void ResetStatus(bool forceReset) - { - if (forceReset) - { - status = AwaiterStatus.Pending; - } - else if (status == AwaiterStatus.Succeeded) - { - status = AwaiterStatus.Pending; - } - } - - public virtual bool TrySetCanceled() - { - if (status == AwaiterStatus.Pending) - { - status = AwaiterStatus.Canceled; - TryInvokeContinuation(); - return true; - } - return false; - } - - public virtual bool TrySetException(Exception ex) - { - if (status == AwaiterStatus.Pending) - { - status = AwaiterStatus.Faulted; - exception = ExceptionDispatchInfo.Capture(ex); - TryInvokeContinuation(); - return true; - } - return false; - } - - public virtual bool TrySetResult(T result) - { - if (status == AwaiterStatus.Pending) - { - status = AwaiterStatus.Succeeded; - this.result = result; - TryInvokeContinuation(); - return true; - } - return false; - } - - protected void TryInvokeContinuation() - { - if (continuation == null) return; - - if (continuation is Action act) - { - continuation = null; - act(); - } - else - { - // reuse Queue(don't null clear) - var q = (MinimumQueue)continuation; - var size = q.Count; - for (int i = 0; i < size; i++) - { - q.Dequeue().Invoke(); - } - } - } - - public void OnCompleted(Action action) - { - UnsafeOnCompleted(action); - } - - public void UnsafeOnCompleted(Action action) - { - if (continuation == null) - { - continuation = action; - return; - } - else - { - if (continuation is Action act) - { - var q = new MinimumQueue(4); - q.Enqueue(act); - q.Enqueue(action); - continuation = q; - return; - } - else - { - ((MinimumQueue)continuation).Enqueue(action); - } - } - } - } - - public abstract class PlayerLoopReusablePromiseBase : ReusablePromise, IPlayerLoopItem - { - readonly PlayerLoopTiming timing; - protected readonly CancellationToken cancellationToken; - bool isRunning = false; - -#if UNITY_EDITOR - string capturedStackTraceForDebugging; -#endif - - public PlayerLoopReusablePromiseBase(PlayerLoopTiming timing, CancellationToken cancellationToken, int skipTrackFrameCountAdditive) - { - this.timing = timing; - this.cancellationToken = cancellationToken; - -#if UNITY_EDITOR - this.capturedStackTraceForDebugging = TaskTracker.CaptureStackTrace(skipTrackFrameCountAdditive + 1); // 1 is self, -#endif - } - - public override bool IsCompleted - { - get - { - if (Status == AwaiterStatus.Canceled || Status == AwaiterStatus.Faulted) return true; - - if (!isRunning) - { - isRunning = true; - ResetStatus(false); - OnRunningStart(); -#if UNITY_EDITOR - TaskTracker.TrackActiveTask(this, capturedStackTraceForDebugging); -#endif - PlayerLoopHelper.AddAction(timing, this); - } - return false; - } - } - - protected abstract void OnRunningStart(); - - protected void Complete() - { - isRunning = false; -#if UNITY_EDITOR - TaskTracker.RemoveTracking(this); -#endif - } - - public abstract bool MoveNext(); - } - - public abstract class PlayerLoopReusablePromiseBase : ReusablePromise, IPlayerLoopItem - { - readonly PlayerLoopTiming timing; - protected readonly CancellationToken cancellationToken; - bool isRunning = false; - -#if UNITY_EDITOR - string capturedStackTraceForDebugging; -#endif - - public PlayerLoopReusablePromiseBase(PlayerLoopTiming timing, CancellationToken cancellationToken, int skipTrackFrameCountAdditive) - { - this.timing = timing; - this.cancellationToken = cancellationToken; - -#if UNITY_EDITOR - this.capturedStackTraceForDebugging = TaskTracker.CaptureStackTrace(skipTrackFrameCountAdditive + 1); // 1 is self, -#endif - } - - public override bool IsCompleted - { - get - { - if (Status == AwaiterStatus.Canceled || Status == AwaiterStatus.Faulted) return true; - - if (!isRunning) - { - isRunning = true; - ResetStatus(false); - OnRunningStart(); -#if UNITY_EDITOR - TaskTracker.TrackActiveTask(this, capturedStackTraceForDebugging); -#endif - PlayerLoopHelper.AddAction(timing, this); - } - return false; - } - } - - protected abstract void OnRunningStart(); - - protected void Complete() - { - isRunning = false; -#if UNITY_EDITOR - TaskTracker.RemoveTracking(this); -#endif - } - - public abstract bool MoveNext(); - } - -} -#endif diff --git a/Assets/UniRx.Async/Internal/ReusablePromise.cs.meta b/Assets/UniRx.Async/Internal/ReusablePromise.cs.meta deleted file mode 100644 index d86918f..0000000 --- a/Assets/UniRx.Async/Internal/ReusablePromise.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: a8cfc99b5928c0242919aac2121b02bb -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/UniRx.Async/Internal/StatePool.cs b/Assets/UniRx.Async/Internal/StatePool.cs new file mode 100644 index 0000000..b46f07b --- /dev/null +++ b/Assets/UniRx.Async/Internal/StatePool.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Concurrent; +using System.Runtime.CompilerServices; + +namespace UniRx.Async.Internal +{ + internal static class StateTuple + { + public static StateTuple Create(T1 item1, T2 item2) + { + return StatePool.Create(item1, item2); + } + } + + internal class StateTuple : IDisposable + { + public T1 Item1; + public T2 Item2; + + public void Deconstruct(out T1 item1, out T2 item2) + { + item1 = this.Item1; + item2 = this.Item2; + } + + public void Dispose() + { + StatePool.Return(this); + } + } + + internal static class StatePool + { + static readonly ConcurrentQueue> queue = new ConcurrentQueue>(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static StateTuple Create(T1 item1, T2 item2) + { + if (queue.TryDequeue(out var value)) + { + value.Item1 = item1; + value.Item2 = item2; + return value; + } + + return new StateTuple { Item1 = item1, Item2 = item2 }; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Return(StateTuple tuple) + { + tuple.Item1 = default; + tuple.Item2 = default; + queue.Enqueue(tuple); + } + } +} diff --git a/Assets/UniRx.Async/Internal/TaskTracker.cs b/Assets/UniRx.Async/Internal/TaskTracker.cs index a1d2ae6..e69df26 100644 --- a/Assets/UniRx.Async/Internal/TaskTracker.cs +++ b/Assets/UniRx.Async/Internal/TaskTracker.cs @@ -116,7 +116,7 @@ namespace UniRx.Async.Internal } /// (trackingId, awaiterType, awaiterStatus, createdTime, stackTrace) - public static void ForEachActiveTask(Action action) + public static void ForEachActiveTask(Action action) { lock (listPool) { @@ -233,7 +233,7 @@ namespace UniRx.Async.Internal } /// (trackingId, awaiterType, awaiterStatus, createdTime, stackTrace) - public static void ForEachActiveTask(Action action) + public static void ForEachActiveTask(Action action) { lock (listPool) { diff --git a/Assets/UniRx.Async/Triggers/AsyncTriggerBase.cs b/Assets/UniRx.Async/Triggers/AsyncTriggerBase.cs index ceb4f7f..b0ffa57 100644 --- a/Assets/UniRx.Async/Triggers/AsyncTriggerBase.cs +++ b/Assets/UniRx.Async/Triggers/AsyncTriggerBase.cs @@ -32,13 +32,13 @@ namespace UniRx.Async.Triggers public override T GetResult() { - if (Status == AwaiterStatus.Pending) return RawResult; + if (Status == UniTaskStatus.Pending) return RawResult; return base.GetResult(); } public override bool TrySetResult(T result) { - if (Status == AwaiterStatus.Pending) + if (Status == UniTaskStatus.Pending) { // keep status as Pending. this.ForceSetResult(result); @@ -50,7 +50,7 @@ namespace UniRx.Async.Triggers public override bool TrySetCanceled() { - if (Status == AwaiterStatus.Canceled) return false; + if (Status == UniTaskStatus.Canceled) return false; TaskTracker.RemoveTracking(this); return base.TrySetCanceled(); } diff --git a/Assets/UniRx.Async/UniTask.Delay.cs b/Assets/UniRx.Async/UniTask.Delay.cs index 0ab0fb2..feb8074 100644 --- a/Assets/UniRx.Async/UniTask.Delay.cs +++ b/Assets/UniRx.Async/UniTask.Delay.cs @@ -24,6 +24,8 @@ namespace UniRx.Async public static UniTask DelayFrame(int delayFrameCount, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken)) { + PlayerLoopHelper.Initialize( + if (delayFrameCount < 0) { throw new ArgumentOutOfRangeException("Delay does not allow minus delayFrameCount. delayFrameCount:" + delayFrameCount); @@ -57,7 +59,7 @@ namespace UniRx.Async : new UniTask(DelayPromise.Create(delayTimeSpan, delayTiming, cancellationToken, out token), token); } - class YieldPromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem + sealed class YieldPromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem { static readonly PromisePool pool = new PromisePool(); @@ -100,12 +102,12 @@ namespace UniRx.Async } } - public AwaiterStatus GetStatus(short token) + public UniTaskStatus GetStatus(short token) { return core.GetStatus(token); } - public AwaiterStatus UnsafeGetStatus() + public UniTaskStatus UnsafeGetStatus() { return core.UnsafeGetStatus(); } @@ -134,7 +136,7 @@ namespace UniRx.Async } } - class DelayFramePromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem + sealed class DelayFramePromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem { static readonly PromisePool pool = new PromisePool(); @@ -181,12 +183,12 @@ namespace UniRx.Async } } - public AwaiterStatus GetStatus(short token) + public UniTaskStatus GetStatus(short token) { return core.GetStatus(token); } - public AwaiterStatus UnsafeGetStatus() + public UniTaskStatus UnsafeGetStatus() { return core.UnsafeGetStatus(); } @@ -223,7 +225,7 @@ namespace UniRx.Async } } - class DelayPromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem + sealed class DelayPromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem { static readonly PromisePool pool = new PromisePool(); @@ -271,12 +273,12 @@ namespace UniRx.Async } } - public AwaiterStatus GetStatus(short token) + public UniTaskStatus GetStatus(short token) { return core.GetStatus(token); } - public AwaiterStatus UnsafeGetStatus() + public UniTaskStatus UnsafeGetStatus() { return core.UnsafeGetStatus(); } @@ -313,7 +315,7 @@ namespace UniRx.Async } } - class DelayIgnoreTimeScalePromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem + sealed class DelayIgnoreTimeScalePromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem { static readonly PromisePool pool = new PromisePool(); @@ -361,12 +363,12 @@ namespace UniRx.Async } } - public AwaiterStatus GetStatus(short token) + public UniTaskStatus GetStatus(short token) { return core.GetStatus(token); } - public AwaiterStatus UnsafeGetStatus() + public UniTaskStatus UnsafeGetStatus() { return core.UnsafeGetStatus(); } @@ -404,7 +406,7 @@ namespace UniRx.Async } } - public struct YieldAwaitable + public readonly struct YieldAwaitable { readonly PlayerLoopTiming timing; @@ -423,7 +425,7 @@ namespace UniRx.Async return UniTask.Yield(timing, CancellationToken.None); } - public struct Awaiter : ICriticalNotifyCompletion + public readonly struct Awaiter : ICriticalNotifyCompletion { readonly PlayerLoopTiming timing; diff --git a/Assets/UniRx.Async/UniTask.Factory.cs b/Assets/UniRx.Async/UniTask.Factory.cs index e97d36e..5878d91 100644 --- a/Assets/UniRx.Async/UniTask.Factory.cs +++ b/Assets/UniRx.Async/UniTask.Factory.cs @@ -83,12 +83,19 @@ namespace UniRx.Async } } - // TODO:... - - /// shorthand of new UniTask[T](Func[UniTask[T]] factory) - public static UniTask Lazy(Func> factory) + public static UniTask Create(Func factory) { - return new UniTask(factory); + return factory(); + } + + public static UniTask Create(Func> factory) + { + return factory(); + } + + public static AsyncLazy Lazy(Func> factory) + { + return new AsyncLazy(factory); } /// @@ -122,7 +129,6 @@ namespace UniRx.Async internal static class CompletedTasks { - public static readonly UniTask Completed = new UniTask(); public static readonly UniTask AsyncUnit = UniTask.FromResult(UniRx.Async.AsyncUnit.Default); public static readonly UniTask True = UniTask.FromResult(true); public static readonly UniTask False = UniTask.FromResult(false); diff --git a/Assets/UniRx.Async/UniTask.WhenAll.cs b/Assets/UniRx.Async/UniTask.WhenAll.cs index 715e55b..7b087d4 100644 --- a/Assets/UniRx.Async/UniTask.WhenAll.cs +++ b/Assets/UniRx.Async/UniTask.WhenAll.cs @@ -62,12 +62,12 @@ namespace UniRx.Async for (int i = 0; i < tasksLength; i++) { - if (tasks[i].IsCompleted) + if (tasks[i].Status.IsCompleted()) { T value = default(T); try { - value = tasks[i].Result; + value = tasks[i].GetAwaiter().GetResult(); } catch (Exception ex) { @@ -189,11 +189,11 @@ namespace UniRx.Async for (int i = 0; i < tasksLength; i++) { - if (tasks[i].IsCompleted) + if (tasks[i].Status.IsCompleted()) { try { - tasks[i].GetResult(); + tasks[i].GetAwaiter().GetResult(); } catch (Exception ex) { diff --git a/Assets/UniRx.Async/UniTask.cs b/Assets/UniRx.Async/UniTask.cs index 1b97d18..5a372a7 100644 --- a/Assets/UniRx.Async/UniTask.cs +++ b/Assets/UniRx.Async/UniTask.cs @@ -39,13 +39,13 @@ namespace UniRx.Async this.token = token; } - public AwaiterStatus Status + public UniTaskStatus Status { [DebuggerHidden] [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (source == null) return AwaiterStatus.Succeeded; + if (source == null) return UniTaskStatus.Succeeded; return source.GetStatus(token); } } @@ -63,8 +63,8 @@ namespace UniRx.Async public UniTask SuppressCancellationThrow() { var status = Status; - if (status == AwaiterStatus.Succeeded) return CompletedTasks.False; - if (status == AwaiterStatus.Canceled) return CompletedTasks.True; + if (status == UniTaskStatus.Succeeded) return CompletedTasks.False; + if (status == UniTaskStatus.Canceled) return CompletedTasks.True; return new UniTask(new IsCanceledSource(source), token); } @@ -89,20 +89,20 @@ namespace UniRx.Async } } - public static implicit operator UniTask(UniTask task) + public UniTask AsAsyncUnitUniTask() { - if (task.source == null) return CompletedTasks.AsyncUnit; + if (this.source == null) return CompletedTasks.AsyncUnit; - var status = task.source.GetStatus(task.token); + var status = this.source.GetStatus(this.token); if (status.IsCompletedSuccessfully()) { return CompletedTasks.AsyncUnit; } - return new UniTask(new AsyncUnitSource(task.source), task.token); + return new UniTask(new AsyncUnitSource(this.source), this.token); } - class AsyncUnitSource : IUniTaskSource + sealed class AsyncUnitSource : IUniTaskSource { readonly IUniTaskSource source; @@ -117,7 +117,7 @@ namespace UniRx.Async return AsyncUnit.Default; } - public AwaiterStatus GetStatus(short token) + public UniTaskStatus GetStatus(short token) { return source.GetStatus(token); } @@ -127,7 +127,7 @@ namespace UniRx.Async source.OnCompleted(continuation, state, token); } - public AwaiterStatus UnsafeGetStatus() + public UniTaskStatus UnsafeGetStatus() { return source.UnsafeGetStatus(); } @@ -138,7 +138,7 @@ namespace UniRx.Async } } - class IsCanceledSource : IUniTaskSource + sealed class IsCanceledSource : IUniTaskSource { readonly IUniTaskSource source; @@ -149,7 +149,7 @@ namespace UniRx.Async public bool GetResult(short token) { - if (source.GetStatus(token) == AwaiterStatus.Canceled) + if (source.GetStatus(token) == UniTaskStatus.Canceled) { return true; } @@ -163,12 +163,12 @@ namespace UniRx.Async GetResult(token); } - public AwaiterStatus GetStatus(short token) + public UniTaskStatus GetStatus(short token) { return source.GetStatus(token); } - public AwaiterStatus UnsafeGetStatus() + public UniTaskStatus UnsafeGetStatus() { return source.UnsafeGetStatus(); } @@ -179,11 +179,11 @@ namespace UniRx.Async } } - class MemoizeSource : IUniTaskSource + sealed class MemoizeSource : IUniTaskSource { IUniTaskSource source; ExceptionDispatchInfo exception; - AwaiterStatus status; + UniTaskStatus status; public MemoizeSource(IUniTaskSource source) { @@ -204,18 +204,18 @@ namespace UniRx.Async try { source.GetResult(token); - status = AwaiterStatus.Succeeded; + status = UniTaskStatus.Succeeded; } catch (Exception ex) { exception = ExceptionDispatchInfo.Capture(ex); if (ex is OperationCanceledException) { - status = AwaiterStatus.Canceled; + status = UniTaskStatus.Canceled; } else { - status = AwaiterStatus.Faulted; + status = UniTaskStatus.Faulted; } throw; } @@ -226,7 +226,7 @@ namespace UniRx.Async } } - public AwaiterStatus GetStatus(short token) + public UniTaskStatus GetStatus(short token) { if (source == null) { @@ -248,7 +248,7 @@ namespace UniRx.Async } } - public AwaiterStatus UnsafeGetStatus() + public UniTaskStatus UnsafeGetStatus() { if (source == null) { @@ -361,13 +361,13 @@ namespace UniRx.Async this.result = default; } - public AwaiterStatus Status + public UniTaskStatus Status { [DebuggerHidden] [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - return (source == null) ? AwaiterStatus.Succeeded : source.GetStatus(token); + return (source == null) ? UniTaskStatus.Succeeded : source.GetStatus(token); } } @@ -393,17 +393,17 @@ namespace UniRx.Async } } - public static implicit operator UniTask(UniTask task) + public UniTask AsUniTask() { - if (task.source == null) return UniTask.CompletedTask; + if (this.source == null) return UniTask.CompletedTask; - var status = task.source.GetStatus(task.token); + var status = this.source.GetStatus(this.token); if (status.IsCompletedSuccessfully()) { return UniTask.CompletedTask; } - return new UniTask(task.source, task.token); + return new UniTask(this.source, this.token); } /// @@ -425,7 +425,7 @@ namespace UniRx.Async : "(" + this.source.UnsafeGetStatus() + ")"; } - class IsCanceledSource : IUniTaskSource<(bool, T)> + sealed class IsCanceledSource : IUniTaskSource<(bool, T)> { readonly IUniTaskSource source; @@ -440,7 +440,7 @@ namespace UniRx.Async [MethodImpl(MethodImplOptions.AggressiveInlining)] public (bool, T) GetResult(short token) { - if (source.GetStatus(token) == AwaiterStatus.Canceled) + if (source.GetStatus(token) == UniTaskStatus.Canceled) { return (true, default); } @@ -458,14 +458,14 @@ namespace UniRx.Async [DebuggerHidden] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public AwaiterStatus GetStatus(short token) + public UniTaskStatus GetStatus(short token) { return source.GetStatus(token); } [DebuggerHidden] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public AwaiterStatus UnsafeGetStatus() + public UniTaskStatus UnsafeGetStatus() { return source.UnsafeGetStatus(); } @@ -478,12 +478,12 @@ namespace UniRx.Async } } - class MemoizeSource : IUniTaskSource + sealed class MemoizeSource : IUniTaskSource { IUniTaskSource source; T result; ExceptionDispatchInfo exception; - AwaiterStatus status; + UniTaskStatus status; public MemoizeSource(IUniTaskSource source) { @@ -505,7 +505,7 @@ namespace UniRx.Async try { result = source.GetResult(token); - status = AwaiterStatus.Succeeded; + status = UniTaskStatus.Succeeded; return result; } catch (Exception ex) @@ -513,11 +513,11 @@ namespace UniRx.Async exception = ExceptionDispatchInfo.Capture(ex); if (ex is OperationCanceledException) { - status = AwaiterStatus.Canceled; + status = UniTaskStatus.Canceled; } else { - status = AwaiterStatus.Faulted; + status = UniTaskStatus.Faulted; } throw; } @@ -533,7 +533,7 @@ namespace UniRx.Async GetResult(token); } - public AwaiterStatus GetStatus(short token) + public UniTaskStatus GetStatus(short token) { if (source == null) { @@ -555,7 +555,7 @@ namespace UniRx.Async } } - public AwaiterStatus UnsafeGetStatus() + public UniTaskStatus UnsafeGetStatus() { if (source == null) { diff --git a/Assets/UniRx.Async/UniTaskCompletionSource.cs b/Assets/UniRx.Async/UniTaskCompletionSource.cs index 73427f8..0ef9906 100644 --- a/Assets/UniRx.Async/UniTaskCompletionSource.cs +++ b/Assets/UniRx.Async/UniTaskCompletionSource.cs @@ -45,7 +45,7 @@ namespace UniRx.Async // Struct Size: TResult + (8 + 2 + 1 + 1 + 8 + 8) TResult result; - object error; // Exception or OperationCanceledException + object error; // ExceptionDispatchInfo or OperationCanceledException short version; bool completed; bool hasUnhandledError; @@ -119,23 +119,23 @@ namespace UniRx.Async /// Gets the status of the operation. /// Opaque value that was provided to the 's constructor. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public AwaiterStatus GetStatus(short token) + public UniTaskStatus GetStatus(short token) { ValidateToken(token); - return (continuation == null || !completed) ? AwaiterStatus.Pending - : (error == null) ? AwaiterStatus.Succeeded - : (error is OperationCanceledException) ? AwaiterStatus.Canceled - : AwaiterStatus.Faulted; + return (continuation == null || !completed) ? UniTaskStatus.Pending + : (error == null) ? UniTaskStatus.Succeeded + : (error is OperationCanceledException) ? UniTaskStatus.Canceled + : UniTaskStatus.Faulted; } /// Gets the status of the operation without token validation. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public AwaiterStatus UnsafeGetStatus() + public UniTaskStatus UnsafeGetStatus() { - return (continuation == null || !completed) ? AwaiterStatus.Pending - : (error == null) ? AwaiterStatus.Succeeded - : (error is OperationCanceledException) ? AwaiterStatus.Canceled - : AwaiterStatus.Faulted; + return (continuation == null || !completed) ? UniTaskStatus.Pending + : (error == null) ? UniTaskStatus.Succeeded + : (error is OperationCanceledException) ? UniTaskStatus.Canceled + : UniTaskStatus.Faulted; } /// Gets the result of the operation. @@ -296,12 +296,12 @@ namespace UniRx.Async core.GetResult(token); } - public AwaiterStatus GetStatus(short token) + public UniTaskStatus GetStatus(short token) { return core.GetStatus(token); } - public AwaiterStatus UnsafeGetStatus() + public UniTaskStatus UnsafeGetStatus() { return core.UnsafeGetStatus(); } @@ -396,12 +396,12 @@ namespace UniRx.Async } - public AwaiterStatus GetStatus(short token) + public UniTaskStatus GetStatus(short token) { return core.GetStatus(token); } - public AwaiterStatus UnsafeGetStatus() + public UniTaskStatus UnsafeGetStatus() { return core.UnsafeGetStatus(); } @@ -487,12 +487,12 @@ namespace UniRx.Async GetResult(token); } - public AwaiterStatus GetStatus(short token) + public UniTaskStatus GetStatus(short token) { return core.GetStatus(token); } - public AwaiterStatus UnsafeGetStatus() + public UniTaskStatus UnsafeGetStatus() { return core.UnsafeGetStatus(); } @@ -591,12 +591,12 @@ namespace UniRx.Async GetResult(token); } - public AwaiterStatus GetStatus(short token) + public UniTaskStatus GetStatus(short token) { return core.GetStatus(token); } - public AwaiterStatus UnsafeGetStatus() + public UniTaskStatus UnsafeGetStatus() { return core.UnsafeGetStatus(); } diff --git a/Assets/UniRx.Async/UniTaskExtensions.cs b/Assets/UniRx.Async/UniTaskExtensions.cs index 64c0f9b..d643bc3 100644 --- a/Assets/UniRx.Async/UniTaskExtensions.cs +++ b/Assets/UniRx.Async/UniTaskExtensions.cs @@ -12,24 +12,6 @@ namespace UniRx.Async { public static partial class UniTaskExtensions { - /// - /// Convert UniTask -> UniTask[AsyncUnit]. - /// - public static UniTask AsAsyncUnitUniTask(this UniTask task) - { - // use implicit conversion - return task; - } - - /// - /// Convert UniTask[T] -> UniTask. - /// - public static UniTask AsUniTask(this UniTask task) - { - // use implicit conversion - return task; - } - /// /// Convert Task[T] -> UniTask[T]. /// @@ -112,17 +94,20 @@ namespace UniRx.Async awaiter.SourceOnCompleted(state => { - var (inTcs, inAwaiter) = ((TaskCompletionSource, UniTask.Awaiter))state; - try + using (var tuple = (StateTuple, UniTask.Awaiter>)state) { - var result = inAwaiter.GetResult(); - inTcs.SetResult(result); + var (inTcs, inAwaiter) = tuple; + try + { + var result = inAwaiter.GetResult(); + inTcs.SetResult(result); + } + catch (Exception ex) + { + inTcs.SetException(ex); + } } - catch (Exception ex) - { - inTcs.SetException(ex); - } - }, (tcs, awaiter)); + }, StateTuple.Create(tcs, awaiter)); return tcs.Task; } @@ -154,17 +139,20 @@ namespace UniRx.Async awaiter.SourceOnCompleted(state => { - var (inTcs, inAwaiter) = ((TaskCompletionSource, UniTask.Awaiter))state; - try + using (var tuple = (StateTuple, UniTask.Awaiter>)state) { - inAwaiter.GetResult(); - inTcs.SetResult(null); + var (inTcs, inAwaiter) = tuple; + try + { + inAwaiter.GetResult(); + inTcs.SetResult(null); + } + catch (Exception ex) + { + inTcs.SetException(ex); + } } - catch (Exception ex) - { - inTcs.SetException(ex); - } - }, (tcs, awaiter)); + }, StateTuple.Create(tcs, awaiter)); return tcs.Task; } @@ -196,7 +184,7 @@ namespace UniRx.Async // left, right both suppress operation canceled exception. var delayCancellationTokenSource = new CancellationTokenSource(); - var timeoutTask = (UniTask)UniTask.Delay(timeout, ignoreTimeScale, timeoutCheckTiming).SuppressCancellationThrow(); + var timeoutTask = UniTask.Delay(timeout, ignoreTimeScale, timeoutCheckTiming).SuppressCancellationThrow(); var (hasValue, value) = await UniTask.WhenAny(task.SuppressCancellationThrow(), timeoutTask); @@ -242,7 +230,7 @@ namespace UniRx.Async // left, right both suppress operation canceled exception. var delayCancellationTokenSource = new CancellationTokenSource(); - var timeoutTask = (UniTask)UniTask.Delay(timeout, ignoreTimeScale, timeoutCheckTiming).SuppressCancellationThrow(); + var timeoutTask = UniTask.Delay(timeout, ignoreTimeScale, timeoutCheckTiming).SuppressCancellationThrow(); var (hasValue, value) = await UniTask.WhenAny(task.SuppressCancellationThrow(), timeoutTask); diff --git a/Assets/UniRx.Async/UnityAsyncExtensions.Jobs.cs b/Assets/UniRx.Async/UnityAsyncExtensions.Jobs.cs index 01185e3..2e958d6 100644 --- a/Assets/UniRx.Async/UnityAsyncExtensions.Jobs.cs +++ b/Assets/UniRx.Async/UnityAsyncExtensions.Jobs.cs @@ -52,20 +52,20 @@ namespace UniRx.Async { JobHandle jobHandle; CancellationToken cancellationToken; - AwaiterStatus status; + UniTaskStatus status; Action continuation; public JobHandleAwaiter(JobHandle jobHandle, CancellationToken cancellationToken, int skipFrame = 2) { - this.status = cancellationToken.IsCancellationRequested ? AwaiterStatus.Canceled - : jobHandle.IsCompleted ? AwaiterStatus.Succeeded - : AwaiterStatus.Pending; + this.status = cancellationToken.IsCancellationRequested ? UniTaskStatus.Canceled + : jobHandle.IsCompleted ? UniTaskStatus.Succeeded + : UniTaskStatus.Pending; if (this.status.IsCompleted()) return; this.jobHandle = jobHandle; this.cancellationToken = cancellationToken; - this.status = AwaiterStatus.Pending; + this.status = UniTaskStatus.Pending; this.continuation = null; TaskTracker.TrackActiveTask(this, skipFrame); @@ -73,15 +73,15 @@ namespace UniRx.Async public bool IsCompleted => status.IsCompleted(); - public AwaiterStatus Status => status; + public UniTaskStatus Status => status; public void GetResult() { - if (status == AwaiterStatus.Succeeded) + if (status == UniTaskStatus.Succeeded) { return; } - else if (status == AwaiterStatus.Canceled) + else if (status == UniTaskStatus.Canceled) { Error.ThrowOperationCanceledException(); } @@ -95,21 +95,21 @@ namespace UniRx.Async { // Call jobHandle.Complete after finished. PlayerLoopHelper.AddAction(PlayerLoopTiming.EarlyUpdate, new JobHandleAwaiter(jobHandle, CancellationToken.None, 1)); - InvokeContinuation(AwaiterStatus.Canceled); + InvokeContinuation(UniTaskStatus.Canceled); return false; } if (jobHandle.IsCompleted) { jobHandle.Complete(); - InvokeContinuation(AwaiterStatus.Succeeded); + InvokeContinuation(UniTaskStatus.Succeeded); return false; } return true; } - void InvokeContinuation(AwaiterStatus status) + void InvokeContinuation(UniTaskStatus status) { this.status = status; var cont = this.continuation; diff --git a/Assets/UniRx.Async/UnityAsyncExtensions.cs b/Assets/UniRx.Async/UnityAsyncExtensions.cs index 47314cb..9bf78bb 100644 --- a/Assets/UniRx.Async/UnityAsyncExtensions.cs +++ b/Assets/UniRx.Async/UnityAsyncExtensions.cs @@ -187,28 +187,28 @@ namespace UniRx.Async { AsyncOperation asyncOperation; Action continuationAction; - AwaiterStatus status; + UniTaskStatus status; public AsyncOperationAwaiter(AsyncOperation asyncOperation) { - this.status = asyncOperation.isDone ? AwaiterStatus.Succeeded : AwaiterStatus.Pending; + this.status = asyncOperation.isDone ? UniTaskStatus.Succeeded : UniTaskStatus.Pending; this.asyncOperation = (this.status.IsCompleted()) ? null : asyncOperation; this.continuationAction = null; } public bool IsCompleted => status.IsCompleted(); - public AwaiterStatus Status => status; + public UniTaskStatus Status => status; public void GetResult() { - if (status == AwaiterStatus.Succeeded) return; + if (status == UniTaskStatus.Succeeded) return; - if (status == AwaiterStatus.Pending) + if (status == UniTaskStatus.Pending) { // first timing of call if (asyncOperation.isDone) { - status = AwaiterStatus.Succeeded; + status = UniTaskStatus.Succeeded; } else { @@ -246,14 +246,14 @@ namespace UniRx.Async AsyncOperation asyncOperation; IProgress progress; CancellationToken cancellationToken; - AwaiterStatus status; + UniTaskStatus status; Action continuation; public AsyncOperationConfiguredAwaiter(AsyncOperation asyncOperation, IProgress progress, CancellationToken cancellationToken) { - this.status = cancellationToken.IsCancellationRequested ? AwaiterStatus.Canceled - : asyncOperation.isDone ? AwaiterStatus.Succeeded - : AwaiterStatus.Pending; + this.status = cancellationToken.IsCancellationRequested ? UniTaskStatus.Canceled + : asyncOperation.isDone ? UniTaskStatus.Succeeded + : UniTaskStatus.Pending; if (this.status.IsCompleted()) return; @@ -266,15 +266,15 @@ namespace UniRx.Async } public bool IsCompleted => status.IsCompleted(); - public AwaiterStatus Status => status; + public UniTaskStatus Status => status; public void GetResult() { - if (status == AwaiterStatus.Succeeded) + if (status == UniTaskStatus.Succeeded) { return; } - else if (status == AwaiterStatus.Canceled) + else if (status == UniTaskStatus.Canceled) { Error.ThrowOperationCanceledException(); } @@ -286,7 +286,7 @@ namespace UniRx.Async { if (cancellationToken.IsCancellationRequested) { - InvokeContinuation(AwaiterStatus.Canceled); + InvokeContinuation(UniTaskStatus.Canceled); return false; } @@ -297,14 +297,14 @@ namespace UniRx.Async if (asyncOperation.isDone) { - InvokeContinuation(AwaiterStatus.Succeeded); + InvokeContinuation(UniTaskStatus.Succeeded); return false; } return true; } - void InvokeContinuation(AwaiterStatus status) + void InvokeContinuation(UniTaskStatus status) { this.status = status; var cont = this.continuation; @@ -335,30 +335,30 @@ namespace UniRx.Async { ResourceRequest asyncOperation; Action continuationAction; - AwaiterStatus status; + UniTaskStatus status; UnityEngine.Object result; public ResourceRequestAwaiter(ResourceRequest asyncOperation) { - this.status = asyncOperation.isDone ? AwaiterStatus.Succeeded : AwaiterStatus.Pending; + this.status = asyncOperation.isDone ? UniTaskStatus.Succeeded : UniTaskStatus.Pending; this.asyncOperation = (this.status.IsCompleted()) ? null : asyncOperation; this.result = (this.status.IsCompletedSuccessfully()) ? asyncOperation.asset : null; this.continuationAction = null; } public bool IsCompleted => status.IsCompleted(); - public AwaiterStatus Status => status; + public UniTaskStatus Status => status; public UnityEngine.Object GetResult() { - if (status == AwaiterStatus.Succeeded) return this.result; + if (status == UniTaskStatus.Succeeded) return this.result; - if (status == AwaiterStatus.Pending) + if (status == UniTaskStatus.Pending) { // first timing of call if (asyncOperation.isDone) { - status = AwaiterStatus.Succeeded; + status = UniTaskStatus.Succeeded; } else { @@ -402,15 +402,15 @@ namespace UniRx.Async ResourceRequest asyncOperation; IProgress progress; CancellationToken cancellationToken; - AwaiterStatus status; + UniTaskStatus status; Action continuation; UnityEngine.Object result; public ResourceRequestConfiguredAwaiter(ResourceRequest asyncOperation, IProgress progress, CancellationToken cancellationToken) { - this.status = cancellationToken.IsCancellationRequested ? AwaiterStatus.Canceled - : asyncOperation.isDone ? AwaiterStatus.Succeeded - : AwaiterStatus.Pending; + this.status = cancellationToken.IsCancellationRequested ? UniTaskStatus.Canceled + : asyncOperation.isDone ? UniTaskStatus.Succeeded + : UniTaskStatus.Pending; if (this.status.IsCompletedSuccessfully()) this.result = asyncOperation.asset; if (this.status.IsCompleted()) return; @@ -425,14 +425,14 @@ namespace UniRx.Async } public bool IsCompleted => status.IsCompleted(); - public AwaiterStatus Status => status; + public UniTaskStatus Status => status; void IAwaiter.GetResult() => GetResult(); public UnityEngine.Object GetResult() { - if (status == AwaiterStatus.Succeeded) return this.result; + if (status == UniTaskStatus.Succeeded) return this.result; - if (status == AwaiterStatus.Canceled) + if (status == UniTaskStatus.Canceled) { Error.ThrowOperationCanceledException(); } @@ -444,7 +444,7 @@ namespace UniRx.Async { if (cancellationToken.IsCancellationRequested) { - InvokeContinuation(AwaiterStatus.Canceled); + InvokeContinuation(UniTaskStatus.Canceled); return false; } @@ -456,14 +456,14 @@ namespace UniRx.Async if (asyncOperation.isDone) { this.result = asyncOperation.asset; - InvokeContinuation(AwaiterStatus.Succeeded); + InvokeContinuation(UniTaskStatus.Succeeded); return false; } return true; } - void InvokeContinuation(AwaiterStatus status) + void InvokeContinuation(UniTaskStatus status) { this.status = status; var cont = this.continuation; @@ -552,12 +552,12 @@ namespace UniRx.Async GetResult(token); } - public AwaiterStatus GetStatus(short token) + public UniTaskStatus GetStatus(short token) { return core.GetStatus(token); } - public AwaiterStatus UnsafeGetStatus() + public UniTaskStatus UnsafeGetStatus() { return core.UnsafeGetStatus(); } @@ -616,30 +616,30 @@ namespace UniRx.Async { AssetBundleRequest asyncOperation; Action continuationAction; - AwaiterStatus status; + UniTaskStatus status; UnityEngine.Object result; public AssetBundleRequestAwaiter(AssetBundleRequest asyncOperation) { - this.status = asyncOperation.isDone ? AwaiterStatus.Succeeded : AwaiterStatus.Pending; + this.status = asyncOperation.isDone ? UniTaskStatus.Succeeded : UniTaskStatus.Pending; this.asyncOperation = (this.status.IsCompleted()) ? null : asyncOperation; this.result = (this.status.IsCompletedSuccessfully()) ? asyncOperation.asset : null; this.continuationAction = null; } public bool IsCompleted => status.IsCompleted(); - public AwaiterStatus Status => status; + public UniTaskStatus Status => status; public UnityEngine.Object GetResult() { - if (status == AwaiterStatus.Succeeded) return this.result; + if (status == UniTaskStatus.Succeeded) return this.result; - if (status == AwaiterStatus.Pending) + if (status == UniTaskStatus.Pending) { // first timing of call if (asyncOperation.isDone) { - status = AwaiterStatus.Succeeded; + status = UniTaskStatus.Succeeded; } else { @@ -683,15 +683,15 @@ namespace UniRx.Async AssetBundleRequest asyncOperation; IProgress progress; CancellationToken cancellationToken; - AwaiterStatus status; + UniTaskStatus status; Action continuation; UnityEngine.Object result; public AssetBundleRequestConfiguredAwaiter(AssetBundleRequest asyncOperation, IProgress progress, CancellationToken cancellationToken) { - this.status = cancellationToken.IsCancellationRequested ? AwaiterStatus.Canceled - : asyncOperation.isDone ? AwaiterStatus.Succeeded - : AwaiterStatus.Pending; + this.status = cancellationToken.IsCancellationRequested ? UniTaskStatus.Canceled + : asyncOperation.isDone ? UniTaskStatus.Succeeded + : UniTaskStatus.Pending; if (this.status.IsCompletedSuccessfully()) this.result = asyncOperation.asset; if (this.status.IsCompleted()) return; @@ -706,14 +706,14 @@ namespace UniRx.Async } public bool IsCompleted => status.IsCompleted(); - public AwaiterStatus Status => status; + public UniTaskStatus Status => status; void IAwaiter.GetResult() => GetResult(); public UnityEngine.Object GetResult() { - if (status == AwaiterStatus.Succeeded) return this.result; + if (status == UniTaskStatus.Succeeded) return this.result; - if (status == AwaiterStatus.Canceled) + if (status == UniTaskStatus.Canceled) { Error.ThrowOperationCanceledException(); } @@ -725,7 +725,7 @@ namespace UniRx.Async { if (cancellationToken.IsCancellationRequested) { - InvokeContinuation(AwaiterStatus.Canceled); + InvokeContinuation(UniTaskStatus.Canceled); return false; } @@ -737,14 +737,14 @@ namespace UniRx.Async if (asyncOperation.isDone) { this.result = asyncOperation.asset; - InvokeContinuation(AwaiterStatus.Succeeded); + InvokeContinuation(UniTaskStatus.Succeeded); return false; } return true; } - void InvokeContinuation(AwaiterStatus status) + void InvokeContinuation(UniTaskStatus status) { this.status = status; var cont = this.continuation; @@ -776,30 +776,30 @@ namespace UniRx.Async { AssetBundleCreateRequest asyncOperation; Action continuationAction; - AwaiterStatus status; + UniTaskStatus status; AssetBundle result; public AssetBundleCreateRequestAwaiter(AssetBundleCreateRequest asyncOperation) { - this.status = asyncOperation.isDone ? AwaiterStatus.Succeeded : AwaiterStatus.Pending; + this.status = asyncOperation.isDone ? UniTaskStatus.Succeeded : UniTaskStatus.Pending; this.asyncOperation = (this.status.IsCompleted()) ? null : asyncOperation; this.result = (this.status.IsCompletedSuccessfully()) ? asyncOperation.assetBundle : null; this.continuationAction = null; } public bool IsCompleted => status.IsCompleted(); - public AwaiterStatus Status => status; + public UniTaskStatus Status => status; public AssetBundle GetResult() { - if (status == AwaiterStatus.Succeeded) return this.result; + if (status == UniTaskStatus.Succeeded) return this.result; - if (status == AwaiterStatus.Pending) + if (status == UniTaskStatus.Pending) { // first timing of call if (asyncOperation.isDone) { - status = AwaiterStatus.Succeeded; + status = UniTaskStatus.Succeeded; } else { @@ -843,15 +843,15 @@ namespace UniRx.Async AssetBundleCreateRequest asyncOperation; IProgress progress; CancellationToken cancellationToken; - AwaiterStatus status; + UniTaskStatus status; Action continuation; AssetBundle result; public AssetBundleCreateRequestConfiguredAwaiter(AssetBundleCreateRequest asyncOperation, IProgress progress, CancellationToken cancellationToken) { - this.status = cancellationToken.IsCancellationRequested ? AwaiterStatus.Canceled - : asyncOperation.isDone ? AwaiterStatus.Succeeded - : AwaiterStatus.Pending; + this.status = cancellationToken.IsCancellationRequested ? UniTaskStatus.Canceled + : asyncOperation.isDone ? UniTaskStatus.Succeeded + : UniTaskStatus.Pending; if (this.status.IsCompletedSuccessfully()) this.result = asyncOperation.assetBundle; if (this.status.IsCompleted()) return; @@ -866,14 +866,14 @@ namespace UniRx.Async } public bool IsCompleted => status.IsCompleted(); - public AwaiterStatus Status => status; + public UniTaskStatus Status => status; void IAwaiter.GetResult() => GetResult(); public AssetBundle GetResult() { - if (status == AwaiterStatus.Succeeded) return this.result; + if (status == UniTaskStatus.Succeeded) return this.result; - if (status == AwaiterStatus.Canceled) + if (status == UniTaskStatus.Canceled) { Error.ThrowOperationCanceledException(); } @@ -885,7 +885,7 @@ namespace UniRx.Async { if (cancellationToken.IsCancellationRequested) { - InvokeContinuation(AwaiterStatus.Canceled); + InvokeContinuation(UniTaskStatus.Canceled); return false; } @@ -897,14 +897,14 @@ namespace UniRx.Async if (asyncOperation.isDone) { this.result = asyncOperation.assetBundle; - InvokeContinuation(AwaiterStatus.Succeeded); + InvokeContinuation(UniTaskStatus.Succeeded); return false; } return true; } - void InvokeContinuation(AwaiterStatus status) + void InvokeContinuation(UniTaskStatus status) { this.status = status; var cont = this.continuation; @@ -942,14 +942,14 @@ namespace UniRx.Async WWW asyncOperation; IProgress progress; CancellationToken cancellationToken; - AwaiterStatus status; + UniTaskStatus status; Action continuation; public WWWConfiguredAwaiter(WWW asyncOperation, IProgress progress, CancellationToken cancellationToken) { - this.status = cancellationToken.IsCancellationRequested ? AwaiterStatus.Canceled - : asyncOperation.isDone ? AwaiterStatus.Succeeded - : AwaiterStatus.Pending; + this.status = cancellationToken.IsCancellationRequested ? UniTaskStatus.Canceled + : asyncOperation.isDone ? UniTaskStatus.Succeeded + : UniTaskStatus.Pending; if (this.status.IsCompleted()) return; @@ -962,15 +962,15 @@ namespace UniRx.Async } public bool IsCompleted => status.IsCompleted(); - public AwaiterStatus Status => status; + public UniTaskStatus Status => status; public void GetResult() { - if (status == AwaiterStatus.Succeeded) + if (status == UniTaskStatus.Succeeded) { return; } - else if (status == AwaiterStatus.Canceled) + else if (status == UniTaskStatus.Canceled) { Error.ThrowOperationCanceledException(); } @@ -982,7 +982,7 @@ namespace UniRx.Async { if (cancellationToken.IsCancellationRequested) { - InvokeContinuation(AwaiterStatus.Canceled); + InvokeContinuation(UniTaskStatus.Canceled); return false; } @@ -993,14 +993,14 @@ namespace UniRx.Async if (asyncOperation.isDone) { - InvokeContinuation(AwaiterStatus.Succeeded); + InvokeContinuation(UniTaskStatus.Succeeded); return false; } return true; } - void InvokeContinuation(AwaiterStatus status) + void InvokeContinuation(UniTaskStatus status) { this.status = status; var cont = this.continuation; @@ -1039,30 +1039,30 @@ namespace UniRx.Async { UnityWebRequestAsyncOperation asyncOperation; Action continuationAction; - AwaiterStatus status; + UniTaskStatus status; UnityWebRequest result; public UnityWebRequestAsyncOperationAwaiter(UnityWebRequestAsyncOperation asyncOperation) { - this.status = asyncOperation.isDone ? AwaiterStatus.Succeeded : AwaiterStatus.Pending; + this.status = asyncOperation.isDone ? UniTaskStatus.Succeeded : UniTaskStatus.Pending; this.asyncOperation = (this.status.IsCompleted()) ? null : asyncOperation; this.result = (this.status.IsCompletedSuccessfully()) ? asyncOperation.webRequest : null; this.continuationAction = null; } public bool IsCompleted => status.IsCompleted(); - public AwaiterStatus Status => status; + public UniTaskStatus Status => status; public UnityWebRequest GetResult() { - if (status == AwaiterStatus.Succeeded) return this.result; + if (status == UniTaskStatus.Succeeded) return this.result; - if (status == AwaiterStatus.Pending) + if (status == UniTaskStatus.Pending) { // first timing of call if (asyncOperation.isDone) { - status = AwaiterStatus.Succeeded; + status = UniTaskStatus.Succeeded; } else { @@ -1107,15 +1107,15 @@ namespace UniRx.Async UnityWebRequestAsyncOperation asyncOperation; IProgress progress; CancellationToken cancellationToken; - AwaiterStatus status; + UniTaskStatus status; Action continuation; UnityWebRequest result; public UnityWebRequestAsyncOperationConfiguredAwaiter(UnityWebRequestAsyncOperation asyncOperation, IProgress progress, CancellationToken cancellationToken) { - this.status = cancellationToken.IsCancellationRequested ? AwaiterStatus.Canceled - : asyncOperation.isDone ? AwaiterStatus.Succeeded - : AwaiterStatus.Pending; + this.status = cancellationToken.IsCancellationRequested ? UniTaskStatus.Canceled + : asyncOperation.isDone ? UniTaskStatus.Succeeded + : UniTaskStatus.Pending; if (this.status.IsCompletedSuccessfully()) this.result = asyncOperation.webRequest; if (this.status.IsCompleted()) return; @@ -1130,14 +1130,14 @@ namespace UniRx.Async } public bool IsCompleted => status.IsCompleted(); - public AwaiterStatus Status => status; + public UniTaskStatus Status => status; void IAwaiter.GetResult() => GetResult(); public UnityWebRequest GetResult() { - if (status == AwaiterStatus.Succeeded) return this.result; + if (status == UniTaskStatus.Succeeded) return this.result; - if (status == AwaiterStatus.Canceled) + if (status == UniTaskStatus.Canceled) { Error.ThrowOperationCanceledException(); } @@ -1149,7 +1149,7 @@ namespace UniRx.Async { if (cancellationToken.IsCancellationRequested) { - InvokeContinuation(AwaiterStatus.Canceled); + InvokeContinuation(UniTaskStatus.Canceled); return false; } @@ -1161,14 +1161,14 @@ namespace UniRx.Async if (asyncOperation.isDone) { this.result = asyncOperation.webRequest; - InvokeContinuation(AwaiterStatus.Succeeded); + InvokeContinuation(UniTaskStatus.Succeeded); return false; } return true; } - void InvokeContinuation(AwaiterStatus status) + void InvokeContinuation(UniTaskStatus status) { this.status = status; var cont = this.continuation; diff --git a/Assets/UniRx.Async/UnityAsyncExtensions.uGUI.cs b/Assets/UniRx.Async/UnityAsyncExtensions.uGUI.cs index b0effcb..45d9da9 100644 --- a/Assets/UniRx.Async/UnityAsyncExtensions.uGUI.cs +++ b/Assets/UniRx.Async/UnityAsyncExtensions.uGUI.cs @@ -265,7 +265,7 @@ namespace UniRx.Async } bool IAwaiter.IsCompleted => isDisposed ? true : false; - AwaiterStatus IAwaiter.Status => isDisposed ? AwaiterStatus.Canceled : AwaiterStatus.Pending; + UniTaskStatus IAwaiter.Status => isDisposed ? UniTaskStatus.Canceled : UniTaskStatus.Pending; void IAwaiter.GetResult() { if (isDisposed) throw new OperationCanceledException(); @@ -381,7 +381,7 @@ namespace UniRx.Async } bool IAwaiter.IsCompleted => isDisposed ? true : false; - AwaiterStatus IAwaiter.Status => isDisposed ? AwaiterStatus.Canceled : AwaiterStatus.Pending; + UniTaskStatus IAwaiter.Status => isDisposed ? UniTaskStatus.Canceled : UniTaskStatus.Pending; T IAwaiter.GetResult() {