class · System.Threading

Mutex

public class Mutex

A lock: one thread at a time between Lock and Unlock (not re-entrant: a thread that holds it must not lock it again). Run(() => …) holds it around a function. Dispose frees its handle (an undisposed one is freed when the object dies).

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

Inheritance

Base classes
Any Mutex
Derived types
Lock

Constructors

new()
#

Properties

i64 Handle { get; }
#

read-only

The handle (for Condition).

Methods

Lock()
#
Unlock()
#
TryLock() : bool
#

Takes it when no other thread holds it: whether it did.

WaitOne()
#

WaitOne and ReleaseMutex: the same as Lock and Unlock.

ReleaseMutex()
#
Run(func body)
#

Runs body holding the lock.

Dispose()
#

Frees it (it must not be used after).

override destroy()
#

override

Private and internal members (6)

i64 handle
#

private

static extern("mutex.new") New() : i64
#

private static

static extern("mutex.lock") LockNative(i64 handle)
#

private static

static extern("mutex.trylock") TryNative(i64 handle) : i64
#

private static

static extern("mutex.unlock") UnlockNative(i64 handle)
#

private static

static extern("mutex.free") Free(i64 handle)
#

private static

Inherited members (4)

Source

Mutex as Antril, decompiled from its Larva HIR.

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

into namespace System.Threading
{
	/// A lock: one thread at a time between Lock and Unlock (not re-entrant: a thread that holds it must not lock it again).
	/// `Run(() => …)` holds it around a function. Dispose frees its handle (an undisposed one is freed when the object dies).
	public class Mutex
	{
		i64 handle;

		static extern("mutex.new") New() : i64;

		static extern("mutex.lock") LockNative(i64 handle);

		static extern("mutex.trylock") TryNative(i64 handle) : i64;

		static extern("mutex.unlock") UnlockNative(i64 handle);

		static extern("mutex.free") Free(i64 handle);

		public:
			new()
			{
				handle = New();
			}

			Lock()
			{
				LockNative(handle);
			}

			Unlock()
			{
				UnlockNative(handle);
			}

			/// Takes it when no other thread holds it: whether it did.
			TryLock() : bool => TryNative(handle) != 0;

			/// WaitOne and ReleaseMutex: the same as Lock and Unlock.
			WaitOne()
			{
				LockNative(handle);
			}

			ReleaseMutex()
			{
				UnlockNative(handle);
			}

			/// Runs `body` holding the lock.
			Run(func body)
			{
				LockNative(handle);
				try
				{
					body();
				}
				finally
				{
					UnlockNative(handle);
				}
			}

			/// Frees it (it must not be used after).
			Dispose()
			{
				if (handle != 0)
					Free(handle);
				handle = 0;
			}

			override destroy()
			{
				try
				{
					Dispose();
				}
				finally
				{
					base.destroy();
				}
			}

			/// The handle (for Condition).
			i64 Handle
			{
				get => handle;
			}
	}
}
Esc