final class · System.Threading

Thread

public final class Thread

A thread of its own running a function: new Thread(() => …), Start, Join.

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

Inheritance

Base classes
Any Thread

Constructors

new(func body)
#

Properties

bool IsAlive { get; }
#

read-only

Started and not finished.

i32 ManagedThreadId { get; }
#

read-only

Its number (0 until it runs): the main thread's is 1.

static i32 CurrentId { get; }
#

static read-only

The number of the thread that asks.

Methods

Start()
#

Starts the thread (once).

Join()
#

Waits until the thread has finished.

Join(i32 milliseconds) : bool
#

Waits at most milliseconds: whether the thread has finished.

static Sleep(i32 milliseconds)
#

static

Sleeps milliseconds (other threads run).

static Yield()
#

static

Lets another thread run.

static SpinWait(i32 iterations)
#

static

Waits a little without giving the processor up (in a loop that waits for another thread).

Private and internal members (12)

func body
#

private

i64 handle
#

private

i32 state
#

private

0 not started, 1 started, 2 joined.

AtomicI32 done
#

private

i32 number
#

private

static extern("thread.start") StartNative(func body) : i64
#

private static

static extern("thread.join") JoinNative(i64 handle, i64 milliseconds) : i64
#

private static

static extern("thread.id") CurrentNative() : i64
#

private static

static extern("thread.yield") YieldNative()
#

private static

static extern("time.sleep") SleepFor(i64 milliseconds)
#

private static

static Misused(str what)
#

private static

Run()
#

private

What the new thread runs: the function, then it says it is done.

Inherited members (5)

Source

Thread as Antril, decompiled from its Larva HIR.

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

import System;

into namespace System.Threading
{
	/// A thread of its own running a function: `new Thread(() => …)`, Start, Join.
	public final class Thread
	{
		func body;
		i64 handle;

		/// 0 not started, 1 started, 2 joined.
		i32 state;
		AtomicI32 done;
		i32 number;

		static extern("thread.start") StartNative(func body) : i64;

		static extern("thread.join") JoinNative(i64 handle, i64 milliseconds) : i64;

		static extern("thread.id") CurrentNative() : i64;

		static extern("thread.yield") YieldNative();

		static extern("time.sleep") SleepFor(i64 milliseconds);

		[NoInline]
		static Misused(str what)
		{
			throw new InvalidOperationError("Thread: " + what);
		}

		/// What the new thread runs: the function, then it says it is done.
		Run()
		{
			number = (i32)CurrentNative();
			try
			{
				body();
			}
			finally
			{
				done.Store(1);
			}
		}

		public:
			new(func body)
			{
				this.body = body;
				done = new AtomicI32();
			}

			/// Starts the thread (once).
			Start()
			{
				if (state != 0)
					Misused("it was started already");
				state = 1;
				handle = StartNative(() => { Run(); });
			}

			/// Waits until the thread has finished.
			Join()
			{
				if (state == 0)
					Misused("it was not started");
				if (state == 1)
				{
					JoinNative(handle, -1);
					handle = 0;
					state = 2;
				}
			}

			/// Waits at most `milliseconds`: whether the thread has finished.
			Join(i32 milliseconds) : bool
			{
				if (state == 0)
					Misused("it was not started");
				if (state == 2)
					return true;
				if (JoinNative(handle, milliseconds) == 0)
					return false;
				handle = 0;
				state = 2;
				return true;
			}

			/// Sleeps `milliseconds` (other threads run).
			static Sleep(i32 milliseconds)
			{
				SleepFor(milliseconds);
			}

			/// Lets another thread run.
			static Yield()
			{
				YieldNative();
			}

			/// Waits a little without giving the processor up (in a loop that waits for another thread).
			static SpinWait(i32 iterations)
			{
				i32 sink = 0;
				loop (i32 i = 0..iterations)
					sink += i;
			}

			/// Started and not finished.
			bool IsAlive
			{
				get => state != 0 && done.Load() == 0;
			}

			/// Its number (0 until it runs): the main thread's is 1.
			i32 ManagedThreadId
			{
				get => number;
			}

			/// The number of the thread that asks.
			static i32 CurrentId
			{
				get => (i32)CurrentNative();
			}
	}
}
Esc