123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- using System;
- using System.Collections.Generic;
- namespace ET
- {
- public static class ETTaskHelper
- {
- public static bool IsCancel(this ETCancellationToken self)
- {
- if (self == null)
- {
- return false;
- }
- return self.IsDispose();
- }
-
- private class CoroutineBlocker
- {
- private int count;
- private List<ETTask> tcss = new List<ETTask>();
- public CoroutineBlocker(int count)
- {
- this.count = count;
- }
- public async ETTask WaitAsync()
- {
- --this.count;
- if (this.count < 0)
- {
- return;
- }
- if (this.count == 0)
- {
- List<ETTask> t = this.tcss;
- this.tcss = null;
- foreach (ETTask ttcs in t)
- {
- ttcs.SetResult();
- }
- return;
- }
- ETTask tcs = ETTask.Create(true);
- tcss.Add(tcs);
- await tcs;
- }
- }
- public static async ETTask<bool> WaitAny<T>(ETTask<T>[] tasks, ETCancellationToken cancellationToken = null)
- {
- if (tasks.Length == 0)
- {
- return false;
- }
- CoroutineBlocker coroutineBlocker = new CoroutineBlocker(2);
- foreach (ETTask<T> task in tasks)
- {
- RunOneTask(task).Coroutine();
- }
- async ETVoid RunOneTask(ETTask<T> task)
- {
- await task;
- await coroutineBlocker.WaitAsync();
- }
- await coroutineBlocker.WaitAsync();
- if (cancellationToken == null)
- {
- return true;
- }
- return !cancellationToken.IsCancel();
- }
- public static async ETTask<bool> WaitAny(ETTask[] tasks, ETCancellationToken cancellationToken = null)
- {
- if (tasks.Length == 0)
- {
- return false;
- }
- CoroutineBlocker coroutineBlocker = new CoroutineBlocker(2);
- foreach (ETTask task in tasks)
- {
- RunOneTask(task).Coroutine();
- }
- async ETVoid RunOneTask(ETTask task)
- {
- await task;
- await coroutineBlocker.WaitAsync();
- }
- await coroutineBlocker.WaitAsync();
- if (cancellationToken == null)
- {
- return true;
- }
- return !cancellationToken.IsCancel();
- }
- public static async ETTask<bool> WaitAll<T>(ETTask<T>[] tasks, ETCancellationToken cancellationToken = null)
- {
- if (tasks.Length == 0)
- {
- return false;
- }
- CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Length + 1);
- foreach (ETTask<T> task in tasks)
- {
- RunOneTask(task).Coroutine();
- }
- async ETVoid RunOneTask(ETTask<T> task)
- {
- await task;
- await coroutineBlocker.WaitAsync();
- }
- await coroutineBlocker.WaitAsync();
- if (cancellationToken == null)
- {
- return true;
- }
- return !cancellationToken.IsCancel();
- }
- public static async ETTask<bool> WaitAll<T>(List<ETTask<T>> tasks, ETCancellationToken cancellationToken = null)
- {
- if (tasks.Count == 0)
- {
- return false;
- }
- CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Count + 1);
- foreach (ETTask<T> task in tasks)
- {
- RunOneTask(task).Coroutine();
- }
- async ETVoid RunOneTask(ETTask<T> task)
- {
- await task;
- await coroutineBlocker.WaitAsync();
- }
- await coroutineBlocker.WaitAsync();
- if (cancellationToken == null)
- {
- return true;
- }
- return !cancellationToken.IsCancel();
- }
- public static async ETTask<bool> WaitAll(ETTask[] tasks, ETCancellationToken cancellationToken = null)
- {
- if (tasks.Length == 0)
- {
- return false;
- }
- CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Length + 1);
- foreach (ETTask task in tasks)
- {
- RunOneTask(task).Coroutine();
- }
- await coroutineBlocker.WaitAsync();
- async ETVoid RunOneTask(ETTask task)
- {
- await task;
- await coroutineBlocker.WaitAsync();
- }
- if (cancellationToken == null)
- {
- return true;
- }
- return !cancellationToken.IsCancel();
- }
- public static async ETTask<bool> WaitAll(List<ETTask> tasks, ETCancellationToken cancellationToken = null)
- {
- if (tasks.Count == 0)
- {
- return false;
- }
- CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Count + 1);
- foreach (ETTask task in tasks)
- {
- RunOneTask(task).Coroutine();
- }
- await coroutineBlocker.WaitAsync();
- async ETVoid RunOneTask(ETTask task)
- {
- await task;
- await coroutineBlocker.WaitAsync();
- }
- return !cancellationToken.IsCancel();
- }
- }
- }
|