// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
namespace System.Threading
{
using System;
using System.Threading;
using System.Runtime.CompilerServices;
using Microsoft.Singularity;
using Microsoft.Singularity.V1.Threads;
using Microsoft.Singularity.V1.Services;
//|
[CLSCompliant(false)]
public sealed class Mutex : WaitHandle
{
private MutexHandle handle;
//|
public Mutex(bool initiallyOwned)
{
MutexHandle handleOnStack;
if (!MutexHandle.Create(initiallyOwned, out handleOnStack)) {
throw new HandleCreateException();
}
handle = handleOnStack;
}
//|
public Mutex()
{
MutexHandle handleOnStack;
if (!MutexHandle.Create(false, out handleOnStack)) {
throw new HandleCreateException();
}
handle = handleOnStack;
}
///
/// Finalizer is responsible for freeing handle that keeps corresponding
/// kernel AutoResetEvent object live.
///
~Mutex() {
if (this.handle.id != 0) {
MutexHandle.Dispose(this.handle);
this.handle = new MutexHandle();
}
}
public bool AcquireMutex()
{
return WaitOne();
}
public bool AcquireMutex(SchedulerTime stop)
{
return WaitOne(stop);
}
//|
public void ReleaseMutex()
{
MutexHandle.Release(handle);
GC.KeepAlive(this);
}
// Called by monitor to see if its lock is held by the current thread.
internal bool IsOwnedByCurrentThread()
{
bool b = MutexHandle.IsOwnedByCurrentThread(handle);
GC.KeepAlive(this);
return b;
}
protected override SyncHandle Handle{
get {
return handle;
}
}
//|
public override bool WaitOne(TimeSpan timeout)
{
bool b = SyncHandle.WaitOne(handle, timeout);
GC.KeepAlive(this);
return b;
}
//|
public override bool WaitOne(SchedulerTime stop)
{
bool b = SyncHandle.WaitOne(handle, stop);
GC.KeepAlive(this);
return b;
}
//|
public override bool WaitOne()
{
bool b = SyncHandle.WaitOne(handle);
GC.KeepAlive(this);
return b;
}
//|
protected override void Dispose(bool explicitDisposing)
{
if (handle.id != 0) {
MutexHandle.Dispose(handle);
handle = new MutexHandle();
}
}
}
}