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).

Tipo
class
Espacio de nombres
System.Threading
Ensamblado
System.Core
Acceso
public
Archivo fuente
Threading.ant

Herencia

Clases base
Any Mutex
Tipos derivados
Lock

Constructores

new()
#

Propiedades

i64 Handle { get; }
#

read-only

The handle (for Condition).

Métodos

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

Miembros privados e internos (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

Miembros heredados (4)

Código fuente

Mutex en Antril, descompilado a partir de su 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