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;
38 using System.Runtime.InteropServices;
41 namespace System.Threading
45 public static class Monitor
48 public sealed class Monitor
53 // Grabs the mutex on object 'obj', with a maximum
54 // wait time 'ms' but doesn't block - if it can't get
55 // the lock it returns false, true if it can
56 [MethodImplAttribute(MethodImplOptions.InternalCall)]
57 private extern static bool Monitor_try_enter(object obj, int ms);
59 // Enter/Exit are implemented directly as icalls for performance reasons
61 // Acquires the mutex on object 'obj'
62 [MethodImplAttribute(MethodImplOptions.InternalCall)]
63 public extern static void Enter(object obj);
65 // Releases the mutex on object 'obj'
67 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
69 [MethodImplAttribute(MethodImplOptions.InternalCall)]
70 public extern static void Exit(object obj);
72 // Signals one of potentially many objects waiting on
74 [MethodImplAttribute(MethodImplOptions.InternalCall)]
75 private extern static void Monitor_pulse(object obj);
77 // Checks whether object 'obj' is currently synchronised
78 [MethodImplAttribute(MethodImplOptions.InternalCall)]
79 private extern static bool Monitor_test_synchronised(object obj);
81 public static void Pulse(object obj) {
83 throw new ArgumentNullException("obj");
85 if(Monitor_test_synchronised(obj)==false) {
86 throw new SynchronizationLockException("Object is not synchronized");
92 // Signals all of potentially many objects waiting on
94 [MethodImplAttribute(MethodImplOptions.InternalCall)]
95 private extern static void Monitor_pulse_all(object obj);
97 public static void PulseAll(object obj) {
99 throw new ArgumentNullException("obj");
101 if(Monitor_test_synchronised(obj)==false) {
102 throw new SynchronizationLockException("Object is not synchronized");
105 Monitor_pulse_all(obj);
108 public static bool TryEnter(object obj) {
110 throw new ArgumentNullException("obj");
112 //if(obj.GetType().IsValueType==true) {
113 // throw new ArgumentException("Value type");
116 return(Monitor_try_enter(obj, 0));
119 public static bool TryEnter(object obj, int millisecondsTimeout) {
121 throw new ArgumentNullException("obj");
123 //if(obj.GetType().IsValueType==true) {
124 // throw new ArgumentException("Value type");
127 // LAMESPEC: should throw an exception when ms<0, but
128 // Timeout.Infinite is -1
129 if(millisecondsTimeout == Timeout.Infinite) {
134 if(millisecondsTimeout<0) {
135 throw new ArgumentException("millisecondsTimeout", "negative value for millisecondsTimeout");
138 return(Monitor_try_enter(obj, millisecondsTimeout));
141 public static bool TryEnter(object obj, TimeSpan timeout) {
143 throw new ArgumentNullException("obj");
145 //if(obj.GetType().IsValueType==true) {
146 // throw new ArgumentException("Value type");
149 // LAMESPEC: should throw an exception when ms<0, but
150 // Timeout.Infinite is -1
151 int ms=Convert.ToInt32(timeout.TotalMilliseconds);
153 if(ms == Timeout.Infinite) {
158 if(ms < 0 || ms > Int32.MaxValue) {
159 throw new ArgumentOutOfRangeException("timeout", "timeout out of range");
162 return(Monitor_try_enter(obj, ms));
165 // Waits for a signal on object 'obj' with maximum
166 // wait time 'ms'. Returns true if the object was
167 // signalled, false if it timed out
168 [MethodImplAttribute(MethodImplOptions.InternalCall)]
169 private extern static bool Monitor_wait(object obj, int ms);
171 public static bool Wait(object obj) {
173 throw new ArgumentNullException("obj");
175 if(Monitor_test_synchronised(obj)==false) {
176 throw new SynchronizationLockException("Object is not synchronized");
179 return(Monitor_wait(obj, Timeout.Infinite));
182 public static bool Wait(object obj, int millisecondsTimeout) {
184 throw new ArgumentNullException("obj");
186 if(Monitor_test_synchronised(obj)==false) {
187 throw new SynchronizationLockException("Object is not synchronized");
189 if (millisecondsTimeout < 0 && millisecondsTimeout != Timeout.Infinite)
190 throw new ArgumentOutOfRangeException ("millisecondsTimeout", "timeout out of range");
192 return(Monitor_wait(obj, millisecondsTimeout));
195 public static bool Wait(object obj, TimeSpan timeout) {
197 throw new ArgumentNullException("obj");
199 // LAMESPEC: says to throw ArgumentException too
200 int ms=Convert.ToInt32(timeout.TotalMilliseconds);
202 if((ms < 0 && ms != Timeout.Infinite) || ms > Int32.MaxValue) {
203 throw new ArgumentOutOfRangeException("timeout", "timeout out of range");
205 if(Monitor_test_synchronised(obj)==false) {
206 throw new SynchronizationLockException("Object is not synchronized");
209 return(Monitor_wait(obj, ms));
212 public static bool Wait(object obj, int millisecondsTimeout, bool exitContext) {
214 if (exitContext) SynchronizationAttribute.ExitContext ();
215 return Wait (obj, millisecondsTimeout);
218 if (exitContext) SynchronizationAttribute.EnterContext ();
222 public static bool Wait(object obj, TimeSpan timeout, bool exitContext) {
224 if (exitContext) SynchronizationAttribute.ExitContext ();
225 return Wait (obj, timeout);
228 if (exitContext) SynchronizationAttribute.EnterContext ();