Add CancellationToken cancellationToken = default to UniTask.Run #113

master
neuecc 2020-07-14 11:25:30 +09:00
parent 3724fc204c
commit 9d3b7adc8e
1 changed files with 63 additions and 10 deletions

View File

@ -1,16 +1,21 @@
#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
using System; using System;
using System.Threading;
namespace Cysharp.Threading.Tasks namespace Cysharp.Threading.Tasks
{ {
public partial struct UniTask public partial struct UniTask
{ {
/// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary> /// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
public static async UniTask Run(Action action, bool configureAwait = true) public static async UniTask Run(Action action, bool configureAwait = true, CancellationToken cancellationToken = default)
{ {
cancellationToken.ThrowIfCancellationRequested();
await UniTask.SwitchToThreadPool(); await UniTask.SwitchToThreadPool();
cancellationToken.ThrowIfCancellationRequested();
if (configureAwait) if (configureAwait)
{ {
try try
@ -26,13 +31,19 @@ namespace Cysharp.Threading.Tasks
{ {
action(); action();
} }
cancellationToken.ThrowIfCancellationRequested();
} }
/// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary> /// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
public static async UniTask Run(Action<object> action, object state, bool configureAwait = true) public static async UniTask Run(Action<object> action, object state, bool configureAwait = true, CancellationToken cancellationToken = default)
{ {
cancellationToken.ThrowIfCancellationRequested();
await UniTask.SwitchToThreadPool(); await UniTask.SwitchToThreadPool();
cancellationToken.ThrowIfCancellationRequested();
if (configureAwait) if (configureAwait)
{ {
try try
@ -48,13 +59,19 @@ namespace Cysharp.Threading.Tasks
{ {
action(state); action(state);
} }
cancellationToken.ThrowIfCancellationRequested();
} }
/// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary> /// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
public static async UniTask Run(Func<UniTask> action, bool configureAwait = true) public static async UniTask Run(Func<UniTask> action, bool configureAwait = true, CancellationToken cancellationToken = default)
{ {
cancellationToken.ThrowIfCancellationRequested();
await UniTask.SwitchToThreadPool(); await UniTask.SwitchToThreadPool();
cancellationToken.ThrowIfCancellationRequested();
if (configureAwait) if (configureAwait)
{ {
try try
@ -70,13 +87,19 @@ namespace Cysharp.Threading.Tasks
{ {
await action(); await action();
} }
cancellationToken.ThrowIfCancellationRequested();
} }
/// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary> /// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
public static async UniTask Run(Func<object, UniTask> action, object state, bool configureAwait = true) public static async UniTask Run(Func<object, UniTask> action, object state, bool configureAwait = true, CancellationToken cancellationToken = default)
{ {
cancellationToken.ThrowIfCancellationRequested();
await UniTask.SwitchToThreadPool(); await UniTask.SwitchToThreadPool();
cancellationToken.ThrowIfCancellationRequested();
if (configureAwait) if (configureAwait)
{ {
try try
@ -92,12 +115,19 @@ namespace Cysharp.Threading.Tasks
{ {
await action(state); await action(state);
} }
cancellationToken.ThrowIfCancellationRequested();
} }
/// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary> /// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
public static async UniTask<T> Run<T>(Func<T> func, bool configureAwait = true) public static async UniTask<T> Run<T>(Func<T> func, bool configureAwait = true, CancellationToken cancellationToken = default)
{ {
cancellationToken.ThrowIfCancellationRequested();
await UniTask.SwitchToThreadPool(); await UniTask.SwitchToThreadPool();
cancellationToken.ThrowIfCancellationRequested();
if (configureAwait) if (configureAwait)
{ {
try try
@ -107,6 +137,7 @@ namespace Cysharp.Threading.Tasks
finally finally
{ {
await UniTask.Yield(); await UniTask.Yield();
cancellationToken.ThrowIfCancellationRequested();
} }
} }
else else
@ -116,9 +147,14 @@ namespace Cysharp.Threading.Tasks
} }
/// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary> /// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
public static async UniTask<T> Run<T>(Func<UniTask<T>> func, bool configureAwait = true) public static async UniTask<T> Run<T>(Func<UniTask<T>> func, bool configureAwait = true, CancellationToken cancellationToken = default)
{ {
cancellationToken.ThrowIfCancellationRequested();
await UniTask.SwitchToThreadPool(); await UniTask.SwitchToThreadPool();
cancellationToken.ThrowIfCancellationRequested();
if (configureAwait) if (configureAwait)
{ {
try try
@ -127,20 +163,28 @@ namespace Cysharp.Threading.Tasks
} }
finally finally
{ {
cancellationToken.ThrowIfCancellationRequested();
await UniTask.Yield(); await UniTask.Yield();
cancellationToken.ThrowIfCancellationRequested();
} }
} }
else else
{ {
return await func(); var result = await func();
cancellationToken.ThrowIfCancellationRequested();
return result;
} }
} }
/// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary> /// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
public static async UniTask<T> Run<T>(Func<object, T> func, object state, bool configureAwait = true) public static async UniTask<T> Run<T>(Func<object, T> func, object state, bool configureAwait = true, CancellationToken cancellationToken = default)
{ {
cancellationToken.ThrowIfCancellationRequested();
await UniTask.SwitchToThreadPool(); await UniTask.SwitchToThreadPool();
cancellationToken.ThrowIfCancellationRequested();
if (configureAwait) if (configureAwait)
{ {
try try
@ -150,6 +194,7 @@ namespace Cysharp.Threading.Tasks
finally finally
{ {
await UniTask.Yield(); await UniTask.Yield();
cancellationToken.ThrowIfCancellationRequested();
} }
} }
else else
@ -159,10 +204,14 @@ namespace Cysharp.Threading.Tasks
} }
/// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary> /// <summary>Run action on the threadPool and return to main thread if configureAwait = true.</summary>
public static async UniTask<T> Run<T>(Func<object, UniTask<T>> func, object state, bool configureAwait = true) public static async UniTask<T> Run<T>(Func<object, UniTask<T>> func, object state, bool configureAwait = true, CancellationToken cancellationToken = default)
{ {
cancellationToken.ThrowIfCancellationRequested();
await UniTask.SwitchToThreadPool(); await UniTask.SwitchToThreadPool();
cancellationToken.ThrowIfCancellationRequested();
if (configureAwait) if (configureAwait)
{ {
try try
@ -171,12 +220,16 @@ namespace Cysharp.Threading.Tasks
} }
finally finally
{ {
cancellationToken.ThrowIfCancellationRequested();
await UniTask.Yield(); await UniTask.Yield();
cancellationToken.ThrowIfCancellationRequested();
} }
} }
else else
{ {
return await func(state); var result = await func(state);
cancellationToken.ThrowIfCancellationRequested();
return result;
} }
} }
} }