class · System.Threading.Tasks

Task

public class Task

Work that finishes later: an async function, a delay, several tasks together. In an async function, await waits for one; elsewhere, Wait() runs the scheduler until it is done. A task finishes once: done, or failed (await and Wait() then throw its error).

Kind
class
Namespace
System.Threading.Tasks
Assembly
System.Core
Access
public
Source file
Tasks.ant

Inheritance

Base classes
Any Task
Derived types
CompleteTask Task<T> WhenAllTask

Properties

bool IsCompleted { get; }
#

read-only

Whether it has finished (done or failed).

bool IsFaulted { get; }
#

read-only

Whether it failed.

static Task CompletedTask { get; }
#

static read-only

A task done.

Methods

Complete()
#

Done: what waits for it runs (in its turn).

Fail(any failure)
#

Failed with failure: what waits for it runs and finds the error.

virtual Run()
#

virtual

Something this task waits for is done (an async function goes on from its await). The scheduler calls it.

Pause(Task awaited) : bool
#

inherited

An async function at an await: false when awaited is done (it goes on); otherwise this task runs again once it is, and true (the function pauses).

AddWaiter(Task task)
#

inherited

AddWaiterLocked(Task task)
#

inherited

Finish(i32 how, any? failure)
#

inherited

Finishes it once threads complete tasks: under the scheduler's lock, and every waiting thread is told.

Wake()
#

inherited

Wait()
#

Runs the scheduler until this task has finished (on another thread than the scheduler's: waits for it); throws its error if it failed.

Check()
#

What await does once the task has finished: throws its error if it failed.

static Delay(i32 milliseconds) : Task
#

static

A task done after milliseconds (measured by the scheduler, which sleeps when nothing else is ready).

static Yield() : Task
#

static

A task done once the tasks ready now have had their turn: await Task.Yield(); lets them run.

static Done() : Task
#

static

static WhenAll(Task[] tasks) : Task
#

static

A task done when every one of tasks has finished (failed if one of them failed: the first error).

static Run(func body) : Task
#

static

Runs body on the thread pool: a task done when it returns (failed if it throws).

static Run<T>(func : T body) : Task<T>
#

static

Runs body on the thread pool: a task with what it returns.

Private and internal members (4)

i32 state
#

private

0 running, 1 done, 2 failed.

any? error
#

private

Task? waiter
#

private

The tasks waiting for this one: the first in a field (most tasks have one), the others in a list.

List<Task>? waiters
#

private

Inherited members (5)

Source

Task as Antril, decompiled from its Larva HIR.

System.Threading.Tasks.Task.ant
// System.Threading.Tasks.Task — System.Core (decompiled from Larva HIR)

import System;
import System.Threading;

into namespace System.Threading.Tasks
{
	/// Work that finishes later: an async function, a delay, several tasks together. In an async function, `await`
	/// waits for one; elsewhere, `Wait()` runs the scheduler until it is done. A task finishes once: done, or failed
	/// (`await` and `Wait()` then throw its error).
	public class Task
	{
		/// 0 running, 1 done, 2 failed.
		i32 state;
		any? error;

		/// The tasks waiting for this one: the first in a field (most tasks have one), the others in a list.
		Task? waiter;
		List<Task>? waiters;

		inherited:
			/// An async function at an `await`: false when `awaited` is done (it goes on); otherwise this task runs again
			/// once it is, and true (the function pauses).
			Pause(Task awaited) : bool
			{
				if (awaited.state != 0)
					return false;
				awaited.AddWaiter(this);
				return true;
			}

			AddWaiter(Task task)
			{
				if (Scheduler.Threaded)
				{
					Scheduler.Sync.Lock();
					AddWaiterLocked(task);
					Scheduler.Sync.Unlock();
					return;
				}
				AddWaiterLocked(task);
			}

			AddWaiterLocked(Task task)
			{
				if (state != 0)
				{
					Scheduler.Ready(task);
					return;
				}
				if (waiter is null)
				{
					waiter = task;
					return;
				}
				List<Task> more = waiters ?? new List<Task>();
				waiters = more;
				more.Add(task);
			}

			/// Finishes it once threads complete tasks: under the scheduler's lock, and every waiting thread is told.
			[NoInline]
			Finish(i32 how, any? failure)
			{
				Scheduler.Sync.Lock();
				if (state != 0)
				{
					Scheduler.Sync.Unlock();
					return;
				}
				error = failure;
				state = how;
				Wake();
				bool sleeping = Scheduler.Sleeping;
				Scheduler.Sync.Unlock();
				if (sleeping)
					Scheduler.Changed.Broadcast();
			}

			[NoInline]
			Wake()
			{
				if (waiter is Task first)
				{
					waiter = null;
					Scheduler.Ready(first);
				}
				if (waiters is List<Task> more)
				{
					waiters = null;
					{
						ListIterator<Task> _iterator2 = more.Iterator();
						loop (_iterator2.Next())
						{
							Task task = _iterator2.Current;
							Scheduler.Ready(task);
						}
					}
				}
			}

		public:
			/// Done: what waits for it runs (in its turn).
			Complete()
			{
				if (Scheduler.Threaded)
				{
					Finish(1, null);
					return;
				}
				if (state != 0)
					return;
				state = 1;
				Wake();
			}

			/// Failed with `failure`: what waits for it runs and finds the error.
			Fail(any failure)
			{
				if (Scheduler.Threaded)
				{
					Finish(2, failure);
					return;
				}
				if (state != 0)
					return;
				error = failure;
				state = 2;
				Wake();
			}

			/// Something this task waits for is done (an async function goes on from its `await`). The scheduler calls it.
			virtual Run()
			{
			}

			/// Runs the scheduler until this task has finished (on another thread than the scheduler's: waits for it);
			/// throws its error if it failed.
			Wait()
			{
				if (state == 0)
				{
					if (Scheduler.Threaded && !Scheduler.OnItsThread())
						Scheduler.BlockUntil(this);
					else
						Scheduler.RunUntil(this);
				}
				Check();
			}

			/// What `await` does once the task has finished: throws its error if it failed.
			Check()
			{
				if (state == 2 && error is any failure)
					throw failure;
			}

			/// A task done after `milliseconds` (measured by the scheduler, which sleeps when nothing else is ready).
			static Delay(i32 milliseconds) : Task
			{
				Task task = new Task();
				Scheduler.After(task, milliseconds);
				return task;
			}

			/// A task done once the tasks ready now have had their turn: `await Task.Yield();` lets them run.
			static Yield() : Task
			{
				Task task = new Task();
				Scheduler.Soon(task);
				return task;
			}

			static Done() : Task
			{
				Task task = new Task();
				task.state = 1;
				return task;
			}

			/// A task done when every one of `tasks` has finished (failed if one of them failed: the first error).
			static WhenAll(Task[] tasks) : Task
			{
				WhenAllTask all = new WhenAllTask(tasks);
				all.Run();
				return all;
			}

			/// Runs `body` on the thread pool: a task done when it returns (failed if it throws).
			static Run(func body) : Task
			{
				Task task = new Task();
				Scheduler.Started();
				ThreadPool.QueueUserWorkItem(() => { try { body(); task.Complete(); } catch (any failure) { task.Fail(failure); } Scheduler.Finished(); });
				return task;
			}

			/// Runs `body` on the thread pool: a task with what it returns.
			static Run<T>(func : T body) : Task<T>
			{
				Task<T> task = new Task<T>();
				Scheduler.Started();
				ThreadPool.QueueUserWorkItem(() => { try { task.SetResult(body()); } catch (any failure) { task.Fail(failure); } Scheduler.Finished(); });
				return task;
			}

			/// Whether it has finished (done or failed).
			bool IsCompleted
			{
				get => state != 0;
			}

			/// Whether it failed.
			bool IsFaulted
			{
				get => state == 2;
			}

			/// A task done.
			static Task CompletedTask
			{
				get => Done();
			}
	}
}
Esc