| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Threading; |
| | 4 | | using Cysharp.Threading.Tasks; |
| | 5 | |
|
| | 6 | | namespace DCL.Helpers |
| | 7 | | { |
| | 8 | | public static class TaskUtils |
| | 9 | | { |
| 892 | 10 | | private static BaseVariable<bool> multithreading => DataStore.i.performance.multithreading; |
| | 11 | | public static async UniTask Run(Action action, CancellationToken cancellationToken = default, bool returnToMainT |
| | 12 | | { |
| 886 | 13 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 14 | |
|
| 886 | 15 | | if (multithreading.Get()) |
| | 16 | | { |
| 0 | 17 | | await UniTask.RunOnThreadPool(action, returnToMainThread, cancellationToken); |
| 0 | 18 | | } |
| | 19 | | else |
| | 20 | | { |
| 2658 | 21 | | await UniTask.Yield(cancellationToken); |
| 884 | 22 | | action(); |
| | 23 | | } |
| 884 | 24 | | } |
| | 25 | |
|
| | 26 | | public static async UniTask Run(Func<UniTask> action, CancellationToken cancellationToken = default, bool return |
| | 27 | | { |
| 0 | 28 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 29 | |
|
| 0 | 30 | | if (multithreading.Get()) |
| | 31 | | { |
| 0 | 32 | | await UniTask.RunOnThreadPool(action, returnToMainThread, cancellationToken); |
| 0 | 33 | | } |
| | 34 | | else |
| | 35 | | { |
| 0 | 36 | | await UniTask.Create(action).AttachExternalCancellation(cancellationToken); |
| | 37 | | } |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | public static async UniTask<T> Run<T>(Func<UniTask<T>> action, CancellationToken cancellationToken = default, bo |
| | 41 | | { |
| | 42 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 43 | |
|
| | 44 | | if (multithreading.Get()) |
| | 45 | | { |
| | 46 | | return await UniTask.RunOnThreadPool(action, returnToMainThread, cancellationToken); |
| | 47 | | } |
| | 48 | |
|
| | 49 | | return await UniTask.Create(action).AttachExternalCancellation(cancellationToken); |
| | 50 | | } |
| | 51 | |
|
| | 52 | | public static async UniTask<T> Run<T>(Func<T> action, CancellationToken cancellationToken = default, bool return |
| | 53 | | { |
| | 54 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 55 | |
|
| | 56 | | if (multithreading.Get()) |
| | 57 | | { |
| | 58 | | return await UniTask.RunOnThreadPool(action, returnToMainThread, cancellationToken); |
| | 59 | | } |
| | 60 | |
|
| | 61 | | await UniTask.Yield(cancellationToken); |
| | 62 | |
|
| | 63 | | return action(); |
| | 64 | | } |
| | 65 | |
|
| | 66 | | public static async UniTask RunThrottledCoroutine(IEnumerator enumerator, Action<Exception> onFail, Func<double, |
| | 67 | | { |
| 108 | 68 | | IEnumerator routine = DCLCoroutineRunner.Run(enumerator, onFail, timeBudget); |
| 323 | 69 | | await routine.ToUniTask(CoroutineStarter.instance); |
| 107 | 70 | | } |
| | 71 | |
|
| | 72 | | } |
| | 73 | | } |