// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
//=============================================================================
//
// Class: WaitHandle (this name is NOT definitive)
//
// Purpose: Class to represent all synchronization objects in the runtime (that allow multiple wait)
//
//=============================================================================
namespace System.Threading
{
using System;
using System.Threading;
using System.Runtime.CompilerServices;
using System.Collections;
using Microsoft.Singularity;
using Microsoft.Singularity.V1.Threads;
using Microsoft.Singularity.V1.Services;
//|
[CLSCompliant(false)]
public abstract class WaitHandle : IDisposable
{
//|
public const int WaitTimeout = -1;
protected WaitHandle()
{
}
protected abstract SyncHandle Handle{
get;
}
//|
public abstract bool WaitOne(TimeSpan timeout);
//|
public abstract bool WaitOne(SchedulerTime stop);
[Obsolete("Do not use DateTime for scheduling. It is meaningless. Use SchedulerTime")]
public bool WaitOne(DateTime stop) {
SchedulerTime st = SchedulerTime.MinValue + (stop - DateTime.BootTime);
return WaitOne(st);
}
//|
public abstract bool WaitOne();
//|
public static unsafe int WaitAny(WaitHandle[] waitHandles,
TimeSpan timeout)
{
SyncHandle[] handles = Thread.CurrentThread.GetSyncHandles(waitHandles.Length);
for (int i = 0; i < waitHandles.Length; i++) {
handles[i] = waitHandles[i].Handle;
}
fixed (SyncHandle *array = &handles[0]) {
return SyncHandle.WaitAny(array, waitHandles.Length, timeout);
}
}
//|
public static unsafe int WaitAny(WaitHandle[] waitHandles,
SchedulerTime stop)
{
SyncHandle[] handles = Thread.CurrentThread.GetSyncHandles(waitHandles.Length);
for (int i = 0; i < waitHandles.Length; i++) {
handles[i] = waitHandles[i].Handle;
}
fixed (SyncHandle *array = &handles[0]) {
return SyncHandle.WaitAny(array, waitHandles.Length, stop);
}
}
//|
public static unsafe int WaitAny(WaitHandle[] waitHandles)
{
SyncHandle[] handles = Thread.CurrentThread.GetSyncHandles(waitHandles.Length);
for (int i = 0; i < waitHandles.Length; i++) {
handles[i] = waitHandles[i].Handle;
}
fixed (SyncHandle *array = &handles[0]) {
return SyncHandle.WaitAny(array, waitHandles.Length);
}
}
//|
protected abstract void Dispose(bool explicitDisposing);
//|
public virtual void Close()
{
Dispose(true);
GC.nativeSuppressFinalize(this);
}
//|
///
void IDisposable.Dispose()
{
Dispose(true);
GC.nativeSuppressFinalize(this);
}
//|
~WaitHandle()
{
Dispose(false);
}
public static implicit operator WaitHandle(SyncHandle s) {
return new SyncWaitHandle(s);
}
}
internal class SyncWaitHandle : WaitHandle
{
private readonly SyncHandle _sync;
public SyncWaitHandle(SyncHandle s) {
_sync = s;
}
protected override SyncHandle Handle {
get { return _sync; }
}
public override bool WaitOne(TimeSpan timeout) {
return SyncHandle.WaitOne(Handle, timeout);
}
public override bool WaitOne(SchedulerTime stop) {
return SyncHandle.WaitOne(Handle, stop);
}
public override bool WaitOne() {
return SyncHandle.WaitOne(Handle);
}
protected override void Dispose(bool explicitDisposing) {
// todo: implement
}
}
}