#if NET_2_0
using System.Runtime.ConstrainedExecution;
+using System.Runtime.InteropServices;
#endif
namespace System.Threading
{
+#if NET_2_0
+ [ComVisible (true)]
+ public static class Monitor
+ {
+#else
public sealed class Monitor
{
private Monitor () {}
+#endif
// Grabs the mutex on object 'obj', with a maximum
// wait time 'ms' but doesn't block - if it can't get
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern static bool Monitor_try_enter(object obj, int ms);
-#if NET_2_0
- [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
-#endif
- public static void Enter(object obj) {
- if(obj==null) {
- throw new ArgumentNullException("Object is null");
- }
- //if(obj.GetType().IsValueType==true) {
- // throw new ArgumentException("Value type");
- //}
-
- Monitor_try_enter(obj, Timeout.Infinite);
- }
+ // Enter/Exit are implemented directly as icalls for performance reasons
- // Releases the mutex on object 'obj'
+ // Acquires the mutex on object 'obj'
[MethodImplAttribute(MethodImplOptions.InternalCall)]
- private extern static void Monitor_exit(object obj);
+ public extern static void Enter(object obj);
+ // Releases the mutex on object 'obj'
#if NET_2_0
[ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
#endif
- public static void Exit(object obj) {
- if(obj==null) {
- throw new ArgumentNullException("Object is null");
- }
- //if(obj.GetType().IsValueType==true) {
- // throw new ArgumentException("Value type");
- //}
-
- Monitor_exit(obj);
- }
+ [MethodImplAttribute(MethodImplOptions.InternalCall)]
+ public extern static void Exit(object obj);
// Signals one of potentially many objects waiting on
// object 'obj'
public static void Pulse(object obj) {
if(obj==null) {
- throw new ArgumentNullException("Object is null");
+ throw new ArgumentNullException("obj");
}
if(Monitor_test_synchronised(obj)==false) {
throw new SynchronizationLockException("Object is not synchronized");
public static void PulseAll(object obj) {
if(obj==null) {
- throw new ArgumentNullException("Object is null");
+ throw new ArgumentNullException("obj");
}
if(Monitor_test_synchronised(obj)==false) {
throw new SynchronizationLockException("Object is not synchronized");
public static bool TryEnter(object obj) {
if(obj==null) {
- throw new ArgumentNullException("Object is null");
+ throw new ArgumentNullException("obj");
}
//if(obj.GetType().IsValueType==true) {
// throw new ArgumentException("Value type");
public static bool TryEnter(object obj, int millisecondsTimeout) {
if(obj==null) {
- throw new ArgumentNullException("Object is null");
+ throw new ArgumentNullException("obj");
}
//if(obj.GetType().IsValueType==true) {
// throw new ArgumentException("Value type");
}
if(millisecondsTimeout<0) {
- throw new ArgumentException("millisecondsTimeout negative");
+ throw new ArgumentException("millisecondsTimeout", "negative value for millisecondsTimeout");
}
return(Monitor_try_enter(obj, millisecondsTimeout));
public static bool TryEnter(object obj, TimeSpan timeout) {
if(obj==null) {
- throw new ArgumentNullException("Object is null");
+ throw new ArgumentNullException("obj");
}
//if(obj.GetType().IsValueType==true) {
// throw new ArgumentException("Value type");
}
if(ms < 0 || ms > Int32.MaxValue) {
- throw new ArgumentOutOfRangeException("timeout out of range");
+ throw new ArgumentOutOfRangeException("timeout", "timeout out of range");
}
return(Monitor_try_enter(obj, ms));
public static bool Wait(object obj) {
if(obj==null) {
- throw new ArgumentNullException("Object is null");
+ throw new ArgumentNullException("obj");
}
if(Monitor_test_synchronised(obj)==false) {
throw new SynchronizationLockException("Object is not synchronized");
public static bool Wait(object obj, int millisecondsTimeout) {
if(obj==null) {
- throw new ArgumentNullException("Object is null");
+ throw new ArgumentNullException("obj");
}
if(Monitor_test_synchronised(obj)==false) {
throw new SynchronizationLockException("Object is not synchronized");
}
- // LAMESPEC: no mention of timeout sanity checking
+ if (millisecondsTimeout < 0 && millisecondsTimeout != Timeout.Infinite)
+ throw new ArgumentOutOfRangeException ("millisecondsTimeout", "timeout out of range");
return(Monitor_wait(obj, millisecondsTimeout));
}
public static bool Wait(object obj, TimeSpan timeout) {
if(obj==null) {
- throw new ArgumentNullException("Object is null");
+ throw new ArgumentNullException("obj");
}
// LAMESPEC: says to throw ArgumentException too
int ms=Convert.ToInt32(timeout.TotalMilliseconds);
- if(ms < 0 || ms > Int32.MaxValue) {
- throw new ArgumentOutOfRangeException("timeout out of range");
+ if((ms < 0 && ms != Timeout.Infinite) || ms > Int32.MaxValue) {
+ throw new ArgumentOutOfRangeException("timeout", "timeout out of range");
}
if(Monitor_test_synchronised(obj)==false) {
throw new SynchronizationLockException("Object is not synchronized");