Merge pull request #557 from Cysharp/hadashiA/fix-cancel-immediate

Fix unintended returning to pool with cancelImmediately
pull/565/head
hadashiA 2024-03-28 15:21:40 +09:00 committed by GitHub
commit b724a2aa84
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 260 additions and 149 deletions

View File

@ -110,6 +110,7 @@ namespace Cysharp.Threading.Tasks
CancellationTokenRegistration cancellationTokenRegistration;
IProgress<float> progress;
bool autoReleaseWhenCanceled;
bool cancelImmediately;
bool completed;
UniTaskCompletionSourceCore<AsyncUnit> core;
@ -134,8 +135,9 @@ namespace Cysharp.Threading.Tasks
result.handle = handle;
result.progress = progress;
result.cancellationToken = cancellationToken;
result.completed = false;
result.cancelImmediately = cancelImmediately;
result.autoReleaseWhenCanceled = autoReleaseWhenCanceled;
result.completed = false;
if (cancelImmediately && cancellationToken.CanBeCanceled)
{
@ -169,10 +171,9 @@ namespace Cysharp.Threading.Tasks
if (completed)
{
TryReturn();
return;
}
else
{
completed = true;
if (cancellationToken.IsCancellationRequested)
{
@ -191,12 +192,21 @@ namespace Cysharp.Threading.Tasks
core.TrySetResult(AsyncUnit.Default);
}
}
}
public void GetResult(short token)
{
try
{
core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
public UniTaskStatus GetStatus(short token)
{
@ -217,7 +227,6 @@ namespace Cysharp.Threading.Tasks
{
if (completed)
{
TryReturn();
return false;
}
@ -304,6 +313,7 @@ namespace Cysharp.Threading.Tasks
CancellationTokenRegistration cancellationTokenRegistration;
IProgress<float> progress;
bool autoReleaseWhenCanceled;
bool cancelImmediately;
bool completed;
UniTaskCompletionSourceCore<T> core;
@ -330,6 +340,7 @@ namespace Cysharp.Threading.Tasks
result.completed = false;
result.progress = progress;
result.autoReleaseWhenCanceled = autoReleaseWhenCanceled;
result.cancelImmediately = cancelImmediately;
if (cancelImmediately && cancellationToken.CanBeCanceled)
{
@ -363,10 +374,8 @@ namespace Cysharp.Threading.Tasks
if (completed)
{
TryReturn();
return;
}
else
{
completed = true;
if (cancellationToken.IsCancellationRequested)
{
@ -385,12 +394,19 @@ namespace Cysharp.Threading.Tasks
core.TrySetResult(argHandle.Result);
}
}
}
public T GetResult(short token)
{
try
{
return core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
TryReturn();
}
}
void IUniTaskSource.GetResult(short token)
{
@ -416,7 +432,6 @@ namespace Cysharp.Threading.Tasks
{
if (completed)
{
TryReturn();
return false;
}

View File

@ -208,6 +208,7 @@ namespace Cysharp.Threading.Tasks
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
UniTaskCompletionSourceCore<object> core;
YieldPromise()
@ -227,6 +228,7 @@ namespace Cysharp.Threading.Tasks
}
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
if (cancelImmediately && cancellationToken.CanBeCanceled)
{
@ -252,10 +254,13 @@ namespace Cysharp.Threading.Tasks
core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
public UniTaskStatus GetStatus(short token)
{
@ -290,6 +295,7 @@ namespace Cysharp.Threading.Tasks
core.Reset();
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
}
@ -309,6 +315,7 @@ namespace Cysharp.Threading.Tasks
UniTaskCompletionSourceCore<AsyncUnit> core;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
NextFramePromise()
{
@ -328,6 +335,7 @@ namespace Cysharp.Threading.Tasks
result.frameCount = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1;
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
if (cancelImmediately && cancellationToken.CanBeCanceled)
{
@ -353,10 +361,13 @@ namespace Cysharp.Threading.Tasks
core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
public UniTaskStatus GetStatus(short token)
{
@ -414,6 +425,7 @@ namespace Cysharp.Threading.Tasks
UniTaskCompletionSourceCore<object> core;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
WaitForEndOfFramePromise()
{
@ -432,6 +444,7 @@ namespace Cysharp.Threading.Tasks
}
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
if (cancelImmediately && cancellationToken.CanBeCanceled)
{
@ -457,10 +470,13 @@ namespace Cysharp.Threading.Tasks
core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
public UniTaskStatus GetStatus(short token)
{
@ -533,6 +549,7 @@ namespace Cysharp.Threading.Tasks
int delayFrameCount;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
int currentFrameCount;
UniTaskCompletionSourceCore<AsyncUnit> core;
@ -556,6 +573,7 @@ namespace Cysharp.Threading.Tasks
result.delayFrameCount = delayFrameCount;
result.cancellationToken = cancellationToken;
result.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1;
result.cancelImmediately = cancelImmediately;
if (cancelImmediately && cancellationToken.CanBeCanceled)
{
@ -581,10 +599,13 @@ namespace Cysharp.Threading.Tasks
core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
public UniTaskStatus GetStatus(short token)
{
@ -653,6 +674,7 @@ namespace Cysharp.Threading.Tasks
delayFrameCount = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
}
@ -673,6 +695,7 @@ namespace Cysharp.Threading.Tasks
float elapsed;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
UniTaskCompletionSourceCore<object> core;
@ -696,6 +719,7 @@ namespace Cysharp.Threading.Tasks
result.delayTimeSpan = (float)delayTimeSpan.TotalSeconds;
result.cancellationToken = cancellationToken;
result.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1;
result.cancelImmediately = cancelImmediately;
if (cancelImmediately && cancellationToken.CanBeCanceled)
{
@ -721,10 +745,13 @@ namespace Cysharp.Threading.Tasks
core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
public UniTaskStatus GetStatus(short token)
{
@ -775,6 +802,7 @@ namespace Cysharp.Threading.Tasks
elapsed = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
}
@ -795,6 +823,7 @@ namespace Cysharp.Threading.Tasks
int initialFrame;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
UniTaskCompletionSourceCore<object> core;
@ -818,6 +847,7 @@ namespace Cysharp.Threading.Tasks
result.delayFrameTimeSpan = (float)delayFrameTimeSpan.TotalSeconds;
result.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1;
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
if (cancelImmediately && cancellationToken.CanBeCanceled)
{
@ -843,10 +873,13 @@ namespace Cysharp.Threading.Tasks
core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
public UniTaskStatus GetStatus(short token)
{
@ -897,6 +930,7 @@ namespace Cysharp.Threading.Tasks
elapsed = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
}
@ -916,6 +950,7 @@ namespace Cysharp.Threading.Tasks
ValueStopwatch stopwatch;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
UniTaskCompletionSourceCore<AsyncUnit> core;
@ -938,6 +973,7 @@ namespace Cysharp.Threading.Tasks
result.stopwatch = ValueStopwatch.StartNew();
result.delayTimeSpanTicks = delayTimeSpan.Ticks;
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
if (cancelImmediately && cancellationToken.CanBeCanceled)
{
@ -963,10 +999,13 @@ namespace Cysharp.Threading.Tasks
core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
public UniTaskStatus GetStatus(short token)
{
@ -1013,6 +1052,7 @@ namespace Cysharp.Threading.Tasks
stopwatch = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
}

View File

@ -49,6 +49,7 @@ namespace Cysharp.Threading.Tasks
Func<bool> predicate;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
UniTaskCompletionSourceCore<object> core;
@ -70,6 +71,7 @@ namespace Cysharp.Threading.Tasks
result.predicate = predicate;
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
if (cancelImmediately && cancellationToken.CanBeCanceled)
{
@ -95,10 +97,13 @@ namespace Cysharp.Threading.Tasks
core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
public UniTaskStatus GetStatus(short token)
{
@ -147,6 +152,7 @@ namespace Cysharp.Threading.Tasks
predicate = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
}
@ -165,6 +171,7 @@ namespace Cysharp.Threading.Tasks
Func<bool> predicate;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
UniTaskCompletionSourceCore<object> core;
@ -211,10 +218,13 @@ namespace Cysharp.Threading.Tasks
core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
public UniTaskStatus GetStatus(short token)
{
@ -263,6 +273,7 @@ namespace Cysharp.Threading.Tasks
predicate = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
}
@ -280,6 +291,7 @@ namespace Cysharp.Threading.Tasks
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
UniTaskCompletionSourceCore<object> core;
@ -287,7 +299,7 @@ namespace Cysharp.Threading.Tasks
{
}
public static IUniTaskSource Create(CancellationToken cancellationToken, PlayerLoopTiming timing, bool completeImmediately, out short token)
public static IUniTaskSource Create(CancellationToken cancellationToken, PlayerLoopTiming timing, bool cancelImmediately, out short token)
{
if (cancellationToken.IsCancellationRequested)
{
@ -300,8 +312,9 @@ namespace Cysharp.Threading.Tasks
}
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
if (completeImmediately && cancellationToken.CanBeCanceled)
if (cancelImmediately && cancellationToken.CanBeCanceled)
{
result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state =>
{
@ -325,10 +338,13 @@ namespace Cysharp.Threading.Tasks
core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
public UniTaskStatus GetStatus(short token)
{
@ -362,6 +378,7 @@ namespace Cysharp.Threading.Tasks
core.Reset();
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
}
@ -385,6 +402,7 @@ namespace Cysharp.Threading.Tasks
IEqualityComparer<U> equalityComparer;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
UniTaskCompletionSourceCore<U> core;
@ -410,6 +428,7 @@ namespace Cysharp.Threading.Tasks
result.currentValue = monitorFunction(target);
result.equalityComparer = equalityComparer ?? UnityEqualityComparer.GetDefault<U>();
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
if (cancelImmediately && cancellationToken.CanBeCanceled)
{
@ -435,10 +454,13 @@ namespace Cysharp.Threading.Tasks
return core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
void IUniTaskSource.GetResult(short token)
{
@ -497,6 +519,7 @@ namespace Cysharp.Threading.Tasks
equalityComparer = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
}
@ -519,6 +542,7 @@ namespace Cysharp.Threading.Tasks
IEqualityComparer<U> equalityComparer;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
UniTaskCompletionSourceCore<U> core;
@ -543,6 +567,7 @@ namespace Cysharp.Threading.Tasks
result.currentValue = monitorFunction(target);
result.equalityComparer = equalityComparer ?? UnityEqualityComparer.GetDefault<U>();
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
if (cancelImmediately && cancellationToken.CanBeCanceled)
{
@ -568,10 +593,13 @@ namespace Cysharp.Threading.Tasks
return core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
void IUniTaskSource.GetResult(short token)
{
@ -630,6 +658,7 @@ namespace Cysharp.Threading.Tasks
equalityComparer = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
}

View File

@ -101,6 +101,7 @@ namespace Cysharp.Threading.Tasks
IProgress<float> progress;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
bool completed;
UniTaskCompletionSourceCore<UnityEngine.Object[]> core;
@ -127,6 +128,7 @@ namespace Cysharp.Threading.Tasks
result.asyncOperation = asyncOperation;
result.progress = progress;
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
result.completed = false;
asyncOperation.completed += result.continuationAction;
@ -155,10 +157,13 @@ namespace Cysharp.Threading.Tasks
return core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
void IUniTaskSource.GetResult(short token)
{
@ -216,6 +221,7 @@ namespace Cysharp.Threading.Tasks
progress = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
@ -223,10 +229,9 @@ namespace Cysharp.Threading.Tasks
{
if (completed)
{
TryReturn();
return;
}
else
{
completed = true;
if (cancellationToken.IsCancellationRequested)
{
@ -239,7 +244,6 @@ namespace Cysharp.Threading.Tasks
}
}
}
}
}
#endif

View File

@ -45,7 +45,7 @@ namespace Cysharp.Threading.Tasks
AsyncGPUReadbackRequest asyncOperation;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
UniTaskCompletionSourceCore<AsyncGPUReadbackRequest> core;
AsyncGPUReadbackRequestAwaiterConfiguredSource()
@ -66,6 +66,7 @@ namespace Cysharp.Threading.Tasks
result.asyncOperation = asyncOperation;
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
if (cancelImmediately && cancellationToken.CanBeCanceled)
{
@ -91,10 +92,13 @@ namespace Cysharp.Threading.Tasks
return core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
void IUniTaskSource.GetResult(short token)
{
@ -146,6 +150,7 @@ namespace Cysharp.Threading.Tasks
asyncOperation = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
}

View File

@ -97,6 +97,7 @@ namespace Cysharp.Threading.Tasks
IProgress<float> progress;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
bool completed;
UniTaskCompletionSourceCore<AsyncUnit> core;
@ -123,6 +124,7 @@ namespace Cysharp.Threading.Tasks
result.asyncOperation = asyncOperation;
result.progress = progress;
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
result.completed = false;
asyncOperation.completed += result.continuationAction;
@ -151,10 +153,13 @@ namespace Cysharp.Threading.Tasks
core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
public UniTaskStatus GetStatus(short token)
@ -209,6 +214,7 @@ namespace Cysharp.Threading.Tasks
progress = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
@ -216,10 +222,8 @@ namespace Cysharp.Threading.Tasks
{
if (completed)
{
TryReturn();
return;
}
else
{
completed = true;
if (cancellationToken.IsCancellationRequested)
{
@ -231,7 +235,6 @@ namespace Cysharp.Threading.Tasks
}
}
}
}
#endregion
@ -320,6 +323,7 @@ namespace Cysharp.Threading.Tasks
IProgress<float> progress;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
bool completed;
UniTaskCompletionSourceCore<UnityEngine.Object> core;
@ -346,6 +350,7 @@ namespace Cysharp.Threading.Tasks
result.asyncOperation = asyncOperation;
result.progress = progress;
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
result.completed = false;
asyncOperation.completed += result.continuationAction;
@ -374,10 +379,13 @@ namespace Cysharp.Threading.Tasks
return core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
void IUniTaskSource.GetResult(short token)
{
@ -436,6 +444,7 @@ namespace Cysharp.Threading.Tasks
progress = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
@ -443,10 +452,8 @@ namespace Cysharp.Threading.Tasks
{
if (completed)
{
TryReturn();
return;
}
else
{
completed = true;
if (cancellationToken.IsCancellationRequested)
{
@ -458,7 +465,6 @@ namespace Cysharp.Threading.Tasks
}
}
}
}
#endregion
@ -548,6 +554,7 @@ namespace Cysharp.Threading.Tasks
IProgress<float> progress;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
bool completed;
UniTaskCompletionSourceCore<UnityEngine.Object> core;
@ -574,6 +581,7 @@ namespace Cysharp.Threading.Tasks
result.asyncOperation = asyncOperation;
result.progress = progress;
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
result.completed = false;
asyncOperation.completed += result.continuationAction;
@ -602,10 +610,13 @@ namespace Cysharp.Threading.Tasks
return core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
void IUniTaskSource.GetResult(short token)
{
@ -664,6 +675,7 @@ namespace Cysharp.Threading.Tasks
progress = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
@ -671,10 +683,8 @@ namespace Cysharp.Threading.Tasks
{
if (completed)
{
TryReturn();
return;
}
else
{
completed = true;
if (cancellationToken.IsCancellationRequested)
{
@ -686,7 +696,6 @@ namespace Cysharp.Threading.Tasks
}
}
}
}
#endregion
#endif
@ -777,6 +786,7 @@ namespace Cysharp.Threading.Tasks
IProgress<float> progress;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
bool completed;
UniTaskCompletionSourceCore<AssetBundle> core;
@ -803,6 +813,7 @@ namespace Cysharp.Threading.Tasks
result.asyncOperation = asyncOperation;
result.progress = progress;
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
result.completed = false;
asyncOperation.completed += result.continuationAction;
@ -831,10 +842,13 @@ namespace Cysharp.Threading.Tasks
return core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
void IUniTaskSource.GetResult(short token)
{
@ -893,6 +907,7 @@ namespace Cysharp.Threading.Tasks
progress = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
@ -900,10 +915,8 @@ namespace Cysharp.Threading.Tasks
{
if (completed)
{
TryReturn();
return;
}
else
{
completed = true;
if (cancellationToken.IsCancellationRequested)
{
@ -915,7 +928,6 @@ namespace Cysharp.Threading.Tasks
}
}
}
}
#endregion
#endif
@ -1021,6 +1033,7 @@ namespace Cysharp.Threading.Tasks
IProgress<float> progress;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
bool completed;
UniTaskCompletionSourceCore<UnityWebRequest> core;
@ -1047,6 +1060,7 @@ namespace Cysharp.Threading.Tasks
result.asyncOperation = asyncOperation;
result.progress = progress;
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
result.completed = false;
asyncOperation.completed += result.continuationAction;
@ -1076,10 +1090,13 @@ namespace Cysharp.Threading.Tasks
return core.GetResult(token);
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
void IUniTaskSource.GetResult(short token)
{
@ -1146,6 +1163,7 @@ namespace Cysharp.Threading.Tasks
progress = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
@ -1153,10 +1171,8 @@ namespace Cysharp.Threading.Tasks
{
if (completed)
{
TryReturn();
return;
}
else
{
completed = true;
if (cancellationToken.IsCancellationRequested)
{
@ -1172,7 +1188,6 @@ namespace Cysharp.Threading.Tasks
}
}
}
}
#endregion
#endif

View File

@ -165,6 +165,7 @@ namespace Cysharp.Threading.Tasks
IProgress<float> progress;
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
bool cancelImmediately;
bool completed;
UniTaskCompletionSourceCore<<#= IsVoid(t) ? "AsyncUnit" : t.returnType #>> core;
@ -191,6 +192,7 @@ namespace Cysharp.Threading.Tasks
result.asyncOperation = asyncOperation;
result.progress = progress;
result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
result.completed = false;
asyncOperation.completed += result.continuationAction;
@ -226,10 +228,13 @@ namespace Cysharp.Threading.Tasks
<# } #>
}
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
<# if (!IsVoid(t)) { #>
void IUniTaskSource.GetResult(short token)
@ -304,6 +309,7 @@ namespace Cysharp.Threading.Tasks
progress = default;
cancellationToken = default;
cancellationTokenRegistration.Dispose();
cancelImmediately = default;
return pool.TryPush(this);
}
@ -311,10 +317,8 @@ namespace Cysharp.Threading.Tasks
{
if (completed)
{
TryReturn();
return;
}
else
{
completed = true;
if (cancellationToken.IsCancellationRequested)
{
@ -337,7 +341,6 @@ namespace Cysharp.Threading.Tasks
<# } #>
}
}
}
#endregion
<# if(IsUnityWebRequest(t) || IsAssetBundleModule(t)) { #>