Compare commits

...

26 Commits
2.5.1 ... 2.5.4

Author SHA1 Message Date
github-actions[bot]
d057074f17 feat: Update package.json to 2.5.4 2024-03-28 06:22:15 +00:00
hadashiA
b724a2aa84 Merge pull request #557 from Cysharp/hadashiA/fix-cancel-immediate
Fix unintended returning to pool with cancelImmediately
2024-03-28 15:21:40 +09:00
hadashiA
938ddd5356 Tweaks 2024-03-28 15:15:55 +09:00
hadashiA
911c37d4d8 Tweaks 2024-03-28 15:14:10 +09:00
hadashiA
caccccb0b5 Fix timing to returning to pool 2024-03-28 15:00:41 +09:00
hadashiA
a48f11d31b Fix unintended returning to pool with cancelImmediately (WaitUntil,etc) 2024-03-28 14:46:37 +09:00
hadashiA
fcd93feb56 Fix unintended returning to pool with cancelImmediately (AsyncOperation) 2024-03-28 14:45:55 +09:00
hadashiA
b472b23773 Fix unintended returning to pool with cancelImmediately (UniTask.Delay) 2024-03-28 14:29:30 +09:00
Yoshifumi Kawai
01c8fada1f Merge pull request #542 from Cysharp/feature/new_release
ci: Cysharp/Actions/.github/workflows/create-release.yaml
2024-03-25 18:30:50 +09:00
Ikiru Yoshizaki
10cd137126 ci: Cysharp/Actions/.github/workflows/create-release.yaml 2024-02-07 21:04:48 +09:00
Yoshifumi Kawai
809d23edae Merge pull request #541 from TORISOUP/fix_ToArray
Optimization of ToArray in Merge method
2024-02-04 23:19:47 +09:00
TORISOUP
d38731bc44 fix call ToArray 2024-02-04 20:51:21 +09:00
github-actions[bot]
64792b672d feat: Update package.json to 2.5.3 2024-01-25 12:52:16 +00:00
neuecc
3892cc2299 Merge branch 'master' of https://github.com/Cysharp/UniTask 2024-01-25 21:51:45 +09:00
neuecc
5bfccaa3b6 autoReleaseWhenCancelled -> autoReleaseWhenCanceled 2024-01-25 21:51:40 +09:00
github-actions[bot]
b49b7332bb feat: Update package.json to 2.5.2 2024-01-25 11:43:24 +00:00
neuecc
08184af737 simplify UNITASK define 2024-01-25 20:42:42 +09:00
Yoshifumi Kawai
0ef6c59385 Merge pull request #535 from kochounoyume/valuetask-error-netframework
UniTask does not work in .NET Framework environment after adding UniTask.AsValueTask
2024-01-25 20:26:21 +09:00
Yoshifumi Kawai
4b0bd3b509 Merge pull request #536 from Cysharp/hadashiA/release-handle
Fix "Release handle when cancellation is requested"
2024-01-25 20:26:13 +09:00
Kochoyume
9a4720d180 Fix:Addressed generic parameter attributes error in the .Net Framework 2024-01-25 20:05:45 +09:00
hadashiA
b99646558c Add a flag autoReleaseWhenCancelled to addressable extensionhs 2024-01-25 19:03:00 +09:00
hadashiA
ee12dd9ae7 Fix omissions in Addressable.Release and IsValid checks 2024-01-25 17:47:06 +09:00
Kochoyume
006e0f2c81 Revert "Fix conditional compilation to work with .NET Framework"
This reverts commit 7d31299b5c.
2024-01-25 16:16:01 +09:00
Kochoyume
7d31299b5c Fix conditional compilation to work with .NET Framework 2024-01-25 01:02:57 +09:00
Yoshifumi Kawai
fe8bf834e6 Update README.md 2024-01-24 10:47:31 +09:00
Luciano Prestes Cavalcanti
36ac0863ad Release handle when cancellation is requested 2023-11-22 08:09:03 -03:00
18 changed files with 397 additions and 264 deletions

View File

@@ -12,17 +12,13 @@ on:
default: false default: false
type: boolean type: boolean
env:
GIT_TAG: ${{ github.event.inputs.tag }}
DRY_RUN: ${{ github.event.inputs.dry-run }}
jobs: jobs:
update-packagejson: update-packagejson:
uses: Cysharp/Actions/.github/workflows/update-packagejson.yaml@main uses: Cysharp/Actions/.github/workflows/update-packagejson.yaml@main
with: with:
file-path: ./src/UniTask/Assets/Plugins/UniTask/package.json file-path: ./src/UniTask/Assets/Plugins/UniTask/package.json
tag: ${{ github.event.inputs.tag }} tag: ${{ inputs.tag }}
dry-run: ${{ fromJson(github.event.inputs.dry-run) }} dry-run: ${{ inputs.dry-run }}
build-dotnet: build-dotnet:
needs: [update-packagejson] needs: [update-packagejson]
@@ -35,9 +31,9 @@ jobs:
ref: ${{ needs.update-packagejson.outputs.sha }} ref: ${{ needs.update-packagejson.outputs.sha }}
- uses: Cysharp/Actions/.github/actions/setup-dotnet@main - uses: Cysharp/Actions/.github/actions/setup-dotnet@main
# build and pack # build and pack
- run: dotnet build -c Release -p:Version=${{ env.GIT_TAG }} - run: dotnet build -c Release -p:Version=${{ inputs.tag }}
- run: dotnet test -c Release --no-build - run: dotnet test -c Release --no-build
- run: dotnet pack ./src/UniTask.NetCore/UniTask.NetCore.csproj -c Release --no-build -p:Version=${{ env.GIT_TAG }} -o ./publish - run: dotnet pack ./src/UniTask.NetCore/UniTask.NetCore.csproj -c Release --no-build -p:Version=${{ inputs.tag }} -o ./publish
# Store artifacts. # Store artifacts.
- uses: actions/upload-artifact@v2 - uses: actions/upload-artifact@v2
with: with:
@@ -77,43 +73,24 @@ jobs:
# Store artifacts. # Store artifacts.
- uses: actions/upload-artifact@v2 - uses: actions/upload-artifact@v2
with: with:
name: UniTask.${{ env.GIT_TAG }}.unitypackage name: UniTask.${{ inputs.tag }}.unitypackage
path: ./src/UniTask/UniTask.${{ env.GIT_TAG }}.unitypackage path: ./src/UniTask/UniTask.${{ inputs.tag }}.unitypackage
# release
create-release: create-release:
if: github.event.inputs.dry-run == 'false'
needs: [update-packagejson, build-dotnet, build-unity] needs: [update-packagejson, build-dotnet, build-unity]
runs-on: ubuntu-latest uses: Cysharp/Actions/.github/workflows/create-release.yaml@main
timeout-minutes: 10
steps:
- uses: Cysharp/Actions/.github/actions/setup-dotnet@main
# Create Releases
- uses: actions/create-release@v1
id: create_release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with: with:
tag_name: ${{ env.GIT_TAG }} commit-id: ${{ needs.update-packagejson.outputs.sha }}
release_name: Ver.${{ env.GIT_TAG }} dry-run: ${{ inputs.dry-run }}
commitish: ${{ needs.update-packagejson.outputs.sha }} tag: ${{ inputs.tag }}
draft: true nuget-push: true
prerelease: false release-upload: true
# Download(All) Artifacts to current directory release-asset-path: ./UniTask.${{ inputs.tag }}.unitypackage/UniTask.${{ inputs.tag }}.unitypackage
- uses: actions/download-artifact@v2 secrets: inherit
# Upload to NuGet
- run: dotnet nuget push "./nuget/*.nupkg" --skip-duplicate -s https://www.nuget.org/api/v2/package -k ${{ secrets.NUGET_KEY }}
# Upload to Releases(unitypackage)
- uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./UniTask.${{ env.GIT_TAG }}.unitypackage/UniTask.${{ env.GIT_TAG }}.unitypackage
asset_name: UniTask.${{ env.GIT_TAG }}.unitypackage
asset_content_type: application/octet-stream
cleanup: cleanup:
if: needs.update-packagejson.outputs.is-branch-created == 'true' if: ${{ needs.update-packagejson.outputs.is-branch-created == 'true' }}
needs: [update-packagejson, build-dotnet, build-unity] needs: [update-packagejson, build-dotnet, build-unity]
uses: Cysharp/Actions/.github/workflows/clean-packagejson-branch.yaml@main uses: Cysharp/Actions/.github/workflows/clean-packagejson-branch.yaml@main
with: with:

View File

@@ -51,7 +51,7 @@ For advanced tips, see blog post: [Extends UnityWebRequest via async decorator p
Getting started Getting started
--- ---
Install via [UPM package](#upm-package) or asset package(`UniTask.*.*.*.unitypackage`) available in [UniTask/releases](https://github.com/Cysharp/UniTask/releases) page. Install via [UPM package](#upm-package) with git reference or asset package(`UniTask.*.*.*.unitypackage`) available in [UniTask/releases](https://github.com/Cysharp/UniTask/releases).
```csharp ```csharp
// extension awaiter/methods can be used by this namespace // extension awaiter/methods can be used by this namespace

View File

@@ -5,6 +5,7 @@
<AssemblyName>UniTask</AssemblyName> <AssemblyName>UniTask</AssemblyName>
<LangVersion>8.0</LangVersion> <LangVersion>8.0</LangVersion>
<RootNamespace>Cysharp.Threading.Tasks</RootNamespace> <RootNamespace>Cysharp.Threading.Tasks</RootNamespace>
<DefineConstants>UNITASK_NETCORE</DefineConstants>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);CS1591</NoWarn> <NoWarn>$(NoWarn);CS1591</NoWarn>

View File

@@ -1,3 +1,5 @@
#pragma warning disable CS1998
using System; using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;

View File

@@ -1,3 +1,5 @@
#pragma warning disable CS1998
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Cysharp.Threading.Tasks; using Cysharp.Threading.Tasks;

View File

@@ -7,6 +7,7 @@ using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices; using System.Runtime.ExceptionServices;
using System.Threading; using System.Threading;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations; using UnityEngine.ResourceManagement.AsyncOperations;
namespace Cysharp.Threading.Tasks namespace Cysharp.Threading.Tasks
@@ -20,17 +21,12 @@ namespace Cysharp.Threading.Tasks
return ToUniTask(handle).GetAwaiter(); return ToUniTask(handle).GetAwaiter();
} }
public static UniTask WithCancellation(this AsyncOperationHandle handle, CancellationToken cancellationToken) public static UniTask WithCancellation(this AsyncOperationHandle handle, CancellationToken cancellationToken, bool cancelImmediately = false, bool autoReleaseWhenCanceled = false)
{ {
return ToUniTask(handle, cancellationToken: cancellationToken); return ToUniTask(handle, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately, autoReleaseWhenCanceled: autoReleaseWhenCanceled);
} }
public static UniTask WithCancellation(this AsyncOperationHandle handle, CancellationToken cancellationToken, bool cancelImmediately) public static UniTask ToUniTask(this AsyncOperationHandle handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false, bool autoReleaseWhenCanceled = false)
{
return ToUniTask(handle, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately);
}
public static UniTask ToUniTask(this AsyncOperationHandle handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false)
{ {
if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken);
@@ -49,7 +45,7 @@ namespace Cysharp.Threading.Tasks
return UniTask.CompletedTask; return UniTask.CompletedTask;
} }
return new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, timing, progress, cancellationToken, cancelImmediately, out var token), token); return new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, timing, progress, cancellationToken, cancelImmediately, autoReleaseWhenCanceled, out var token), token);
} }
public struct AsyncOperationHandleAwaiter : ICriticalNotifyCompletion public struct AsyncOperationHandleAwaiter : ICriticalNotifyCompletion
@@ -108,21 +104,23 @@ namespace Cysharp.Threading.Tasks
TaskPool.RegisterSizeGetter(typeof(AsyncOperationHandleConfiguredSource), () => pool.Size); TaskPool.RegisterSizeGetter(typeof(AsyncOperationHandleConfiguredSource), () => pool.Size);
} }
readonly Action<AsyncOperationHandle> continuationAction; readonly Action<AsyncOperationHandle> completedCallback;
AsyncOperationHandle handle; AsyncOperationHandle handle;
CancellationToken cancellationToken; CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration; CancellationTokenRegistration cancellationTokenRegistration;
IProgress<float> progress; IProgress<float> progress;
bool autoReleaseWhenCanceled;
bool cancelImmediately;
bool completed; bool completed;
UniTaskCompletionSourceCore<AsyncUnit> core; UniTaskCompletionSourceCore<AsyncUnit> core;
AsyncOperationHandleConfiguredSource() AsyncOperationHandleConfiguredSource()
{ {
continuationAction = Continuation; completedCallback = HandleCompleted;
} }
public static IUniTaskSource Create(AsyncOperationHandle handle, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) public static IUniTaskSource Create(AsyncOperationHandle handle, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, bool cancelImmediately, bool autoReleaseWhenCanceled, out short token)
{ {
if (cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{ {
@@ -137,6 +135,8 @@ namespace Cysharp.Threading.Tasks
result.handle = handle; result.handle = handle;
result.progress = progress; result.progress = progress;
result.cancellationToken = cancellationToken; result.cancellationToken = cancellationToken;
result.cancelImmediately = cancelImmediately;
result.autoReleaseWhenCanceled = autoReleaseWhenCanceled;
result.completed = false; result.completed = false;
if (cancelImmediately && cancellationToken.CanBeCanceled) if (cancelImmediately && cancellationToken.CanBeCanceled)
@@ -144,6 +144,10 @@ namespace Cysharp.Threading.Tasks
result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state =>
{ {
var promise = (AsyncOperationHandleConfiguredSource)state; var promise = (AsyncOperationHandleConfiguredSource)state;
if (promise.autoReleaseWhenCanceled && promise.handle.IsValid())
{
Addressables.Release(promise.handle);
}
promise.core.TrySetCanceled(promise.cancellationToken); promise.core.TrySetCanceled(promise.cancellationToken);
}, result); }, result);
} }
@@ -152,25 +156,31 @@ namespace Cysharp.Threading.Tasks
PlayerLoopHelper.AddAction(timing, result); PlayerLoopHelper.AddAction(timing, result);
handle.Completed += result.continuationAction; handle.Completed += result.completedCallback;
token = result.core.Version; token = result.core.Version;
return result; return result;
} }
void Continuation(AsyncOperationHandle _) void HandleCompleted(AsyncOperationHandle _)
{ {
handle.Completed -= continuationAction; if (handle.IsValid())
{
handle.Completed -= completedCallback;
}
if (completed) if (completed)
{ {
TryReturn(); return;
} }
else
{
completed = true; completed = true;
if (cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{ {
if (autoReleaseWhenCanceled && handle.IsValid())
{
Addressables.Release(handle);
}
core.TrySetCanceled(cancellationToken); core.TrySetCanceled(cancellationToken);
} }
else if (handle.Status == AsyncOperationStatus.Failed) else if (handle.Status == AsyncOperationStatus.Failed)
@@ -182,12 +192,21 @@ namespace Cysharp.Threading.Tasks
core.TrySetResult(AsyncUnit.Default); core.TrySetResult(AsyncUnit.Default);
} }
} }
}
public void GetResult(short token) public void GetResult(short token)
{
try
{ {
core.GetResult(token); core.GetResult(token);
} }
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
{
TryReturn();
}
}
}
public UniTaskStatus GetStatus(short token) public UniTaskStatus GetStatus(short token)
{ {
@@ -208,13 +227,16 @@ namespace Cysharp.Threading.Tasks
{ {
if (completed) if (completed)
{ {
TryReturn();
return false; return false;
} }
if (cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{ {
completed = true; completed = true;
if (autoReleaseWhenCanceled && handle.IsValid())
{
Addressables.Release(handle);
}
core.TrySetCanceled(cancellationToken); core.TrySetCanceled(cancellationToken);
return false; return false;
} }
@@ -248,17 +270,12 @@ namespace Cysharp.Threading.Tasks
return ToUniTask(handle).GetAwaiter(); return ToUniTask(handle).GetAwaiter();
} }
public static UniTask<T> WithCancellation<T>(this AsyncOperationHandle<T> handle, CancellationToken cancellationToken) public static UniTask<T> WithCancellation<T>(this AsyncOperationHandle<T> handle, CancellationToken cancellationToken, bool cancelImmediately = false, bool autoReleaseWhenCanceled = false)
{ {
return ToUniTask(handle, cancellationToken: cancellationToken); return ToUniTask(handle, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately, autoReleaseWhenCanceled: autoReleaseWhenCanceled);
} }
public static UniTask<T> WithCancellation<T>(this AsyncOperationHandle<T> handle, CancellationToken cancellationToken, bool cancelImmediately) public static UniTask<T> ToUniTask<T>(this AsyncOperationHandle<T> handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false, bool autoReleaseWhenCanceled = false)
{
return ToUniTask(handle, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately);
}
public static UniTask<T> ToUniTask<T>(this AsyncOperationHandle<T> handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false)
{ {
if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<T>(cancellationToken); if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<T>(cancellationToken);
@@ -276,7 +293,7 @@ namespace Cysharp.Threading.Tasks
return UniTask.FromResult(handle.Result); return UniTask.FromResult(handle.Result);
} }
return new UniTask<T>(AsyncOperationHandleConfiguredSource<T>.Create(handle, timing, progress, cancellationToken, cancelImmediately, out var token), token); return new UniTask<T>(AsyncOperationHandleConfiguredSource<T>.Create(handle, timing, progress, cancellationToken, cancelImmediately, autoReleaseWhenCanceled, out var token), token);
} }
sealed class AsyncOperationHandleConfiguredSource<T> : IUniTaskSource<T>, IPlayerLoopItem, ITaskPoolNode<AsyncOperationHandleConfiguredSource<T>> sealed class AsyncOperationHandleConfiguredSource<T> : IUniTaskSource<T>, IPlayerLoopItem, ITaskPoolNode<AsyncOperationHandleConfiguredSource<T>>
@@ -290,21 +307,23 @@ namespace Cysharp.Threading.Tasks
TaskPool.RegisterSizeGetter(typeof(AsyncOperationHandleConfiguredSource<T>), () => pool.Size); TaskPool.RegisterSizeGetter(typeof(AsyncOperationHandleConfiguredSource<T>), () => pool.Size);
} }
readonly Action<AsyncOperationHandle<T>> continuationAction; readonly Action<AsyncOperationHandle<T>> completedCallback;
AsyncOperationHandle<T> handle; AsyncOperationHandle<T> handle;
CancellationToken cancellationToken; CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration; CancellationTokenRegistration cancellationTokenRegistration;
IProgress<float> progress; IProgress<float> progress;
bool autoReleaseWhenCanceled;
bool cancelImmediately;
bool completed; bool completed;
UniTaskCompletionSourceCore<T> core; UniTaskCompletionSourceCore<T> core;
AsyncOperationHandleConfiguredSource() AsyncOperationHandleConfiguredSource()
{ {
continuationAction = Continuation; completedCallback = HandleCompleted;
} }
public static IUniTaskSource<T> Create(AsyncOperationHandle<T> handle, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, bool cancelImmediately, out short token) public static IUniTaskSource<T> Create(AsyncOperationHandle<T> handle, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, bool cancelImmediately, bool autoReleaseWhenCanceled, out short token)
{ {
if (cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{ {
@@ -320,12 +339,18 @@ namespace Cysharp.Threading.Tasks
result.cancellationToken = cancellationToken; result.cancellationToken = cancellationToken;
result.completed = false; result.completed = false;
result.progress = progress; result.progress = progress;
result.autoReleaseWhenCanceled = autoReleaseWhenCanceled;
result.cancelImmediately = cancelImmediately;
if (cancelImmediately && cancellationToken.CanBeCanceled) if (cancelImmediately && cancellationToken.CanBeCanceled)
{ {
result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state =>
{ {
var promise = (AsyncOperationHandleConfiguredSource<T>)state; var promise = (AsyncOperationHandleConfiguredSource<T>)state;
if (promise.autoReleaseWhenCanceled && promise.handle.IsValid())
{
Addressables.Release(promise.handle);
}
promise.core.TrySetCanceled(promise.cancellationToken); promise.core.TrySetCanceled(promise.cancellationToken);
}, result); }, result);
} }
@@ -334,25 +359,30 @@ namespace Cysharp.Threading.Tasks
PlayerLoopHelper.AddAction(timing, result); PlayerLoopHelper.AddAction(timing, result);
handle.Completed += result.continuationAction; handle.Completed += result.completedCallback;
token = result.core.Version; token = result.core.Version;
return result; return result;
} }
void Continuation(AsyncOperationHandle<T> argHandle) void HandleCompleted(AsyncOperationHandle<T> argHandle)
{ {
handle.Completed -= continuationAction; if (handle.IsValid())
{
handle.Completed -= completedCallback;
}
if (completed) if (completed)
{ {
TryReturn(); return;
} }
else
{
completed = true; completed = true;
if (cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{ {
if (autoReleaseWhenCanceled && handle.IsValid())
{
Addressables.Release(handle);
}
core.TrySetCanceled(cancellationToken); core.TrySetCanceled(cancellationToken);
} }
else if (argHandle.Status == AsyncOperationStatus.Failed) else if (argHandle.Status == AsyncOperationStatus.Failed)
@@ -364,12 +394,19 @@ namespace Cysharp.Threading.Tasks
core.TrySetResult(argHandle.Result); core.TrySetResult(argHandle.Result);
} }
} }
}
public T GetResult(short token) public T GetResult(short token)
{
try
{ {
return core.GetResult(token); return core.GetResult(token);
} }
finally
{
if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
TryReturn();
}
}
void IUniTaskSource.GetResult(short token) void IUniTaskSource.GetResult(short token)
{ {
@@ -395,13 +432,16 @@ namespace Cysharp.Threading.Tasks
{ {
if (completed) if (completed)
{ {
TryReturn();
return false; return false;
} }
if (cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{ {
completed = true; completed = true;
if (autoReleaseWhenCanceled && handle.IsValid())
{
Addressables.Release(handle);
}
core.TrySetCanceled(cancellationToken); core.TrySetCanceled(cancellationToken);
return false; return false;
} }

View File

@@ -2,7 +2,8 @@
"name": "UniTask.Addressables", "name": "UniTask.Addressables",
"references": [ "references": [
"UniTask", "UniTask",
"Unity.ResourceManager" "Unity.ResourceManager",
"Unity.Addressables"
], ],
"includePlatforms": [], "includePlatforms": [],
"excludePlatforms": [], "excludePlatforms": [],

View File

@@ -1,4 +1,9 @@
#pragma warning disable CS1591 #pragma warning disable CS1591
#pragma warning disable CS0108
#if (UNITASK_NETCORE && !NETSTANDARD2_0) || UNITY_2022_3_OR_NEWER
#define SUPPORT_VALUETASK
#endif
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@@ -19,9 +24,8 @@ namespace Cysharp.Threading.Tasks
// similar as IValueTaskSource // similar as IValueTaskSource
public interface IUniTaskSource public interface IUniTaskSource
#if (!UNITY_2018_3_OR_NEWER || UNITY_2021_2_OR_NEWER) && !NETSTANDARD2_0 #if SUPPORT_VALUETASK
: System.Threading.Tasks.Sources.IValueTaskSource : System.Threading.Tasks.Sources.IValueTaskSource
#pragma warning disable CS0108
#endif #endif
{ {
UniTaskStatus GetStatus(short token); UniTaskStatus GetStatus(short token);
@@ -30,8 +34,7 @@ namespace Cysharp.Threading.Tasks
UniTaskStatus UnsafeGetStatus(); // only for debug use. UniTaskStatus UnsafeGetStatus(); // only for debug use.
#if (!UNITY_2018_3_OR_NEWER || UNITY_2021_2_OR_NEWER) && !NETSTANDARD2_0 #if SUPPORT_VALUETASK
#pragma warning restore CS0108
System.Threading.Tasks.Sources.ValueTaskSourceStatus System.Threading.Tasks.Sources.IValueTaskSource.GetStatus(short token) System.Threading.Tasks.Sources.ValueTaskSourceStatus System.Threading.Tasks.Sources.IValueTaskSource.GetStatus(short token)
{ {
@@ -53,13 +56,13 @@ namespace Cysharp.Threading.Tasks
} }
public interface IUniTaskSource<out T> : IUniTaskSource public interface IUniTaskSource<out T> : IUniTaskSource
#if (!UNITY_2018_3_OR_NEWER || UNITY_2021_2_OR_NEWER) && !NETSTANDARD2_0 #if SUPPORT_VALUETASK
, System.Threading.Tasks.Sources.IValueTaskSource<T> , System.Threading.Tasks.Sources.IValueTaskSource<T>
#endif #endif
{ {
new T GetResult(short token); new T GetResult(short token);
#if (!UNITY_2018_3_OR_NEWER || UNITY_2021_2_OR_NEWER) && !NETSTANDARD2_0 #if SUPPORT_VALUETASK
new public UniTaskStatus GetStatus(short token) new public UniTaskStatus GetStatus(short token)
{ {

View File

@@ -27,7 +27,9 @@ namespace Cysharp.Threading.Tasks.Linq
public static IUniTaskAsyncEnumerable<T> Merge<T>(this IEnumerable<IUniTaskAsyncEnumerable<T>> sources) public static IUniTaskAsyncEnumerable<T> Merge<T>(this IEnumerable<IUniTaskAsyncEnumerable<T>> sources)
{ {
return new Merge<T>(sources.ToArray()); return sources is IUniTaskAsyncEnumerable<T>[] array
? new Merge<T>(array)
: new Merge<T>(sources.ToArray());
} }
public static IUniTaskAsyncEnumerable<T> Merge<T>(params IUniTaskAsyncEnumerable<T>[] sources) public static IUniTaskAsyncEnumerable<T> Merge<T>(params IUniTaskAsyncEnumerable<T>[] sources)

View File

@@ -1,5 +1,10 @@
#if !UNITY_2018_3_OR_NEWER || UNITY_2021_2_OR_NEWER #pragma warning disable 0649
#pragma warning disable 0649
#if UNITASK_NETCORE || UNITY_2022_3_OR_NEWER
#define SUPPORT_VALUETASK
#endif
#if SUPPORT_VALUETASK
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -11,7 +16,7 @@ namespace Cysharp.Threading.Tasks
{ {
public static ValueTask AsValueTask(this in UniTask task) public static ValueTask AsValueTask(this in UniTask task)
{ {
#if NETSTANDARD2_0 #if (UNITASK_NETCORE && NETSTANDARD2_0)
return new ValueTask(new UniTaskValueTaskSource(task), 0); return new ValueTask(new UniTaskValueTaskSource(task), 0);
#else #else
return task; return task;
@@ -20,7 +25,7 @@ namespace Cysharp.Threading.Tasks
public static ValueTask<T> AsValueTask<T>(this in UniTask<T> task) public static ValueTask<T> AsValueTask<T>(this in UniTask<T> task)
{ {
#if NETSTANDARD2_0 #if (UNITASK_NETCORE && NETSTANDARD2_0)
return new ValueTask<T>(new UniTaskValueTaskSource<T>(task), 0); return new ValueTask<T>(new UniTaskValueTaskSource<T>(task), 0);
#else #else
return task; return task;
@@ -37,7 +42,7 @@ namespace Cysharp.Threading.Tasks
await task; await task;
} }
#if NETSTANDARD2_0 #if (UNITASK_NETCORE && NETSTANDARD2_0)
class UniTaskValueTaskSource : IValueTaskSource class UniTaskValueTaskSource : IValueTaskSource
{ {

View File

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

View File

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

View File

@@ -1,6 +1,10 @@
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
#pragma warning disable CS0436 #pragma warning disable CS0436
#if UNITASK_NETCORE || UNITY_2022_3_OR_NEWER
#define SUPPORT_VALUETASK
#endif
using Cysharp.Threading.Tasks.CompilerServices; using Cysharp.Threading.Tasks.CompilerServices;
using System; using System;
using System.Diagnostics; using System.Diagnostics;
@@ -69,7 +73,7 @@ namespace Cysharp.Threading.Tasks
return new UniTask<bool>(new IsCanceledSource(source), token); return new UniTask<bool>(new IsCanceledSource(source), token);
} }
#if !UNITY_2018_3_OR_NEWER || UNITY_2021_2_OR_NEWER #if SUPPORT_VALUETASK
public static implicit operator System.Threading.Tasks.ValueTask(in UniTask self) public static implicit operator System.Threading.Tasks.ValueTask(in UniTask self)
{ {
@@ -78,7 +82,7 @@ namespace Cysharp.Threading.Tasks
return default; return default;
} }
#if NETSTANDARD2_0 #if (UNITASK_NETCORE && NETSTANDARD2_0)
return self.AsValueTask(); return self.AsValueTask();
#else #else
return new System.Threading.Tasks.ValueTask(self.source, self.token); return new System.Threading.Tasks.ValueTask(self.source, self.token);
@@ -118,7 +122,7 @@ namespace Cysharp.Threading.Tasks
this.source.GetResult(this.token); this.source.GetResult(this.token);
return CompletedTasks.AsyncUnit; return CompletedTasks.AsyncUnit;
} }
else if(this.source is IUniTaskSource<AsyncUnit> asyncUnitSource) else if (this.source is IUniTaskSource<AsyncUnit> asyncUnitSource)
{ {
return new UniTask<AsyncUnit>(asyncUnitSource, this.token); return new UniTask<AsyncUnit>(asyncUnitSource, this.token);
} }
@@ -440,7 +444,7 @@ namespace Cysharp.Threading.Tasks
return self.AsUniTask(); return self.AsUniTask();
} }
#if !UNITY_2018_3_OR_NEWER || UNITY_2021_2_OR_NEWER #if SUPPORT_VALUETASK
public static implicit operator System.Threading.Tasks.ValueTask<T>(in UniTask<T> self) public static implicit operator System.Threading.Tasks.ValueTask<T>(in UniTask<T> self)
{ {
@@ -449,7 +453,7 @@ namespace Cysharp.Threading.Tasks
return new System.Threading.Tasks.ValueTask<T>(self.result); return new System.Threading.Tasks.ValueTask<T>(self.result);
} }
#if NETSTANDARD2_0 #if (UNITASK_NETCORE && NETSTANDARD2_0)
return self.AsValueTask(); return self.AsValueTask();
#else #else
return new System.Threading.Tasks.ValueTask<T>(self.source, self.token); return new System.Threading.Tasks.ValueTask<T>(self.source, self.token);

View File

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

View File

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

View File

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

View File

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

View File

@@ -2,7 +2,7 @@
"name": "com.cysharp.unitask", "name": "com.cysharp.unitask",
"displayName": "UniTask", "displayName": "UniTask",
"author": { "name": "Cysharp, Inc.", "url": "https://cysharp.co.jp/en/" }, "author": { "name": "Cysharp, Inc.", "url": "https://cysharp.co.jp/en/" },
"version": "2.5.1", "version": "2.5.4",
"unity": "2018.4", "unity": "2018.4",
"description": "Provides an efficient async/await integration to Unity.", "description": "Provides an efficient async/await integration to Unity.",
"keywords": [ "async/await", "async", "Task", "UniTask" ], "keywords": [ "async/await", "async", "Task", "UniTask" ],