| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using System.Runtime.CompilerServices; |
| | 4 | | using System.Threading; |
| | 5 | |
|
| | 6 | | [assembly: InternalsVisibleTo("TaskUtilsTests")] |
| | 7 | |
|
| | 8 | | namespace DCL.Helpers |
| | 9 | | { |
| | 10 | | public unsafe class UniTaskUtils |
| | 11 | | { |
| | 12 | | public static UniTask WaitForBoolean(ref bool reference, bool targetValue = true, PlayerLoopTiming timing = Play |
| 3 | 13 | | { |
| | 14 | | // fixes the reference in memory within the block only |
| 3 | 15 | | fixed (bool* pointer = &reference) |
| | 16 | | { |
| | 17 | | // But Unity GC is non-compacting so it does not move references |
| | 18 | | // Thus, it is safe to pass it and save as a class field |
| 3 | 19 | | return new UniTask(WaitForBooleanPromise.Create(pointer, targetValue, timing, cancellationToken, out var |
| | 20 | | } |
| | 21 | | } |
| | 22 | |
|
| | 23 | | internal sealed class WaitForBooleanPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<WaitForBooleanPromi |
| | 24 | | { |
| | 25 | | internal static TaskPool<WaitForBooleanPromise> pool; |
| | 26 | | private WaitForBooleanPromise nextNode; |
| | 27 | |
|
| 5 | 28 | | ref WaitForBooleanPromise ITaskPoolNode<WaitForBooleanPromise>.NextNode => ref nextNode; |
| | 29 | |
|
| | 30 | | private CancellationToken cancellationToken; |
| | 31 | |
|
| | 32 | | private bool* pointer; |
| | 33 | | private bool targetValue; |
| | 34 | |
|
| | 35 | | private UniTaskCompletionSourceCore<object> core; |
| | 36 | |
|
| | 37 | | static WaitForBooleanPromise() |
| | 38 | | { |
| 1 | 39 | | TaskPool.RegisterSizeGetter(typeof(WaitForBooleanPromise), () => pool.Size); |
| 1 | 40 | | } |
| | 41 | |
|
| 2 | 42 | | private WaitForBooleanPromise() { } |
| | 43 | |
|
| | 44 | | public static IUniTaskSource Create(bool* pointer, bool targetValue, PlayerLoopTiming timing, CancellationTo |
| | 45 | | { |
| 3 | 46 | | if (cancellationToken.IsCancellationRequested) |
| 0 | 47 | | return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token); |
| | 48 | |
|
| 3 | 49 | | if (!pool.TryPop(out var result)) |
| 1 | 50 | | result = new WaitForBooleanPromise(); |
| | 51 | |
|
| 3 | 52 | | result.pointer = pointer; |
| 3 | 53 | | result.targetValue = targetValue; |
| 3 | 54 | | result.cancellationToken = cancellationToken; |
| | 55 | |
|
| 3 | 56 | | TaskTracker.TrackActiveTask(result, 3); |
| | 57 | |
|
| 3 | 58 | | PlayerLoopHelper.AddAction(timing, result); |
| | 59 | |
|
| 3 | 60 | | token = result.core.Version; |
| 3 | 61 | | return result; |
| | 62 | | } |
| | 63 | |
|
| | 64 | | public void GetResult(short token) |
| | 65 | | { |
| 6 | 66 | | try { core.GetResult(token); } |
| 6 | 67 | | finally { TryReturn(); } |
| 3 | 68 | | } |
| | 69 | |
|
| | 70 | | public UniTaskStatus GetStatus(short token) |
| | 71 | | { |
| 3 | 72 | | return core.GetStatus(token); |
| | 73 | | } |
| | 74 | |
|
| | 75 | | public UniTaskStatus UnsafeGetStatus() |
| | 76 | | { |
| 0 | 77 | | return core.UnsafeGetStatus(); |
| | 78 | | } |
| | 79 | |
|
| | 80 | | public void OnCompleted(Action<object> continuation, object state, short token) |
| | 81 | | { |
| 3 | 82 | | core.OnCompleted(continuation, state, token); |
| 3 | 83 | | } |
| | 84 | |
|
| | 85 | | public bool MoveNext() |
| | 86 | | { |
| 59 | 87 | | if (cancellationToken.IsCancellationRequested) |
| | 88 | | { |
| 0 | 89 | | core.TrySetCanceled(cancellationToken); |
| 0 | 90 | | return false; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | try |
| | 94 | | { |
| 59 | 95 | | if (*pointer != targetValue) |
| 56 | 96 | | return true; |
| 3 | 97 | | } |
| 0 | 98 | | catch (Exception ex) |
| | 99 | | { |
| 0 | 100 | | core.TrySetException(ex); |
| 0 | 101 | | return false; |
| | 102 | | } |
| | 103 | |
|
| 3 | 104 | | core.TrySetResult(null); |
| 3 | 105 | | return false; |
| 56 | 106 | | } |
| | 107 | |
|
| | 108 | | bool TryReturn() |
| | 109 | | { |
| 3 | 110 | | TaskTracker.RemoveTracking(this); |
| 3 | 111 | | core.Reset(); |
| 3 | 112 | | pointer = default; |
| 3 | 113 | | cancellationToken = default; |
| 3 | 114 | | return pool.TryPush(this); |
| | 115 | | } |
| | 116 | | } |
| | 117 | | } |
| | 118 | | } |