2014-08-13 10:39:27 +01:00
|
|
|
//
|
|
|
|
// System.Threading.WaitHandle.cs
|
|
|
|
//
|
|
|
|
// Author:
|
|
|
|
// Dick Porter (dick@ximian.com)
|
|
|
|
// Gonzalo Paniagua Javier (gonzalo@ximian.com
|
|
|
|
//
|
|
|
|
// (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
|
|
|
|
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
// a copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
// permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
// the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
// included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Runtime.Remoting.Contexts;
|
|
|
|
using System.Security.Permissions;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using Microsoft.Win32.SafeHandles;
|
|
|
|
using System.Runtime.ConstrainedExecution;
|
|
|
|
|
|
|
|
namespace System.Threading
|
|
|
|
{
|
|
|
|
[StructLayout (LayoutKind.Sequential)]
|
2016-08-03 10:59:49 +00:00
|
|
|
public abstract partial class WaitHandle
|
2014-08-13 10:39:27 +01:00
|
|
|
: MarshalByRefObject, IDisposable
|
|
|
|
{
|
2016-08-03 10:59:49 +00:00
|
|
|
protected static readonly IntPtr InvalidHandle = (IntPtr) (-1);
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2016-08-03 10:59:49 +00:00
|
|
|
static int WaitMultiple(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext, bool WaitAll)
|
2014-08-13 10:39:27 +01:00
|
|
|
{
|
2016-08-03 10:59:49 +00:00
|
|
|
int release_last = -1;
|
2014-08-13 10:39:27 +01:00
|
|
|
|
|
|
|
try {
|
2016-08-03 10:59:49 +00:00
|
|
|
#if !DISABLE_REMOTING
|
|
|
|
if (exitContext)
|
2014-08-13 10:39:27 +01:00
|
|
|
SynchronizationAttribute.ExitContext ();
|
|
|
|
#endif
|
|
|
|
|
2016-08-03 10:59:49 +00:00
|
|
|
for (int i = 0; i < waitHandles.Length; ++i) {
|
|
|
|
try {
|
|
|
|
} finally {
|
|
|
|
/* we have to put it in a finally block, to avoid having a ThreadAbortException
|
|
|
|
* between the return from DangerousAddRef and the assignement to release_last */
|
|
|
|
bool release = false;
|
|
|
|
waitHandles [i].SafeWaitHandle.DangerousAddRef (ref release);
|
|
|
|
release_last = i;
|
|
|
|
}
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
2016-08-03 10:59:49 +00:00
|
|
|
if (WaitAll)
|
|
|
|
return WaitAll_internal (waitHandles, millisecondsTimeout);
|
|
|
|
else
|
|
|
|
return WaitAny_internal (waitHandles, millisecondsTimeout);
|
|
|
|
} finally {
|
|
|
|
for (int i = release_last; i >= 0; --i) {
|
|
|
|
waitHandles [i].SafeWaitHandle.DangerousRelease ();
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
2016-08-03 10:59:49 +00:00
|
|
|
#if !DISABLE_REMOTING
|
|
|
|
if (exitContext)
|
|
|
|
SynchronizationAttribute.EnterContext ();
|
2014-08-13 10:39:27 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[MethodImplAttribute(MethodImplOptions.InternalCall)]
|
2016-08-03 10:59:49 +00:00
|
|
|
private static extern int WaitAll_internal(WaitHandle[] handles, int ms);
|
2014-08-13 10:39:27 +01:00
|
|
|
|
|
|
|
[MethodImplAttribute(MethodImplOptions.InternalCall)]
|
2016-08-03 10:59:49 +00:00
|
|
|
private static extern int WaitAny_internal(WaitHandle[] handles, int ms);
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2016-08-03 10:59:49 +00:00
|
|
|
static int WaitOneNative (SafeHandle waitableSafeHandle, uint millisecondsTimeout, bool hasThreadAffinity, bool exitContext)
|
2014-08-13 10:39:27 +01:00
|
|
|
{
|
|
|
|
bool release = false;
|
|
|
|
try {
|
2016-08-03 10:59:49 +00:00
|
|
|
#if !DISABLE_REMOTING
|
2014-08-13 10:39:27 +01:00
|
|
|
if (exitContext)
|
2016-08-03 10:59:49 +00:00
|
|
|
SynchronizationAttribute.ExitContext ();
|
2014-08-13 10:39:27 +01:00
|
|
|
#endif
|
|
|
|
|
2016-08-03 10:59:49 +00:00
|
|
|
waitableSafeHandle.DangerousAddRef (ref release);
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2016-08-03 10:59:49 +00:00
|
|
|
return WaitOne_internal (waitableSafeHandle.DangerousGetHandle (), (int) millisecondsTimeout);
|
|
|
|
} finally {
|
|
|
|
if (release)
|
|
|
|
waitableSafeHandle.DangerousRelease ();
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2016-08-03 10:59:49 +00:00
|
|
|
#if !DISABLE_REMOTING
|
2014-08-13 10:39:27 +01:00
|
|
|
if (exitContext)
|
|
|
|
SynchronizationAttribute.EnterContext ();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-03 10:59:49 +00:00
|
|
|
[MethodImplAttribute(MethodImplOptions.InternalCall)]
|
|
|
|
static extern int WaitOne_internal(IntPtr handle, int ms);
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2016-08-03 10:59:49 +00:00
|
|
|
static int SignalAndWaitOne (SafeWaitHandle waitHandleToSignal,SafeWaitHandle waitHandleToWaitOn, int millisecondsTimeout, bool hasThreadAffinity, bool exitContext)
|
2014-08-13 10:39:27 +01:00
|
|
|
{
|
2016-08-03 10:59:49 +00:00
|
|
|
bool releaseHandleToSignal = false, releaseHandleToWaitOn = false;
|
|
|
|
try {
|
|
|
|
waitHandleToSignal.DangerousAddRef (ref releaseHandleToSignal);
|
|
|
|
waitHandleToWaitOn.DangerousAddRef (ref releaseHandleToWaitOn);
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2016-08-03 10:59:49 +00:00
|
|
|
return SignalAndWait_Internal (waitHandleToSignal.DangerousGetHandle (), waitHandleToWaitOn.DangerousGetHandle (), millisecondsTimeout);
|
|
|
|
} finally {
|
|
|
|
if (releaseHandleToSignal)
|
|
|
|
waitHandleToSignal.DangerousRelease ();
|
|
|
|
if (releaseHandleToWaitOn)
|
|
|
|
waitHandleToWaitOn.DangerousRelease ();
|
|
|
|
}
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
2016-08-03 10:59:49 +00:00
|
|
|
|
|
|
|
[MethodImplAttribute(MethodImplOptions.InternalCall)]
|
|
|
|
static extern int SignalAndWait_Internal (IntPtr toSignal, IntPtr toWaitOn, int ms);
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
}
|