2019-05-19 23:14:47 +08:00
|
|
|
|
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
|
|
|
|
|
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using UniRx.Async.Internal;
|
|
|
|
|
|
|
|
|
|
namespace UniRx.Async
|
|
|
|
|
{
|
|
|
|
|
public partial struct UniTask
|
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
public static UniTask<T[]> WhenAll<T>(params UniTask<T>[] tasks)
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
return new UniTask<T[]>(new WhenAllPromise<T>(tasks, tasks.Length), 0);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
public static UniTask<T[]> WhenAll<T>(IEnumerable<UniTask<T>> tasks)
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-05 03:08:53 +08:00
|
|
|
|
using (var span = ArrayPoolUtil.CopyToRentArray(tasks))
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
var promise = new WhenAllPromise<T>(span.Array, span.Length); // consumed array in constructor.
|
|
|
|
|
return new UniTask<T[]>(promise, 0);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
public static UniTask WhenAll(params UniTask[] tasks)
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
return new UniTask(new WhenAllPromise(tasks, tasks.Length), 0);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
public static UniTask WhenAll(IEnumerable<UniTask> tasks)
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-05 03:08:53 +08:00
|
|
|
|
using (var span = ArrayPoolUtil.CopyToRentArray(tasks))
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
var promise = new WhenAllPromise(span.Array, span.Length); // consumed array in constructor.
|
|
|
|
|
return new UniTask(promise, 0);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
sealed class WhenAllPromise<T> : IUniTaskSource<T[]>
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
T[] result;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
int completeCount;
|
2020-04-21 12:36:23 +08:00
|
|
|
|
UniTaskCompletionSourceCore<T[]> core; // don't reset(called after GetResult, will invoke TrySetException.)
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
|
|
|
|
public WhenAllPromise(UniTask<T>[] tasks, int tasksLength)
|
|
|
|
|
{
|
2020-05-05 03:08:53 +08:00
|
|
|
|
TaskTracker.TrackActiveTask(this, 3);
|
2020-04-21 12:36:23 +08:00
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
this.completeCount = 0;
|
|
|
|
|
this.result = new T[tasksLength];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < tasksLength; i++)
|
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
UniTask<T>.Awaiter awaiter;
|
|
|
|
|
try
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
awaiter = tasks[i].GetAwaiter();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
core.TrySetException(ex);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
if (awaiter.IsCompleted)
|
|
|
|
|
{
|
|
|
|
|
TryInvokeContinuation(this, awaiter, i);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
awaiter.SourceOnCompleted(state =>
|
|
|
|
|
{
|
|
|
|
|
using (var t = (StateTuple<WhenAllPromise<T>, UniTask<T>.Awaiter, int>)state)
|
|
|
|
|
{
|
|
|
|
|
TryInvokeContinuation(t.Item1, t.Item2, t.Item3);
|
|
|
|
|
}
|
|
|
|
|
}, StateTuple.Create(this, awaiter, i));
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
static void TryInvokeContinuation(WhenAllPromise<T> self, in UniTask<T>.Awaiter awaiter, int i)
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
self.result[i] = awaiter.GetResult();
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
self.core.TrySetException(ex);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
if (Interlocked.Increment(ref self.completeCount) == self.result.Length)
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
self.core.TrySetResult(self.result);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
public T[] GetResult(short token)
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-05 03:08:53 +08:00
|
|
|
|
TaskTracker.RemoveTracking(this);
|
2020-04-21 12:36:23 +08:00
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
return core.GetResult(token);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
void IUniTaskSource.GetResult(short token)
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
GetResult(token);
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
public UniTaskStatus GetStatus(short token)
|
|
|
|
|
{
|
|
|
|
|
return core.GetStatus(token);
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
public UniTaskStatus UnsafeGetStatus()
|
|
|
|
|
{
|
|
|
|
|
return core.UnsafeGetStatus();
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
|
|
|
|
{
|
|
|
|
|
core.OnCompleted(continuation, state, token);
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
~WhenAllPromise()
|
|
|
|
|
{
|
|
|
|
|
core.Reset();
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
sealed class WhenAllPromise : IUniTaskSource
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
|
|
|
|
int completeCount;
|
2020-04-21 12:36:23 +08:00
|
|
|
|
int tasksLength;
|
|
|
|
|
UniTaskCompletionSourceCore<AsyncUnit> core; // don't reset(called after GetResult, will invoke TrySetException.)
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
|
|
|
|
public WhenAllPromise(UniTask[] tasks, int tasksLength)
|
|
|
|
|
{
|
2020-05-05 03:08:53 +08:00
|
|
|
|
TaskTracker.TrackActiveTask(this, 3);
|
2020-04-21 12:36:23 +08:00
|
|
|
|
|
|
|
|
|
this.tasksLength = tasksLength;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
this.completeCount = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < tasksLength; i++)
|
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
UniTask.Awaiter awaiter;
|
|
|
|
|
try
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
awaiter = tasks[i].GetAwaiter();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
core.TrySetException(ex);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
if (awaiter.IsCompleted)
|
|
|
|
|
{
|
|
|
|
|
TryInvokeContinuation(this, awaiter);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
awaiter.SourceOnCompleted(state =>
|
|
|
|
|
{
|
|
|
|
|
using (var t = (StateTuple<WhenAllPromise, UniTask.Awaiter>)state)
|
|
|
|
|
{
|
|
|
|
|
TryInvokeContinuation(t.Item1, t.Item2);
|
|
|
|
|
}
|
|
|
|
|
}, StateTuple.Create(this, awaiter));
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
static void TryInvokeContinuation(WhenAllPromise self, in UniTask.Awaiter awaiter)
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
awaiter.GetResult();
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
self.core.TrySetException(ex);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
if (Interlocked.Increment(ref self.completeCount) == self.tasksLength)
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
self.core.TrySetResult(AsyncUnit.Default);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
public void GetResult(short token)
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-05-05 03:08:53 +08:00
|
|
|
|
TaskTracker.RemoveTracking(this);
|
2020-04-21 12:36:23 +08:00
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
core.GetResult(token);
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
public UniTaskStatus GetStatus(short token)
|
2019-05-19 23:14:47 +08:00
|
|
|
|
{
|
2020-04-21 12:36:23 +08:00
|
|
|
|
return core.GetStatus(token);
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
public UniTaskStatus UnsafeGetStatus()
|
|
|
|
|
{
|
|
|
|
|
return core.UnsafeGetStatus();
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
|
|
|
|
{
|
|
|
|
|
core.OnCompleted(continuation, state, token);
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
2020-04-21 12:36:23 +08:00
|
|
|
|
~WhenAllPromise()
|
|
|
|
|
{
|
|
|
|
|
core.Reset();
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|