New error message.
[mono.git] / mcs / class / corlib / System.Threading / Monitor.cs
1 //
2 // System.Threading.Monitor.cs
3 //
4 // Author:
5 //   Dick Porter (dick@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 using System.Runtime.CompilerServices;
34 using System.Runtime.Remoting.Contexts;
35
36 #if NET_2_0
37 using System.Runtime.ConstrainedExecution;
38 using System.Runtime.InteropServices;
39 #endif
40
41 namespace System.Threading
42 {
43 #if NET_2_0
44         [ComVisible (true)]
45         public static class Monitor
46         {
47 #else
48         public sealed class Monitor
49         {
50                 private Monitor () {}
51 #endif
52
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);
58
59                 // Enter/Exit are implemented directly as icalls for performance reasons
60
61                 // Acquires the mutex on object 'obj'
62                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
63                 public extern static void Enter(object obj);
64
65                 // Releases the mutex on object 'obj'
66 #if NET_2_0
67                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
68 #endif
69                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
70                 public extern static void Exit(object obj);
71
72                 // Signals one of potentially many objects waiting on
73                 // object 'obj'
74                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
75                 private extern static void Monitor_pulse(object obj);
76
77                 // Checks whether object 'obj' is currently synchronised
78                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
79                 private extern static bool Monitor_test_synchronised(object obj);
80
81                 public static void Pulse(object obj) {
82                         if(obj==null) {
83                                 throw new ArgumentNullException("obj");
84                         }
85                         if(Monitor_test_synchronised(obj)==false) {
86                                 throw new SynchronizationLockException("Object is not synchronized");
87                         }
88
89                         Monitor_pulse(obj);
90                 }
91
92                 // Signals all of potentially many objects waiting on
93                 // object 'obj'
94                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
95                 private extern static void Monitor_pulse_all(object obj);
96
97                 public static void PulseAll(object obj) {
98                         if(obj==null) {
99                                 throw new ArgumentNullException("obj");
100                         }
101                         if(Monitor_test_synchronised(obj)==false) {
102                                 throw new SynchronizationLockException("Object is not synchronized");
103                         }
104
105                         Monitor_pulse_all(obj);
106                 }
107
108                 public static bool TryEnter(object obj) {
109                         if(obj==null) {
110                                 throw new ArgumentNullException("obj");
111                         }
112                         //if(obj.GetType().IsValueType==true) {
113                         //      throw new ArgumentException("Value type");
114                         //}
115                         
116                         return(Monitor_try_enter(obj, 0));
117                 }
118
119                 public static bool TryEnter(object obj, int millisecondsTimeout) {
120                         if(obj==null) {
121                                 throw new ArgumentNullException("obj");
122                         }
123                         //if(obj.GetType().IsValueType==true) {
124                         //      throw new ArgumentException("Value type");
125                         //}
126
127                         // LAMESPEC: should throw an exception when ms<0, but
128                         // Timeout.Infinite is -1
129                         if(millisecondsTimeout == Timeout.Infinite) {
130                                 Enter(obj);
131                                 return(true);
132                         }
133                         
134                         if(millisecondsTimeout<0) {
135                                 throw new ArgumentException("millisecondsTimeout", "negative value for millisecondsTimeout");
136                         }
137                         
138                         return(Monitor_try_enter(obj, millisecondsTimeout));
139                 }
140
141                 public static bool TryEnter(object obj, TimeSpan timeout) {
142                         if(obj==null) {
143                                 throw new ArgumentNullException("obj");
144                         }
145                         //if(obj.GetType().IsValueType==true) {
146                         //      throw new ArgumentException("Value type");
147                         //}
148
149                         // LAMESPEC: should throw an exception when ms<0, but
150                         // Timeout.Infinite is -1
151                         int ms=Convert.ToInt32(timeout.TotalMilliseconds);
152                         
153                         if(ms == Timeout.Infinite) {
154                                 Enter(obj);
155                                 return(true);
156                         }
157
158                         if(ms < 0 || ms > Int32.MaxValue) {
159                                 throw new ArgumentOutOfRangeException("timeout", "timeout out of range");
160                         }
161                         
162                         return(Monitor_try_enter(obj, ms));
163                 }
164
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);
170
171                 public static bool Wait(object obj) {
172                         if(obj==null) {
173                                 throw new ArgumentNullException("obj");
174                         }
175                         if(Monitor_test_synchronised(obj)==false) {
176                                 throw new SynchronizationLockException("Object is not synchronized");
177                         }
178
179                         return(Monitor_wait(obj, Timeout.Infinite));
180                 }
181
182                 public static bool Wait(object obj, int millisecondsTimeout) {
183                         if(obj==null) {
184                                 throw new ArgumentNullException("obj");
185                         }
186                         if(Monitor_test_synchronised(obj)==false) {
187                                 throw new SynchronizationLockException("Object is not synchronized");
188                         }
189                         if (millisecondsTimeout < 0 && millisecondsTimeout != Timeout.Infinite)
190                                 throw new ArgumentOutOfRangeException ("millisecondsTimeout", "timeout out of range");
191
192                         return(Monitor_wait(obj, millisecondsTimeout));
193                 }
194
195                 public static bool Wait(object obj, TimeSpan timeout) {
196                         if(obj==null) {
197                                 throw new ArgumentNullException("obj");
198                         }
199                         // LAMESPEC: says to throw ArgumentException too
200                         int ms=Convert.ToInt32(timeout.TotalMilliseconds);
201                         
202                         if((ms < 0 && ms != Timeout.Infinite) || ms > Int32.MaxValue) {
203                                 throw new ArgumentOutOfRangeException("timeout", "timeout out of range");
204                         }
205                         if(Monitor_test_synchronised(obj)==false) {
206                                 throw new SynchronizationLockException("Object is not synchronized");
207                         }
208
209                         return(Monitor_wait(obj, ms));
210                 }
211
212                 public static bool Wait(object obj, int millisecondsTimeout, bool exitContext) {
213                         try {
214                                 if (exitContext) SynchronizationAttribute.ExitContext ();
215                                 return Wait (obj, millisecondsTimeout);
216                         }
217                         finally {
218                                 if (exitContext) SynchronizationAttribute.EnterContext ();
219                         }
220                 }
221
222                 public static bool Wait(object obj, TimeSpan timeout, bool exitContext) {
223                         try {
224                                 if (exitContext) SynchronizationAttribute.ExitContext ();
225                                 return Wait (obj, timeout);
226                         }
227                         finally {
228                                 if (exitContext) SynchronizationAttribute.EnterContext ();
229                         }
230                 }
231         }
232 }