2 // System.Threading.Monitor.cs
5 // Dick Porter (dick@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
10 using System.Runtime.CompilerServices;
12 namespace System.Threading
14 public sealed class Monitor
18 // Grabs the mutex on object 'obj', with a maximum
19 // wait time 'ms' but doesn't block - if it can't get
20 // the lock it returns false, true if it can
21 [MethodImplAttribute(MethodImplOptions.InternalCall)]
22 private extern static bool Monitor_try_enter(object obj, int ms);
23 public static void Enter(object obj) {
25 throw new ArgumentNullException("Object is null");
27 //if(obj.GetType().IsValueType==true) {
28 // throw new ArgumentException("Value type");
31 Monitor_try_enter(obj, Timeout.Infinite);
34 // Releases the mutex on object 'obj'
35 [MethodImplAttribute(MethodImplOptions.InternalCall)]
36 private extern static void Monitor_exit(object obj);
38 public static void Exit(object obj) {
40 throw new ArgumentNullException("Object is null");
42 //if(obj.GetType().IsValueType==true) {
43 // throw new ArgumentException("Value type");
49 // Signals one of potentially many objects waiting on
51 [MethodImplAttribute(MethodImplOptions.InternalCall)]
52 private extern static void Monitor_pulse(object obj);
54 // Checks whether object 'obj' is currently synchronised
55 [MethodImplAttribute(MethodImplOptions.InternalCall)]
56 private extern static bool Monitor_test_synchronised(object obj);
58 public static void Pulse(object obj) {
60 throw new ArgumentNullException("Object is null");
62 if(Monitor_test_synchronised(obj)==false) {
63 throw new SynchronizationLockException("Object is not synchronized");
69 // Signals all of potentially many objects waiting on
71 [MethodImplAttribute(MethodImplOptions.InternalCall)]
72 private extern static void Monitor_pulse_all(object obj);
74 public static void PulseAll(object obj) {
76 throw new ArgumentNullException("Object is null");
78 if(Monitor_test_synchronised(obj)==false) {
79 throw new SynchronizationLockException("Object is not synchronized");
82 Monitor_pulse_all(obj);
85 public static bool TryEnter(object obj) {
87 throw new ArgumentNullException("Object is null");
89 //if(obj.GetType().IsValueType==true) {
90 // throw new ArgumentException("Value type");
93 return(Monitor_try_enter(obj, 0));
96 public static bool TryEnter(object obj, int millisecondsTimeout) {
98 throw new ArgumentNullException("Object is null");
100 //if(obj.GetType().IsValueType==true) {
101 // throw new ArgumentException("Value type");
104 // LAMESPEC: should throw an exception when ms<0, but
105 // Timeout.Infinite is -1
106 if(millisecondsTimeout == Timeout.Infinite) {
111 if(millisecondsTimeout<0) {
112 throw new ArgumentException("millisecondsTimeout negative");
115 return(Monitor_try_enter(obj, millisecondsTimeout));
118 public static bool TryEnter(object obj, TimeSpan timeout) {
120 throw new ArgumentNullException("Object is null");
122 //if(obj.GetType().IsValueType==true) {
123 // throw new ArgumentException("Value type");
126 // LAMESPEC: should throw an exception when ms<0, but
127 // Timeout.Infinite is -1
128 int ms=Convert.ToInt32(timeout.TotalMilliseconds);
130 if(ms == Timeout.Infinite) {
135 if(ms < 0 || ms > Int32.MaxValue) {
136 throw new ArgumentOutOfRangeException("timeout out of range");
139 return(Monitor_try_enter(obj, ms));
142 // Waits for a signal on object 'obj' with maximum
143 // wait time 'ms'. Returns true if the object was
144 // signalled, false if it timed out
145 [MethodImplAttribute(MethodImplOptions.InternalCall)]
146 private extern static bool Monitor_wait(object obj, int ms);
148 public static bool Wait(object obj) {
150 throw new ArgumentNullException("Object is null");
152 if(Monitor_test_synchronised(obj)==false) {
153 throw new SynchronizationLockException("Object is not synchronized");
156 return(Monitor_wait(obj, Timeout.Infinite));
159 public static bool Wait(object obj, int millisecondsTimeout) {
161 throw new ArgumentNullException("Object is null");
163 if(Monitor_test_synchronised(obj)==false) {
164 throw new SynchronizationLockException("Object is not synchronized");
166 // LAMESPEC: no mention of timeout sanity checking
168 return(Monitor_wait(obj, millisecondsTimeout));
171 public static bool Wait(object obj, TimeSpan timeout) {
173 throw new ArgumentNullException("Object is null");
175 // LAMESPEC: says to throw ArgumentException too
176 int ms=Convert.ToInt32(timeout.TotalMilliseconds);
178 if(ms < 0 || ms > Int32.MaxValue) {
179 throw new ArgumentOutOfRangeException("timeout out of range");
181 if(Monitor_test_synchronised(obj)==false) {
182 throw new SynchronizationLockException("Object is not synchronized");
185 return(Monitor_wait(obj, ms));
189 public static bool Wait(object obj, int millisecondsTimeout, bool exitContext) {
191 throw new ArgumentNullException("Object is null");
193 // FIXME when I understand what a
194 // "synchronisation domain" is and does
199 public static bool Wait(object obj, TimeSpan timeout, bool exitContext) {
201 throw new ArgumentNullException("Object is null");
203 // LAMESPEC: says to throw ArgumentException too
204 int ms=Convert.ToInt32(timeout.TotalMilliseconds);
206 if(ms < 0 || ms > Int32.MaxValue) {
207 throw new ArgumentOutOfRangeException("timeout out of range");
210 // FIXME when I understand what a
211 // "synchronisation domain" is and does