* roottypes.cs: Rename from tree.cs.
[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 #endif
39
40 namespace System.Threading
41 {
42         public sealed class Monitor
43         {
44                 private Monitor () {}
45
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);
51
52                 public static void Enter(object obj) {
53                         if(obj==null) {
54                                 throw new ArgumentNullException("obj");
55                         }
56                         //if(obj.GetType().IsValueType==true) {
57                         //      throw new ArgumentException("Value type");
58                         //}
59
60                         Monitor_try_enter(obj, Timeout.Infinite);
61                 }
62
63                 // Releases the mutex on object 'obj'
64                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
65                 private extern static void Monitor_exit(object obj);
66
67 #if NET_2_0
68                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
69 #endif
70                 public static void Exit(object obj) {
71                         if(obj==null) {
72                                 throw new ArgumentNullException("obj");
73                         }
74                         //if(obj.GetType().IsValueType==true) {
75                         //      throw new ArgumentException("Value type");
76                         //}
77
78                         Monitor_exit(obj);
79                 }
80
81                 // Signals one of potentially many objects waiting on
82                 // object 'obj'
83                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
84                 private extern static void Monitor_pulse(object obj);
85
86                 // Checks whether object 'obj' is currently synchronised
87                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
88                 private extern static bool Monitor_test_synchronised(object obj);
89
90                 public static void Pulse(object obj) {
91                         if(obj==null) {
92                                 throw new ArgumentNullException("obj");
93                         }
94                         if(Monitor_test_synchronised(obj)==false) {
95                                 throw new SynchronizationLockException("Object is not synchronized");
96                         }
97
98                         Monitor_pulse(obj);
99                 }
100
101                 // Signals all of potentially many objects waiting on
102                 // object 'obj'
103                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
104                 private extern static void Monitor_pulse_all(object obj);
105
106                 public static void PulseAll(object obj) {
107                         if(obj==null) {
108                                 throw new ArgumentNullException("obj");
109                         }
110                         if(Monitor_test_synchronised(obj)==false) {
111                                 throw new SynchronizationLockException("Object is not synchronized");
112                         }
113
114                         Monitor_pulse_all(obj);
115                 }
116
117                 public static bool TryEnter(object obj) {
118                         if(obj==null) {
119                                 throw new ArgumentNullException("obj");
120                         }
121                         //if(obj.GetType().IsValueType==true) {
122                         //      throw new ArgumentException("Value type");
123                         //}
124                         
125                         return(Monitor_try_enter(obj, 0));
126                 }
127
128                 public static bool TryEnter(object obj, int millisecondsTimeout) {
129                         if(obj==null) {
130                                 throw new ArgumentNullException("obj");
131                         }
132                         //if(obj.GetType().IsValueType==true) {
133                         //      throw new ArgumentException("Value type");
134                         //}
135
136                         // LAMESPEC: should throw an exception when ms<0, but
137                         // Timeout.Infinite is -1
138                         if(millisecondsTimeout == Timeout.Infinite) {
139                                 Enter(obj);
140                                 return(true);
141                         }
142                         
143                         if(millisecondsTimeout<0) {
144                                 throw new ArgumentException("millisecondsTimeout", "negative value for millisecondsTimeout");
145                         }
146                         
147                         return(Monitor_try_enter(obj, millisecondsTimeout));
148                 }
149
150                 public static bool TryEnter(object obj, TimeSpan timeout) {
151                         if(obj==null) {
152                                 throw new ArgumentNullException("obj");
153                         }
154                         //if(obj.GetType().IsValueType==true) {
155                         //      throw new ArgumentException("Value type");
156                         //}
157
158                         // LAMESPEC: should throw an exception when ms<0, but
159                         // Timeout.Infinite is -1
160                         int ms=Convert.ToInt32(timeout.TotalMilliseconds);
161                         
162                         if(ms == Timeout.Infinite) {
163                                 Enter(obj);
164                                 return(true);
165                         }
166
167                         if(ms < 0 || ms > Int32.MaxValue) {
168                                 throw new ArgumentOutOfRangeException("timeout", "timeout out of range");
169                         }
170                         
171                         return(Monitor_try_enter(obj, ms));
172                 }
173
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);
179
180                 public static bool Wait(object obj) {
181                         if(obj==null) {
182                                 throw new ArgumentNullException("obj");
183                         }
184                         if(Monitor_test_synchronised(obj)==false) {
185                                 throw new SynchronizationLockException("Object is not synchronized");
186                         }
187
188                         return(Monitor_wait(obj, Timeout.Infinite));
189                 }
190
191                 public static bool Wait(object obj, int millisecondsTimeout) {
192                         if(obj==null) {
193                                 throw new ArgumentNullException("obj");
194                         }
195                         if(Monitor_test_synchronised(obj)==false) {
196                                 throw new SynchronizationLockException("Object is not synchronized");
197                         }
198                         if (millisecondsTimeout < 0 && millisecondsTimeout != Timeout.Infinite)
199                                 throw new ArgumentOutOfRangeException ("millisecondsTimeout", "timeout out of range");
200
201                         return(Monitor_wait(obj, millisecondsTimeout));
202                 }
203
204                 public static bool Wait(object obj, TimeSpan timeout) {
205                         if(obj==null) {
206                                 throw new ArgumentNullException("obj");
207                         }
208                         // LAMESPEC: says to throw ArgumentException too
209                         int ms=Convert.ToInt32(timeout.TotalMilliseconds);
210                         
211                         if((ms < 0 && ms != Timeout.Infinite) || ms > Int32.MaxValue) {
212                                 throw new ArgumentOutOfRangeException("timeout", "timeout out of range");
213                         }
214                         if(Monitor_test_synchronised(obj)==false) {
215                                 throw new SynchronizationLockException("Object is not synchronized");
216                         }
217
218                         return(Monitor_wait(obj, ms));
219                 }
220
221                 public static bool Wait(object obj, int millisecondsTimeout, bool exitContext) {
222                         try {
223                                 if (exitContext) SynchronizationAttribute.ExitContext ();
224                                 return Wait (obj, millisecondsTimeout);
225                         }
226                         finally {
227                                 if (exitContext) SynchronizationAttribute.EnterContext ();
228                         }
229                 }
230
231                 public static bool Wait(object obj, TimeSpan timeout, bool exitContext) {
232                         try {
233                                 if (exitContext) SynchronizationAttribute.ExitContext ();
234                                 return Wait (obj, timeout);
235                         }
236                         finally {
237                                 if (exitContext) SynchronizationAttribute.EnterContext ();
238                         }
239                 }
240         }
241 }