namespace System.Threading.Tasks;
/// Work that finishes later — a delay, an `async` method, work on the pool
/// — awaited without stopping what runs. What awaits it goes on in the
/// synchronization context it awaited in (a side's thread, a UI's), unless
/// `ConfigureAwait(false)` says anywhere will do. Its panic is its
/// awaiter's. Safe across threads. Game time (ticks) is `Yog.GameTime`'s.
[Intrinsic]
public readonly class Task {
/// Done: it finished or it failed.
public bool IsCompleted { get; }
public bool IsCompletedSuccessfully { get; }
/// It failed: awaiting it panics as it did.
public bool IsFaulted { get; }
/// A task already done.
public static Task CompletedTask { get; }
/// The factory of tasks on the pool.
public static TaskFactory Factory { get; }
/// Done once `delay` has passed, in real time (`Task.Delay(5s)`).
public static Task Delay(Seconds delay);
public static Task Delay(TimeSpan delay);
/// Done at the next turn of its side (in the game, its next tick).
public static Task Yield();
/// `work` run on the pool (`TaskScheduler.Default`): the way off a
/// side's thread for heavy work, awaited back in the caller's context.
[Crosses(nameof(work), Crossing.Moved)]
public static Task<TResult> Run<TResult>(Func<TResult> work);
[Crosses(nameof(work), Crossing.Moved)]
public static Task Run(Action work);
/// A task already done with `result`.
public static Task<TResult> FromResult<TResult>(TResult result);
/// Done when every one of `tasks` is; failed as the first that fails.
public static Task WhenAll(params Task[] tasks);
/// Done when one of `tasks` is: that one.
public static Task<Task> WhenAny(params Task[] tasks);
/// Done when every one of `tasks` is: their results, in order; failed
/// as the first that fails.
[Reified]
public static Task<TResult[]> WhenAll<TResult>(params Task<TResult>[] tasks);
/// Done when one of `tasks` is: that one.
public static Task<Task<TResult>> WhenAny<TResult>(params Task<TResult>[] tasks);
/// Waits for every one of `tasks` (blocking; see `Wait`).
public static void WaitAll(params Task[] tasks);
/// Waits for one of `tasks` (blocking): its index.
public static i32 WaitAny(params Task[] tasks);
/// `continuation` run with it once it's done; done after it.
public Task ContinueWith(Action<Task> continuation);
/// As it is (`true`), or awaited without the awaiting context (`false`):
/// what awaits it goes on wherever it's finished.
public Task ConfigureAwait(bool continueOnCapturedContext);
/// Waits for it, blocking; its failure panics here. On a side's thread
/// what's posted to it meanwhile runs, so the thread doesn't wait for
/// itself; awaiting is still the way not to block.
public void Wait();
/// As `Wait()`, at most `timeout`: whether it's done.
public bool Wait(TimeSpan timeout);
/// What `await` uses: `GetAwaiter().GetResult()` waits as `Wait()`.
public TaskAwaiter GetAwaiter();
}
/// A task that finishes with a `TResult`.
[Intrinsic]
public readonly class Task<TResult> : Task {
/// What it finished with, waited for as `Wait()` does; its failure
/// panics here.
public TResult Result { get; }
public new Task<TResult> ConfigureAwait(bool continueOnCapturedContext);
public new TaskAwaiter<TResult> GetAwaiter();
}
/// What awaits a `Task`: whether it's done, what runs once it is, its end.
[Intrinsic]
public readonly class TaskAwaiter {
public bool IsCompleted { get; }
/// `continuation` run once the task is done, in the awaiting context.
public void OnCompleted(Action continuation);
/// Waits for the task (as `Task.Wait()`); its failure panics here.
public void GetResult();
}
/// What awaits a `Task<TResult>`: its result.
[Intrinsic]
public readonly class TaskAwaiter<TResult> : TaskAwaiter {
public new TResult GetResult();
}
/// A task settled by hand (.NET's `TaskCompletionSource`): what bridges a
/// callback to `await`.
[Intrinsic]
[Threads(ThreadSafety.Guarded)]
public readonly sealed class TaskCompletionSource {
public TaskCompletionSource() {}
public Task Task { get; }
/// It done; a panic when it's settled already.
public readonly void SetResult();
/// It done, unless it's settled already: whether this settled it.
public readonly bool TrySetResult();
/// It failed with `error`; a panic when it's settled already.
public readonly void SetException(Error error);
public readonly bool TrySetException(Error error);
}
/// A task given its result by hand (.NET's `TaskCompletionSource<TResult>`).
[Intrinsic]
[Threads(ThreadSafety.Guarded)]
public readonly sealed class TaskCompletionSource<TResult> {
public TaskCompletionSource() {}
public Task<TResult> Task { get; }
public readonly void SetResult(TResult result);
public readonly bool TrySetResult(TResult result);
public readonly void SetException(Error error);
public readonly bool TrySetException(Error error);
}