2 // System.Threading.Monitor.cs
5 // Dick Porter (dick@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Runtime.CompilerServices;
34 using System.Runtime.Remoting.Contexts;
37 using System.Runtime.ConstrainedExecution;
40 namespace System.Threading
42 public sealed class Monitor
46 // Grabs the mutex on object 'obj', with a maximum
47 // wait time 'ms' but doesn't block - if it can't get
48 // the lock it returns false, true if it can
49 [MethodImplAttribute(MethodImplOptions.InternalCall)]
50 private extern static bool Monitor_try_enter(object obj, int ms);
52 public static void Enter(object obj) {
54 throw new ArgumentNullException("obj");
56 //if(obj.GetType().IsValueType==true) {
57 // throw new ArgumentException("Value type");
60 Monitor_try_enter(obj, Timeout.Infinite);
63 // Releases the mutex on object 'obj'
64 [MethodImplAttribute(MethodImplOptions.InternalCall)]
65 private extern static void Monitor_exit(object obj);
68 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
70 public static void Exit(object obj) {
72 throw new ArgumentNullException("obj");
74 //if(obj.GetType().IsValueType==true) {
75 // throw new ArgumentException("Value type");
81 // Signals one of potentially many objects waiting on
83 [MethodImplAttribute(MethodImplOptions.InternalCall)]
84 private extern static void Monitor_pulse(object obj);
86 // Checks whether object 'obj' is currently synchronised
87 [MethodImplAttribute(MethodImplOptions.InternalCall)]
88 private extern static bool Monitor_test_synchronised(object obj);
90 public static void Pulse(object obj) {
92 throw new ArgumentNullException("obj");
94 if(Monitor_test_synchronised(obj)==false) {
95 throw new SynchronizationLockException("Object is not synchronized");
101 // Signals all of potentially many objects waiting on
103 [MethodImplAttribute(MethodImplOptions.InternalCall)]
104 private extern static void Monitor_pulse_all(object obj);
106 public static void PulseAll(object obj) {
108 throw new ArgumentNullException("obj");
110 if(Monitor_test_synchronised(obj)==false) {
111 throw new SynchronizationLockException("Object is not synchronized");
114 Monitor_pulse_all(obj);
117 public static bool TryEnter(object obj) {
119 throw new ArgumentNullException("obj");
121 //if(obj.GetType().IsValueType==true) {
122 // throw new ArgumentException("Value type");
125 return(Monitor_try_enter(obj, 0));
128 public static bool TryEnter(object obj, int millisecondsTimeout) {
130 throw new ArgumentNullException("obj");
132 //if(obj.GetType().IsValueType==true) {
133 // throw new ArgumentException("Value type");
136 // LAMESPEC: should throw an exception when ms<0, but
137 // Timeout.Infinite is -1
138 if(millisecondsTimeout == Timeout.Infinite) {
143 if(millisecondsTimeout<0) {
144 throw new ArgumentException("millisecondsTimeout", "negative value for millisecondsTimeout");
147 return(Monitor_try_enter(obj, millisecondsTimeout));
150 public static bool TryEnter(object obj, TimeSpan timeout) {
152 throw new ArgumentNullException("obj");
154 //if(obj.GetType().IsValueType==true) {
155 // throw new ArgumentException("Value type");
158 // LAMESPEC: should throw an exception when ms<0, but
159 // Timeout.Infinite is -1
160 int ms=Convert.ToInt32(timeout.TotalMilliseconds);
162 if(ms == Timeout.Infinite) {
167 if(ms < 0 || ms > Int32.MaxValue) {
168 throw new ArgumentOutOfRangeException("timeout", "timeout out of range");
171 return(Monitor_try_enter(obj, ms));
174 // Waits for a signal on object 'obj' with maximum
175 // wait time 'ms'. Returns true if the object was
176 // signalled, false if it timed out
177 [MethodImplAttribute(MethodImplOptions.InternalCall)]
178 private extern static bool Monitor_wait(object obj, int ms);
180 public static bool Wait(object obj) {
182 throw new ArgumentNullException("obj");
184 if(Monitor_test_synchronised(obj)==false) {
185 throw new SynchronizationLockException("Object is not synchronized");
188 return(Monitor_wait(obj, Timeout.Infinite));
191 public static bool Wait(object obj, int millisecondsTimeout) {
193 throw new ArgumentNullException("obj");
195 if(Monitor_test_synchronised(obj)==false) {
196 throw new SynchronizationLockException("Object is not synchronized");
198 if (millisecondsTimeout < 0 && millisecondsTimeout != Timeout.Infinite)
199 throw new ArgumentOutOfRangeException ("millisecondsTimeout", "timeout out of range");
201 return(Monitor_wait(obj, millisecondsTimeout));
204 public static bool Wait(object obj, TimeSpan timeout) {
206 throw new ArgumentNullException("obj");
208 // LAMESPEC: says to throw ArgumentException too
209 int ms=Convert.ToInt32(timeout.TotalMilliseconds);
211 if((ms < 0 && ms != Timeout.Infinite) || ms > Int32.MaxValue) {
212 throw new ArgumentOutOfRangeException("timeout", "timeout out of range");
214 if(Monitor_test_synchronised(obj)==false) {
215 throw new SynchronizationLockException("Object is not synchronized");
218 return(Monitor_wait(obj, ms));
221 public static bool Wait(object obj, int millisecondsTimeout, bool exitContext) {
223 if (exitContext) SynchronizationAttribute.ExitContext ();
224 return Wait (obj, millisecondsTimeout);
227 if (exitContext) SynchronizationAttribute.EnterContext ();
231 public static bool Wait(object obj, TimeSpan timeout, bool exitContext) {
233 if (exitContext) SynchronizationAttribute.ExitContext ();
234 return Wait (obj, timeout);
237 if (exitContext) SynchronizationAttribute.EnterContext ();