WIP reuse and cancellation

v2.5.11
neuecc 2024-10-25 11:51:55 +09:00
parent 9a3ec31533
commit 936b224b2b
8 changed files with 239 additions and 124 deletions

View File

@ -135,8 +135,8 @@ namespace Cysharp.Threading.Tasks
bool TryReturn()
{
TaskTracker.RemoveTracking(this);
core.Reset();
cancellationTokenRegistration.Dispose();
core.Reset();
cancellationTokenRegistration = default;
parent.triggerEvent.Remove(this);
parent = null;
@ -453,8 +453,8 @@ namespace Cysharp.Threading.Tasks
bool TryReturn()
{
TaskTracker.RemoveTracking(this);
core.Reset();
cancellationTokenRegistration.Dispose();
core.Reset();
cancellationTokenRegistration = default;
parent.triggerEvent.Remove(this);
parent = null;

View File

@ -353,8 +353,8 @@ namespace Cysharp.Threading.Tasks
bool TryReturn()
{
TaskTracker.RemoveTracking(this);
core.Reset();
cancellationRegistration.Dispose();
core.Reset();
RestoreOriginalCallback();

View File

@ -255,7 +255,7 @@ namespace Cysharp.Threading.Tasks
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
if (!cancellationToken.IsCancellationRequested)
{
TryReturn();
}
@ -289,6 +289,7 @@ namespace Cysharp.Threading.Tasks
return false;
}
cancellationTokenRegistration.Dispose();
core.TrySetResult(null);
return false;
}
@ -296,9 +297,9 @@ namespace Cysharp.Threading.Tasks
bool TryReturn()
{
TaskTracker.RemoveTracking(this);
cancellationTokenRegistration.Dispose();
core.Reset();
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
@ -366,7 +367,7 @@ namespace Cysharp.Threading.Tasks
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
if (!cancellationToken.IsCancellationRequested)
{
TryReturn();
}
@ -412,9 +413,9 @@ namespace Cysharp.Threading.Tasks
bool TryReturn()
{
TaskTracker.RemoveTracking(this);
cancellationTokenRegistration.Dispose();
core.Reset();
cancellationToken = default;
cancellationTokenRegistration.Dispose();
return pool.TryPush(this);
}
}

View File

@ -42,6 +42,7 @@ namespace Cysharp.Threading.Tasks
return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, cancelImmediately, out var token), token);
}
#if !UNITY_2023_1_OR_NEWER
public struct AsyncOperationAwaiter : ICriticalNotifyCompletion
{
AsyncOperation asyncOperation;
@ -81,6 +82,7 @@ namespace Cysharp.Threading.Tasks
asyncOperation.completed += continuationAction;
}
}
#endif
sealed class AsyncOperationConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<AsyncOperationConfiguredSource>
{
@ -99,6 +101,7 @@ namespace Cysharp.Threading.Tasks
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
bool completed;
bool allowReturn;
UniTaskCompletionSourceCore<AsyncUnit> core;
@ -126,7 +129,8 @@ namespace Cysharp.Threading.Tasks
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
result.completed = false;
result.allowReturn = false;
asyncOperation.completed += result.continuationAction;
if (cancelImmediately && cancellationToken.CanBeCanceled)
@ -154,9 +158,16 @@ namespace Cysharp.Threading.Tasks
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
if (!cancellationToken.IsCancellationRequested)
{
TryReturn();
if (allowReturn)
{
TryReturn();
}
else
{
allowReturn = true;
}
}
else
{
@ -183,9 +194,15 @@ namespace Cysharp.Threading.Tasks
public bool MoveNext()
{
// Already completed
if (completed || asyncOperation == null)
if (allowReturn)
{
TryReturn();
return false;
}
if (completed)
{
allowReturn = true;
return false;
}
@ -202,6 +219,8 @@ namespace Cysharp.Threading.Tasks
if (asyncOperation.isDone)
{
cancellationTokenRegistration.Dispose();
allowReturn = true;
core.TrySetResult(AsyncUnit.Default);
return false;
}
@ -212,29 +231,31 @@ namespace Cysharp.Threading.Tasks
bool TryReturn()
{
TaskTracker.RemoveTracking(this);
cancellationTokenRegistration.Dispose();
core.Reset();
asyncOperation.completed -= continuationAction;
asyncOperation = default;
progress = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
void Continuation(AsyncOperation _)
{
if (completed)
{
return;
}
completed = true;
cancellationTokenRegistration.Dispose();
if (cancellationToken.IsCancellationRequested)
{
core.TrySetCanceled(cancellationToken);
}
else
{
if (progress != null)
{
progress.Report(asyncOperation.progress);
}
core.TrySetResult(AsyncUnit.Default);
}
}
@ -329,6 +350,7 @@ namespace Cysharp.Threading.Tasks
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
bool completed;
bool allowReturn;
UniTaskCompletionSourceCore<UnityEngine.Object> core;
@ -356,7 +378,8 @@ namespace Cysharp.Threading.Tasks
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
result.completed = false;
result.allowReturn = false;
asyncOperation.completed += result.continuationAction;
if (cancelImmediately && cancellationToken.CanBeCanceled)
@ -384,9 +407,16 @@ namespace Cysharp.Threading.Tasks
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
if (!cancellationToken.IsCancellationRequested)
{
TryReturn();
if (allowReturn)
{
TryReturn();
}
else
{
allowReturn = true;
}
}
else
{
@ -417,9 +447,15 @@ namespace Cysharp.Threading.Tasks
public bool MoveNext()
{
// Already completed
if (completed || asyncOperation == null)
if (allowReturn)
{
TryReturn();
return false;
}
if (completed)
{
allowReturn = true;
return false;
}
@ -436,6 +472,8 @@ namespace Cysharp.Threading.Tasks
if (asyncOperation.isDone)
{
cancellationTokenRegistration.Dispose();
allowReturn = true;
core.TrySetResult(asyncOperation.asset);
return false;
}
@ -446,29 +484,31 @@ namespace Cysharp.Threading.Tasks
bool TryReturn()
{
TaskTracker.RemoveTracking(this);
cancellationTokenRegistration.Dispose();
core.Reset();
asyncOperation.completed -= continuationAction;
asyncOperation = default;
progress = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
void Continuation(AsyncOperation _)
{
if (completed)
{
return;
}
completed = true;
cancellationTokenRegistration.Dispose();
if (cancellationToken.IsCancellationRequested)
{
core.TrySetCanceled(cancellationToken);
}
else
{
if (progress != null)
{
progress.Report(asyncOperation.progress);
}
core.TrySetResult(asyncOperation.asset);
}
}
@ -564,6 +604,7 @@ namespace Cysharp.Threading.Tasks
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
bool completed;
bool allowReturn;
UniTaskCompletionSourceCore<UnityEngine.Object> core;
@ -591,7 +632,8 @@ namespace Cysharp.Threading.Tasks
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
result.completed = false;
result.allowReturn = false;
asyncOperation.completed += result.continuationAction;
if (cancelImmediately && cancellationToken.CanBeCanceled)
@ -619,9 +661,16 @@ namespace Cysharp.Threading.Tasks
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
if (!cancellationToken.IsCancellationRequested)
{
TryReturn();
if (allowReturn)
{
TryReturn();
}
else
{
allowReturn = true;
}
}
else
{
@ -652,9 +701,15 @@ namespace Cysharp.Threading.Tasks
public bool MoveNext()
{
// Already completed
if (completed || asyncOperation == null)
if (allowReturn)
{
TryReturn();
return false;
}
if (completed)
{
allowReturn = true;
return false;
}
@ -671,6 +726,8 @@ namespace Cysharp.Threading.Tasks
if (asyncOperation.isDone)
{
cancellationTokenRegistration.Dispose();
allowReturn = true;
core.TrySetResult(asyncOperation.asset);
return false;
}
@ -681,29 +738,31 @@ namespace Cysharp.Threading.Tasks
bool TryReturn()
{
TaskTracker.RemoveTracking(this);
cancellationTokenRegistration.Dispose();
core.Reset();
asyncOperation.completed -= continuationAction;
asyncOperation = default;
progress = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
void Continuation(AsyncOperation _)
{
if (completed)
{
return;
}
completed = true;
cancellationTokenRegistration.Dispose();
if (cancellationToken.IsCancellationRequested)
{
core.TrySetCanceled(cancellationToken);
}
else
{
if (progress != null)
{
progress.Report(asyncOperation.progress);
}
core.TrySetResult(asyncOperation.asset);
}
}
@ -800,6 +859,7 @@ namespace Cysharp.Threading.Tasks
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
bool completed;
bool allowReturn;
UniTaskCompletionSourceCore<AssetBundle> core;
@ -827,7 +887,8 @@ namespace Cysharp.Threading.Tasks
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
result.completed = false;
result.allowReturn = false;
asyncOperation.completed += result.continuationAction;
if (cancelImmediately && cancellationToken.CanBeCanceled)
@ -855,9 +916,16 @@ namespace Cysharp.Threading.Tasks
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
if (!cancellationToken.IsCancellationRequested)
{
TryReturn();
if (allowReturn)
{
TryReturn();
}
else
{
allowReturn = true;
}
}
else
{
@ -888,9 +956,15 @@ namespace Cysharp.Threading.Tasks
public bool MoveNext()
{
// Already completed
if (completed || asyncOperation == null)
if (allowReturn)
{
TryReturn();
return false;
}
if (completed)
{
allowReturn = true;
return false;
}
@ -907,6 +981,8 @@ namespace Cysharp.Threading.Tasks
if (asyncOperation.isDone)
{
cancellationTokenRegistration.Dispose();
allowReturn = true;
core.TrySetResult(asyncOperation.assetBundle);
return false;
}
@ -916,30 +992,32 @@ namespace Cysharp.Threading.Tasks
bool TryReturn()
{
cancellationTokenRegistration.Dispose();
TaskTracker.RemoveTracking(this);
core.Reset();
asyncOperation.completed -= continuationAction;
asyncOperation = default;
progress = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
void Continuation(AsyncOperation _)
{
if (completed)
{
return;
}
completed = true;
cancellationTokenRegistration.Dispose();
if (cancellationToken.IsCancellationRequested)
{
core.TrySetCanceled(cancellationToken);
}
else
{
if (progress != null)
{
progress.Report(asyncOperation.progress);
}
core.TrySetResult(asyncOperation.assetBundle);
}
}
@ -1051,6 +1129,7 @@ namespace Cysharp.Threading.Tasks
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
bool completed;
bool allowReturn;
UniTaskCompletionSourceCore<UnityWebRequest> core;
@ -1078,7 +1157,8 @@ namespace Cysharp.Threading.Tasks
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
result.completed = false;
result.allowReturn = false;
asyncOperation.completed += result.continuationAction;
if (cancelImmediately && cancellationToken.CanBeCanceled)
@ -1107,9 +1187,16 @@ namespace Cysharp.Threading.Tasks
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
if (!cancellationToken.IsCancellationRequested)
{
TryReturn();
if (allowReturn)
{
TryReturn();
}
else
{
allowReturn = true;
}
}
else
{
@ -1140,15 +1227,20 @@ namespace Cysharp.Threading.Tasks
public bool MoveNext()
{
// Already completed
if (completed || asyncOperation == null)
if (allowReturn)
{
TryReturn();
return false;
}
if (completed)
{
allowReturn = true;
return false;
}
if (cancellationToken.IsCancellationRequested)
{
asyncOperation.webRequest.Abort();
core.TrySetCanceled(cancellationToken);
return false;
}
@ -1160,14 +1252,9 @@ namespace Cysharp.Threading.Tasks
if (asyncOperation.isDone)
{
if (asyncOperation.webRequest.IsError())
{
core.TrySetException(new UnityWebRequestException(asyncOperation.webRequest));
}
else
{
core.TrySetResult(asyncOperation.webRequest);
}
cancellationTokenRegistration.Dispose();
allowReturn = true;
core.TrySetResult(asyncOperation.webRequest);
return false;
}
@ -1176,24 +1263,22 @@ namespace Cysharp.Threading.Tasks
bool TryReturn()
{
cancellationTokenRegistration.Dispose();
TaskTracker.RemoveTracking(this);
core.Reset();
asyncOperation.completed -= continuationAction;
asyncOperation = default;
progress = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
void Continuation(AsyncOperation _)
{
if (completed)
{
return;
}
completed = true;
cancellationTokenRegistration.Dispose();
if (cancellationToken.IsCancellationRequested)
{
core.TrySetCanceled(cancellationToken);
@ -1204,6 +1289,11 @@ namespace Cysharp.Threading.Tasks
}
else
{
if (progress != null)
{
progress.Report(asyncOperation.progress);
}
core.TrySetResult(asyncOperation.webRequest);
}
}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b055f3837a55b4a44abf9bf4bcb3594f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1 @@
MyTEST

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 49ccc900467d35543bb602a3de4d786a
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -21,7 +21,6 @@ using System.Linq.Expressions;
using UnityEngine.Events;
// using DG.Tweening;
@ -180,7 +179,7 @@ public class SandboxMain : MonoBehaviour
//UnityAction action;
return 10;
}
@ -592,69 +591,78 @@ public class SandboxMain : MonoBehaviour
async UniTaskVoid Start()
{
await new WhenEachTest().Each();
// 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 () =>
var p = Progress.Create<float>(x =>
{
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();
Debug.Log(x);
});
var foo = await Resources.LoadAsync("test2").ToUniTask(progress: p) as TextAsset;
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)
//{
// Debug.Log("fail");
//}
//else
//{
// var jpg = texture.EncodeToJPG();
// File.WriteAllBytes("testscreencapture.jpg", jpg);
// Debug.Log("ok?");
//}
}));
cancelButton.onClick.AddListener(UniTask.UnityAction(async () =>
{
//clickCancelSource.Cancel();
//await new WhenEachTest().Each();
//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();
}));
//// UniTask.Delay(TimeSpan.FromSeconds(1)).TimeoutWithoutException
await UniTask.Yield();
//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();
// File.WriteAllBytes("testscreencapture.jpg", jpg);
// Debug.Log("ok?");
// //var texture = ScreenCapture.CaptureScreenshotAsTexture();
// //if (texture == null)
// //{
// // Debug.Log("fail");
// //}
// //else
// //{
// // var jpg = texture.EncodeToJPG();
// // File.WriteAllBytes("testscreencapture.jpg", jpg);
// // Debug.Log("ok?");
// //}
//}));
//cancelButton.onClick.AddListener(UniTask.UnityAction(async () =>
//{
// //clickCancelSource.Cancel();
// //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();
//}));
}
async UniTaskVoid RunCheck(PlayerLoopTiming timing)