mirror of https://github.com/Cysharp/UniTask
WIP reuse and cancellation
parent
9a3ec31533
commit
936b224b2b
|
@ -135,8 +135,8 @@ namespace Cysharp.Threading.Tasks
|
||||||
bool TryReturn()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
|
||||||
cancellationTokenRegistration.Dispose();
|
cancellationTokenRegistration.Dispose();
|
||||||
|
core.Reset();
|
||||||
cancellationTokenRegistration = default;
|
cancellationTokenRegistration = default;
|
||||||
parent.triggerEvent.Remove(this);
|
parent.triggerEvent.Remove(this);
|
||||||
parent = null;
|
parent = null;
|
||||||
|
@ -453,8 +453,8 @@ namespace Cysharp.Threading.Tasks
|
||||||
bool TryReturn()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
|
||||||
cancellationTokenRegistration.Dispose();
|
cancellationTokenRegistration.Dispose();
|
||||||
|
core.Reset();
|
||||||
cancellationTokenRegistration = default;
|
cancellationTokenRegistration = default;
|
||||||
parent.triggerEvent.Remove(this);
|
parent.triggerEvent.Remove(this);
|
||||||
parent = null;
|
parent = null;
|
||||||
|
|
|
@ -353,8 +353,8 @@ namespace Cysharp.Threading.Tasks
|
||||||
bool TryReturn()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
|
||||||
cancellationRegistration.Dispose();
|
cancellationRegistration.Dispose();
|
||||||
|
core.Reset();
|
||||||
|
|
||||||
RestoreOriginalCallback();
|
RestoreOriginalCallback();
|
||||||
|
|
||||||
|
|
|
@ -255,7 +255,7 @@ namespace Cysharp.Threading.Tasks
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
|
if (!cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
TryReturn();
|
TryReturn();
|
||||||
}
|
}
|
||||||
|
@ -289,6 +289,7 @@ namespace Cysharp.Threading.Tasks
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
core.TrySetResult(null);
|
core.TrySetResult(null);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -296,9 +297,9 @@ namespace Cysharp.Threading.Tasks
|
||||||
bool TryReturn()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
TaskTracker.RemoveTracking(this);
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
core.Reset();
|
core.Reset();
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
cancellationTokenRegistration.Dispose();
|
|
||||||
cancelImmediately = default;
|
cancelImmediately = default;
|
||||||
return pool.TryPush(this);
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
@ -366,7 +367,7 @@ namespace Cysharp.Threading.Tasks
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
|
if (!cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
TryReturn();
|
TryReturn();
|
||||||
}
|
}
|
||||||
|
@ -412,9 +413,9 @@ namespace Cysharp.Threading.Tasks
|
||||||
bool TryReturn()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
TaskTracker.RemoveTracking(this);
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
core.Reset();
|
core.Reset();
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
cancellationTokenRegistration.Dispose();
|
|
||||||
return pool.TryPush(this);
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,7 @@ namespace Cysharp.Threading.Tasks
|
||||||
return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token);
|
return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !UNITY_2023_1_OR_NEWER
|
||||||
public struct AsyncOperationAwaiter : ICriticalNotifyCompletion
|
public struct AsyncOperationAwaiter : ICriticalNotifyCompletion
|
||||||
{
|
{
|
||||||
AsyncOperation asyncOperation;
|
AsyncOperation asyncOperation;
|
||||||
|
@ -81,6 +82,7 @@ namespace Cysharp.Threading.Tasks
|
||||||
asyncOperation.completed += continuationAction;
|
asyncOperation.completed += continuationAction;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
sealed class AsyncOperationConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<AsyncOperationConfiguredSource>
|
sealed class AsyncOperationConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<AsyncOperationConfiguredSource>
|
||||||
{
|
{
|
||||||
|
@ -99,6 +101,7 @@ namespace Cysharp.Threading.Tasks
|
||||||
CancellationTokenRegistration cancellationTokenRegistration;
|
CancellationTokenRegistration cancellationTokenRegistration;
|
||||||
bool cancelImmediately;
|
bool cancelImmediately;
|
||||||
bool completed;
|
bool completed;
|
||||||
|
bool allowReturn;
|
||||||
|
|
||||||
UniTaskCompletionSourceCore<AsyncUnit> core;
|
UniTaskCompletionSourceCore<AsyncUnit> core;
|
||||||
|
|
||||||
|
@ -126,6 +129,7 @@ namespace Cysharp.Threading.Tasks
|
||||||
result.cancellationToken = cancellationToken;
|
result.cancellationToken = cancellationToken;
|
||||||
result.cancelImmediately = cancelImmediately;
|
result.cancelImmediately = cancelImmediately;
|
||||||
result.completed = false;
|
result.completed = false;
|
||||||
|
result.allowReturn = false;
|
||||||
|
|
||||||
asyncOperation.completed += result.continuationAction;
|
asyncOperation.completed += result.continuationAction;
|
||||||
|
|
||||||
|
@ -154,11 +158,18 @@ namespace Cysharp.Threading.Tasks
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
|
if (!cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
if (allowReturn)
|
||||||
{
|
{
|
||||||
TryReturn();
|
TryReturn();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
allowReturn = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
TaskTracker.RemoveTracking(this);
|
||||||
}
|
}
|
||||||
|
@ -183,9 +194,15 @@ namespace Cysharp.Threading.Tasks
|
||||||
|
|
||||||
public bool MoveNext()
|
public bool MoveNext()
|
||||||
{
|
{
|
||||||
// Already completed
|
if (allowReturn)
|
||||||
if (completed || asyncOperation == null)
|
|
||||||
{
|
{
|
||||||
|
TryReturn();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (completed)
|
||||||
|
{
|
||||||
|
allowReturn = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,6 +219,8 @@ namespace Cysharp.Threading.Tasks
|
||||||
|
|
||||||
if (asyncOperation.isDone)
|
if (asyncOperation.isDone)
|
||||||
{
|
{
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
|
allowReturn = true;
|
||||||
core.TrySetResult(AsyncUnit.Default);
|
core.TrySetResult(AsyncUnit.Default);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -212,29 +231,31 @@ namespace Cysharp.Threading.Tasks
|
||||||
bool TryReturn()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
TaskTracker.RemoveTracking(this);
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
core.Reset();
|
core.Reset();
|
||||||
asyncOperation.completed -= continuationAction;
|
asyncOperation.completed -= continuationAction;
|
||||||
asyncOperation = default;
|
asyncOperation = default;
|
||||||
progress = default;
|
progress = default;
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
cancellationTokenRegistration.Dispose();
|
|
||||||
cancelImmediately = default;
|
cancelImmediately = default;
|
||||||
return pool.TryPush(this);
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Continuation(AsyncOperation _)
|
void Continuation(AsyncOperation _)
|
||||||
{
|
{
|
||||||
if (completed)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
completed = true;
|
completed = true;
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
if (cancellationToken.IsCancellationRequested)
|
if (cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
core.TrySetCanceled(cancellationToken);
|
core.TrySetCanceled(cancellationToken);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
if (progress != null)
|
||||||
|
{
|
||||||
|
progress.Report(asyncOperation.progress);
|
||||||
|
}
|
||||||
|
|
||||||
core.TrySetResult(AsyncUnit.Default);
|
core.TrySetResult(AsyncUnit.Default);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -329,6 +350,7 @@ namespace Cysharp.Threading.Tasks
|
||||||
CancellationTokenRegistration cancellationTokenRegistration;
|
CancellationTokenRegistration cancellationTokenRegistration;
|
||||||
bool cancelImmediately;
|
bool cancelImmediately;
|
||||||
bool completed;
|
bool completed;
|
||||||
|
bool allowReturn;
|
||||||
|
|
||||||
UniTaskCompletionSourceCore<UnityEngine.Object> core;
|
UniTaskCompletionSourceCore<UnityEngine.Object> core;
|
||||||
|
|
||||||
|
@ -356,6 +378,7 @@ namespace Cysharp.Threading.Tasks
|
||||||
result.cancellationToken = cancellationToken;
|
result.cancellationToken = cancellationToken;
|
||||||
result.cancelImmediately = cancelImmediately;
|
result.cancelImmediately = cancelImmediately;
|
||||||
result.completed = false;
|
result.completed = false;
|
||||||
|
result.allowReturn = false;
|
||||||
|
|
||||||
asyncOperation.completed += result.continuationAction;
|
asyncOperation.completed += result.continuationAction;
|
||||||
|
|
||||||
|
@ -384,11 +407,18 @@ namespace Cysharp.Threading.Tasks
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
|
if (!cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
if (allowReturn)
|
||||||
{
|
{
|
||||||
TryReturn();
|
TryReturn();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
allowReturn = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
TaskTracker.RemoveTracking(this);
|
||||||
}
|
}
|
||||||
|
@ -417,9 +447,15 @@ namespace Cysharp.Threading.Tasks
|
||||||
|
|
||||||
public bool MoveNext()
|
public bool MoveNext()
|
||||||
{
|
{
|
||||||
// Already completed
|
if (allowReturn)
|
||||||
if (completed || asyncOperation == null)
|
|
||||||
{
|
{
|
||||||
|
TryReturn();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (completed)
|
||||||
|
{
|
||||||
|
allowReturn = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -436,6 +472,8 @@ namespace Cysharp.Threading.Tasks
|
||||||
|
|
||||||
if (asyncOperation.isDone)
|
if (asyncOperation.isDone)
|
||||||
{
|
{
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
|
allowReturn = true;
|
||||||
core.TrySetResult(asyncOperation.asset);
|
core.TrySetResult(asyncOperation.asset);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -446,29 +484,31 @@ namespace Cysharp.Threading.Tasks
|
||||||
bool TryReturn()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
TaskTracker.RemoveTracking(this);
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
core.Reset();
|
core.Reset();
|
||||||
asyncOperation.completed -= continuationAction;
|
asyncOperation.completed -= continuationAction;
|
||||||
asyncOperation = default;
|
asyncOperation = default;
|
||||||
progress = default;
|
progress = default;
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
cancellationTokenRegistration.Dispose();
|
|
||||||
cancelImmediately = default;
|
cancelImmediately = default;
|
||||||
return pool.TryPush(this);
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Continuation(AsyncOperation _)
|
void Continuation(AsyncOperation _)
|
||||||
{
|
{
|
||||||
if (completed)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
completed = true;
|
completed = true;
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
if (cancellationToken.IsCancellationRequested)
|
if (cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
core.TrySetCanceled(cancellationToken);
|
core.TrySetCanceled(cancellationToken);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
if (progress != null)
|
||||||
|
{
|
||||||
|
progress.Report(asyncOperation.progress);
|
||||||
|
}
|
||||||
|
|
||||||
core.TrySetResult(asyncOperation.asset);
|
core.TrySetResult(asyncOperation.asset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -564,6 +604,7 @@ namespace Cysharp.Threading.Tasks
|
||||||
CancellationTokenRegistration cancellationTokenRegistration;
|
CancellationTokenRegistration cancellationTokenRegistration;
|
||||||
bool cancelImmediately;
|
bool cancelImmediately;
|
||||||
bool completed;
|
bool completed;
|
||||||
|
bool allowReturn;
|
||||||
|
|
||||||
UniTaskCompletionSourceCore<UnityEngine.Object> core;
|
UniTaskCompletionSourceCore<UnityEngine.Object> core;
|
||||||
|
|
||||||
|
@ -591,6 +632,7 @@ namespace Cysharp.Threading.Tasks
|
||||||
result.cancellationToken = cancellationToken;
|
result.cancellationToken = cancellationToken;
|
||||||
result.cancelImmediately = cancelImmediately;
|
result.cancelImmediately = cancelImmediately;
|
||||||
result.completed = false;
|
result.completed = false;
|
||||||
|
result.allowReturn = false;
|
||||||
|
|
||||||
asyncOperation.completed += result.continuationAction;
|
asyncOperation.completed += result.continuationAction;
|
||||||
|
|
||||||
|
@ -619,11 +661,18 @@ namespace Cysharp.Threading.Tasks
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
|
if (!cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
if (allowReturn)
|
||||||
{
|
{
|
||||||
TryReturn();
|
TryReturn();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
allowReturn = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
TaskTracker.RemoveTracking(this);
|
||||||
}
|
}
|
||||||
|
@ -652,9 +701,15 @@ namespace Cysharp.Threading.Tasks
|
||||||
|
|
||||||
public bool MoveNext()
|
public bool MoveNext()
|
||||||
{
|
{
|
||||||
// Already completed
|
if (allowReturn)
|
||||||
if (completed || asyncOperation == null)
|
|
||||||
{
|
{
|
||||||
|
TryReturn();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (completed)
|
||||||
|
{
|
||||||
|
allowReturn = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -671,6 +726,8 @@ namespace Cysharp.Threading.Tasks
|
||||||
|
|
||||||
if (asyncOperation.isDone)
|
if (asyncOperation.isDone)
|
||||||
{
|
{
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
|
allowReturn = true;
|
||||||
core.TrySetResult(asyncOperation.asset);
|
core.TrySetResult(asyncOperation.asset);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -681,29 +738,31 @@ namespace Cysharp.Threading.Tasks
|
||||||
bool TryReturn()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
TaskTracker.RemoveTracking(this);
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
core.Reset();
|
core.Reset();
|
||||||
asyncOperation.completed -= continuationAction;
|
asyncOperation.completed -= continuationAction;
|
||||||
asyncOperation = default;
|
asyncOperation = default;
|
||||||
progress = default;
|
progress = default;
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
cancellationTokenRegistration.Dispose();
|
|
||||||
cancelImmediately = default;
|
cancelImmediately = default;
|
||||||
return pool.TryPush(this);
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Continuation(AsyncOperation _)
|
void Continuation(AsyncOperation _)
|
||||||
{
|
{
|
||||||
if (completed)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
completed = true;
|
completed = true;
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
if (cancellationToken.IsCancellationRequested)
|
if (cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
core.TrySetCanceled(cancellationToken);
|
core.TrySetCanceled(cancellationToken);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
if (progress != null)
|
||||||
|
{
|
||||||
|
progress.Report(asyncOperation.progress);
|
||||||
|
}
|
||||||
|
|
||||||
core.TrySetResult(asyncOperation.asset);
|
core.TrySetResult(asyncOperation.asset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -800,6 +859,7 @@ namespace Cysharp.Threading.Tasks
|
||||||
CancellationTokenRegistration cancellationTokenRegistration;
|
CancellationTokenRegistration cancellationTokenRegistration;
|
||||||
bool cancelImmediately;
|
bool cancelImmediately;
|
||||||
bool completed;
|
bool completed;
|
||||||
|
bool allowReturn;
|
||||||
|
|
||||||
UniTaskCompletionSourceCore<AssetBundle> core;
|
UniTaskCompletionSourceCore<AssetBundle> core;
|
||||||
|
|
||||||
|
@ -827,6 +887,7 @@ namespace Cysharp.Threading.Tasks
|
||||||
result.cancellationToken = cancellationToken;
|
result.cancellationToken = cancellationToken;
|
||||||
result.cancelImmediately = cancelImmediately;
|
result.cancelImmediately = cancelImmediately;
|
||||||
result.completed = false;
|
result.completed = false;
|
||||||
|
result.allowReturn = false;
|
||||||
|
|
||||||
asyncOperation.completed += result.continuationAction;
|
asyncOperation.completed += result.continuationAction;
|
||||||
|
|
||||||
|
@ -855,11 +916,18 @@ namespace Cysharp.Threading.Tasks
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
|
if (!cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
if (allowReturn)
|
||||||
{
|
{
|
||||||
TryReturn();
|
TryReturn();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
allowReturn = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
TaskTracker.RemoveTracking(this);
|
||||||
}
|
}
|
||||||
|
@ -888,9 +956,15 @@ namespace Cysharp.Threading.Tasks
|
||||||
|
|
||||||
public bool MoveNext()
|
public bool MoveNext()
|
||||||
{
|
{
|
||||||
// Already completed
|
if (allowReturn)
|
||||||
if (completed || asyncOperation == null)
|
|
||||||
{
|
{
|
||||||
|
TryReturn();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (completed)
|
||||||
|
{
|
||||||
|
allowReturn = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -907,6 +981,8 @@ namespace Cysharp.Threading.Tasks
|
||||||
|
|
||||||
if (asyncOperation.isDone)
|
if (asyncOperation.isDone)
|
||||||
{
|
{
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
|
allowReturn = true;
|
||||||
core.TrySetResult(asyncOperation.assetBundle);
|
core.TrySetResult(asyncOperation.assetBundle);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -916,30 +992,32 @@ namespace Cysharp.Threading.Tasks
|
||||||
|
|
||||||
bool TryReturn()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
TaskTracker.RemoveTracking(this);
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
core.Reset();
|
||||||
asyncOperation.completed -= continuationAction;
|
asyncOperation.completed -= continuationAction;
|
||||||
asyncOperation = default;
|
asyncOperation = default;
|
||||||
progress = default;
|
progress = default;
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
cancellationTokenRegistration.Dispose();
|
|
||||||
cancelImmediately = default;
|
cancelImmediately = default;
|
||||||
return pool.TryPush(this);
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Continuation(AsyncOperation _)
|
void Continuation(AsyncOperation _)
|
||||||
{
|
{
|
||||||
if (completed)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
completed = true;
|
completed = true;
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
if (cancellationToken.IsCancellationRequested)
|
if (cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
core.TrySetCanceled(cancellationToken);
|
core.TrySetCanceled(cancellationToken);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
if (progress != null)
|
||||||
|
{
|
||||||
|
progress.Report(asyncOperation.progress);
|
||||||
|
}
|
||||||
|
|
||||||
core.TrySetResult(asyncOperation.assetBundle);
|
core.TrySetResult(asyncOperation.assetBundle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1051,6 +1129,7 @@ namespace Cysharp.Threading.Tasks
|
||||||
CancellationTokenRegistration cancellationTokenRegistration;
|
CancellationTokenRegistration cancellationTokenRegistration;
|
||||||
bool cancelImmediately;
|
bool cancelImmediately;
|
||||||
bool completed;
|
bool completed;
|
||||||
|
bool allowReturn;
|
||||||
|
|
||||||
UniTaskCompletionSourceCore<UnityWebRequest> core;
|
UniTaskCompletionSourceCore<UnityWebRequest> core;
|
||||||
|
|
||||||
|
@ -1078,6 +1157,7 @@ namespace Cysharp.Threading.Tasks
|
||||||
result.cancellationToken = cancellationToken;
|
result.cancellationToken = cancellationToken;
|
||||||
result.cancelImmediately = cancelImmediately;
|
result.cancelImmediately = cancelImmediately;
|
||||||
result.completed = false;
|
result.completed = false;
|
||||||
|
result.allowReturn = false;
|
||||||
|
|
||||||
asyncOperation.completed += result.continuationAction;
|
asyncOperation.completed += result.continuationAction;
|
||||||
|
|
||||||
|
@ -1107,11 +1187,18 @@ namespace Cysharp.Threading.Tasks
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
|
if (!cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
if (allowReturn)
|
||||||
{
|
{
|
||||||
TryReturn();
|
TryReturn();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
allowReturn = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
TaskTracker.RemoveTracking(this);
|
TaskTracker.RemoveTracking(this);
|
||||||
}
|
}
|
||||||
|
@ -1140,15 +1227,20 @@ namespace Cysharp.Threading.Tasks
|
||||||
|
|
||||||
public bool MoveNext()
|
public bool MoveNext()
|
||||||
{
|
{
|
||||||
// Already completed
|
if (allowReturn)
|
||||||
if (completed || asyncOperation == null)
|
|
||||||
{
|
{
|
||||||
|
TryReturn();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (completed)
|
||||||
|
{
|
||||||
|
allowReturn = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cancellationToken.IsCancellationRequested)
|
if (cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
asyncOperation.webRequest.Abort();
|
|
||||||
core.TrySetCanceled(cancellationToken);
|
core.TrySetCanceled(cancellationToken);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -1160,14 +1252,9 @@ namespace Cysharp.Threading.Tasks
|
||||||
|
|
||||||
if (asyncOperation.isDone)
|
if (asyncOperation.isDone)
|
||||||
{
|
{
|
||||||
if (asyncOperation.webRequest.IsError())
|
cancellationTokenRegistration.Dispose();
|
||||||
{
|
allowReturn = true;
|
||||||
core.TrySetException(new UnityWebRequestException(asyncOperation.webRequest));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
core.TrySetResult(asyncOperation.webRequest);
|
core.TrySetResult(asyncOperation.webRequest);
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1176,24 +1263,22 @@ namespace Cysharp.Threading.Tasks
|
||||||
|
|
||||||
bool TryReturn()
|
bool TryReturn()
|
||||||
{
|
{
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
TaskTracker.RemoveTracking(this);
|
TaskTracker.RemoveTracking(this);
|
||||||
core.Reset();
|
core.Reset();
|
||||||
asyncOperation.completed -= continuationAction;
|
asyncOperation.completed -= continuationAction;
|
||||||
asyncOperation = default;
|
asyncOperation = default;
|
||||||
progress = default;
|
progress = default;
|
||||||
cancellationToken = default;
|
cancellationToken = default;
|
||||||
cancellationTokenRegistration.Dispose();
|
|
||||||
cancelImmediately = default;
|
cancelImmediately = default;
|
||||||
return pool.TryPush(this);
|
return pool.TryPush(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Continuation(AsyncOperation _)
|
void Continuation(AsyncOperation _)
|
||||||
{
|
{
|
||||||
if (completed)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
completed = true;
|
completed = true;
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
|
|
||||||
if (cancellationToken.IsCancellationRequested)
|
if (cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
core.TrySetCanceled(cancellationToken);
|
core.TrySetCanceled(cancellationToken);
|
||||||
|
@ -1204,6 +1289,11 @@ namespace Cysharp.Threading.Tasks
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
if (progress != null)
|
||||||
|
{
|
||||||
|
progress.Report(asyncOperation.progress);
|
||||||
|
}
|
||||||
|
|
||||||
core.TrySetResult(asyncOperation.webRequest);
|
core.TrySetResult(asyncOperation.webRequest);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: b055f3837a55b4a44abf9bf4bcb3594f
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -0,0 +1 @@
|
||||||
|
MyTEST
|
|
@ -0,0 +1,7 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 49ccc900467d35543bb602a3de4d786a
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -21,7 +21,6 @@ using System.Linq.Expressions;
|
||||||
using UnityEngine.Events;
|
using UnityEngine.Events;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// using DG.Tweening;
|
// using DG.Tweening;
|
||||||
|
|
||||||
|
|
||||||
|
@ -592,69 +591,78 @@ public class SandboxMain : MonoBehaviour
|
||||||
|
|
||||||
async UniTaskVoid Start()
|
async UniTaskVoid Start()
|
||||||
{
|
{
|
||||||
await new WhenEachTest().Each();
|
var p = Progress.Create<float>(x =>
|
||||||
|
|
||||||
|
|
||||||
// UniTask.Delay(TimeSpan.FromSeconds(1)).TimeoutWithoutException
|
|
||||||
|
|
||||||
|
|
||||||
var currentLoop = PlayerLoop.GetDefaultPlayerLoop();
|
|
||||||
PlayerLoopHelper.Initialize(ref currentLoop, InjectPlayerLoopTimings.Minimum); // minimum is Update | FixedUpdate | LastPostLateUpdate
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// TestAsync(cts.Token).Forget();
|
|
||||||
|
|
||||||
okButton.onClick.AddListener(UniTask.UnityAction(async () =>
|
|
||||||
{
|
{
|
||||||
await UniTask.WaitForEndOfFrame(this);
|
Debug.Log(x);
|
||||||
var texture = new Texture2D(Screen.width, Screen.height);
|
});
|
||||||
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
|
var foo = await Resources.LoadAsync("test2").ToUniTask(progress: p) as TextAsset;
|
||||||
texture.Apply();
|
Debug.Log(foo.text);
|
||||||
|
//var foo2 = await Resources.LoadAsync("test2").ToUniTask() as TextAsset;
|
||||||
|
//Debug.Log(foo2.text); // onemore?
|
||||||
|
|
||||||
var jpg = texture.EncodeToJPG();
|
|
||||||
File.WriteAllBytes("testscreencapture.jpg", jpg);
|
|
||||||
Debug.Log("ok?");
|
|
||||||
|
|
||||||
//var texture = ScreenCapture.CaptureScreenshotAsTexture();
|
|
||||||
//if (texture == null)
|
//await new WhenEachTest().Each();
|
||||||
//{
|
|
||||||
// Debug.Log("fail");
|
|
||||||
//}
|
//// UniTask.Delay(TimeSpan.FromSeconds(1)).TimeoutWithoutException
|
||||||
//else
|
|
||||||
|
|
||||||
|
//var currentLoop = PlayerLoop.GetDefaultPlayerLoop();
|
||||||
|
//PlayerLoopHelper.Initialize(ref currentLoop, InjectPlayerLoopTimings.Minimum); // minimum is Update | FixedUpdate | LastPostLateUpdate
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//// TestAsync(cts.Token).Forget();
|
||||||
|
|
||||||
|
//okButton.onClick.AddListener(UniTask.UnityAction(async () =>
|
||||||
//{
|
//{
|
||||||
|
// await UniTask.WaitForEndOfFrame(this);
|
||||||
|
// var texture = new Texture2D(Screen.width, Screen.height);
|
||||||
|
// texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
|
||||||
|
// texture.Apply();
|
||||||
|
|
||||||
// var jpg = texture.EncodeToJPG();
|
// var jpg = texture.EncodeToJPG();
|
||||||
// File.WriteAllBytes("testscreencapture.jpg", jpg);
|
// File.WriteAllBytes("testscreencapture.jpg", jpg);
|
||||||
// Debug.Log("ok?");
|
// Debug.Log("ok?");
|
||||||
//}
|
|
||||||
}));
|
|
||||||
|
|
||||||
cancelButton.onClick.AddListener(UniTask.UnityAction(async () =>
|
// //var texture = ScreenCapture.CaptureScreenshotAsTexture();
|
||||||
{
|
// //if (texture == null)
|
||||||
//clickCancelSource.Cancel();
|
// //{
|
||||||
|
// // Debug.Log("fail");
|
||||||
|
// //}
|
||||||
|
// //else
|
||||||
|
// //{
|
||||||
|
// // var jpg = texture.EncodeToJPG();
|
||||||
|
// // File.WriteAllBytes("testscreencapture.jpg", jpg);
|
||||||
|
// // Debug.Log("ok?");
|
||||||
|
// //}
|
||||||
|
//}));
|
||||||
|
|
||||||
//RunCheck(PlayerLoopTiming.Initialization).Forget();
|
//cancelButton.onClick.AddListener(UniTask.UnityAction(async () =>
|
||||||
//RunCheck(PlayerLoopTiming.LastInitialization).Forget();
|
//{
|
||||||
//RunCheck(PlayerLoopTiming.EarlyUpdate).Forget();
|
// //clickCancelSource.Cancel();
|
||||||
//RunCheck(PlayerLoopTiming.LastEarlyUpdate).Forget();
|
|
||||||
//RunCheck(PlayerLoopTiming.FixedUpdate).Forget();
|
|
||||||
//RunCheck(PlayerLoopTiming.LastFixedUpdate).Forget();
|
|
||||||
//RunCheck(PlayerLoopTiming.PreUpdate).Forget();
|
|
||||||
//RunCheck(PlayerLoopTiming.LastPreUpdate).Forget();
|
|
||||||
//RunCheck(PlayerLoopTiming.Update).Forget();
|
|
||||||
//RunCheck(PlayerLoopTiming.LastUpdate).Forget();
|
|
||||||
//RunCheck(PlayerLoopTiming.PreLateUpdate).Forget();
|
|
||||||
//RunCheck(PlayerLoopTiming.LastPreLateUpdate).Forget();
|
|
||||||
//RunCheck(PlayerLoopTiming.PostLateUpdate).Forget();
|
|
||||||
//RunCheck(PlayerLoopTiming.LastPostLateUpdate).Forget();
|
|
||||||
|
|
||||||
await UniTask.Yield();
|
// //RunCheck(PlayerLoopTiming.Initialization).Forget();
|
||||||
}));
|
// //RunCheck(PlayerLoopTiming.LastInitialization).Forget();
|
||||||
|
// //RunCheck(PlayerLoopTiming.EarlyUpdate).Forget();
|
||||||
|
// //RunCheck(PlayerLoopTiming.LastEarlyUpdate).Forget();
|
||||||
|
// //RunCheck(PlayerLoopTiming.FixedUpdate).Forget();
|
||||||
|
// //RunCheck(PlayerLoopTiming.LastFixedUpdate).Forget();
|
||||||
|
// //RunCheck(PlayerLoopTiming.PreUpdate).Forget();
|
||||||
|
// //RunCheck(PlayerLoopTiming.LastPreUpdate).Forget();
|
||||||
|
// //RunCheck(PlayerLoopTiming.Update).Forget();
|
||||||
|
// //RunCheck(PlayerLoopTiming.LastUpdate).Forget();
|
||||||
|
// //RunCheck(PlayerLoopTiming.PreLateUpdate).Forget();
|
||||||
|
// //RunCheck(PlayerLoopTiming.LastPreLateUpdate).Forget();
|
||||||
|
// //RunCheck(PlayerLoopTiming.PostLateUpdate).Forget();
|
||||||
|
// //RunCheck(PlayerLoopTiming.LastPostLateUpdate).Forget();
|
||||||
|
|
||||||
await UniTask.Yield();
|
// await UniTask.Yield();
|
||||||
|
//}));
|
||||||
}
|
}
|
||||||
|
|
||||||
async UniTaskVoid RunCheck(PlayerLoopTiming timing)
|
async UniTaskVoid RunCheck(PlayerLoopTiming timing)
|
||||||
|
|
Loading…
Reference in New Issue