fix WhenAll,WhenAny 0-length

pull/73/head
neuecc 2020-05-07 11:27:27 +09:00
parent 66fa203f7c
commit be539fdb10
4 changed files with 59 additions and 7 deletions

View File

@ -16,8 +16,16 @@ namespace NetCoreSandbox
static async UniTask<int> outer()
{
var v = await DoAsync();
return v;
//await Task.WhenAll();
//var foo = await Task.WhenAny(Array.Empty<Task<int>>());
await UniTask.WhenAny(new UniTask[0]);
return 10;
//var v = await DoAsync();
//return v;
}

View File

@ -32,11 +32,21 @@ namespace Cysharp.Threading.Tasks.Internal
}
}
public static RentArray<T> CopyToRentArray<T>(IEnumerable<T> source)
public static RentArray<T> Materialize<T>(IEnumerable<T> source)
{
if (source is T[] array)
{
return new RentArray<T>(array, array.Length, null);
}
var defaultCount = 32;
if (source is ICollection<T> coll)
{
if (coll.Count == 0)
{
return new RentArray<T>(Array.Empty<T>(), 0, null);
}
defaultCount = coll.Count;
var pool = ArrayPool<T>.Shared;
var buffer = pool.Rent(defaultCount);

View File

@ -11,12 +11,17 @@ namespace Cysharp.Threading.Tasks
{
public static UniTask<T[]> WhenAll<T>(params UniTask<T>[] tasks)
{
if (tasks.Length == 0)
{
return UniTask.FromResult(Array.Empty<T>());
}
return new UniTask<T[]>(new WhenAllPromise<T>(tasks, tasks.Length), 0);
}
public static UniTask<T[]> WhenAll<T>(IEnumerable<UniTask<T>> tasks)
{
using (var span = ArrayPoolUtil.CopyToRentArray(tasks))
using (var span = ArrayPoolUtil.Materialize(tasks))
{
var promise = new WhenAllPromise<T>(span.Array, span.Length); // consumed array in constructor.
return new UniTask<T[]>(promise, 0);
@ -25,12 +30,17 @@ namespace Cysharp.Threading.Tasks
public static UniTask WhenAll(params UniTask[] tasks)
{
if (tasks.Length == 0)
{
return UniTask.CompletedTask;
}
return new UniTask(new WhenAllPromise(tasks, tasks.Length), 0);
}
public static UniTask WhenAll(IEnumerable<UniTask> tasks)
{
using (var span = ArrayPoolUtil.CopyToRentArray(tasks))
using (var span = ArrayPoolUtil.Materialize(tasks))
{
var promise = new WhenAllPromise(span.Array, span.Length); // consumed array in constructor.
return new UniTask(promise, 0);
@ -48,6 +58,14 @@ namespace Cysharp.Threading.Tasks
TaskTracker.TrackActiveTask(this, 3);
this.completeCount = 0;
if (tasksLength == 0)
{
this.result = Array.Empty<T>();
core.TrySetResult(result);
return;
}
this.result = new T[tasksLength];
for (int i = 0; i < tasksLength; i++)
@ -144,6 +162,12 @@ namespace Cysharp.Threading.Tasks
this.tasksLength = tasksLength;
this.completeCount = 0;
if (tasksLength == 0)
{
core.TrySetResult(AsyncUnit.Default);
return;
}
for (int i = 0; i < tasksLength; i++)
{
UniTask.Awaiter awaiter;

View File

@ -21,7 +21,7 @@ namespace Cysharp.Threading.Tasks
public static UniTask<(int winArgumentIndex, T result)> WhenAny<T>(IEnumerable<UniTask<T>> tasks)
{
using (var span = ArrayPoolUtil.CopyToRentArray(tasks))
using (var span = ArrayPoolUtil.Materialize(tasks))
{
return new UniTask<(int, T)>(new WhenAnyPromise<T>(span.Array, span.Length), 0);
}
@ -36,7 +36,7 @@ namespace Cysharp.Threading.Tasks
/// <summary>Return value is winArgumentIndex</summary>
public static UniTask<int> WhenAny(IEnumerable<UniTask> tasks)
{
using (var span = ArrayPoolUtil.CopyToRentArray(tasks))
using (var span = ArrayPoolUtil.Materialize(tasks))
{
return new UniTask<int>(new WhenAnyPromise(span.Array, span.Length), 0);
}
@ -186,6 +186,11 @@ namespace Cysharp.Threading.Tasks
public WhenAnyPromise(UniTask<T>[] tasks, int tasksLength)
{
if (tasksLength == 0)
{
throw new ArgumentException("The tasks argument contains no tasks.");
}
TaskTracker.TrackActiveTask(this, 3);
for (int i = 0; i < tasksLength; i++)
@ -277,6 +282,11 @@ namespace Cysharp.Threading.Tasks
public WhenAnyPromise(UniTask[] tasks, int tasksLength)
{
if (tasksLength == 0)
{
throw new ArgumentException("The tasks argument contains no tasks.");
}
TaskTracker.TrackActiveTask(this, 3);
for (int i = 0; i < tasksLength; i++)