mscorlib(4.0.0.0) API with additions
Monitor.cs
4 using System.Security;
6 
7 namespace System.Threading
8 {
10  [ComVisible(true)]
11  [__DynamicallyInvokable]
12  [HostProtection(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
13  public static class Monitor
14  {
18  [MethodImpl(MethodImplOptions.InternalCall)]
19  [SecuritySafeCritical]
20  [__DynamicallyInvokable]
21  public static extern void Enter(object obj);
22 
28  [__DynamicallyInvokable]
29  public static void Enter(object obj, ref bool lockTaken)
30  {
31  if (lockTaken)
32  {
33  ThrowLockTakenException();
34  }
35  ReliableEnter(obj, ref lockTaken);
36  }
37 
38  private static void ThrowLockTakenException()
39  {
40  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeFalse"), "lockTaken");
41  }
42 
43  [MethodImpl(MethodImplOptions.InternalCall)]
44  [SecuritySafeCritical]
45  private static extern void ReliableEnter(object obj, ref bool lockTaken);
46 
51  [MethodImpl(MethodImplOptions.InternalCall)]
52  [SecuritySafeCritical]
53  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
54  [__DynamicallyInvokable]
55  public static extern void Exit(object obj);
56 
62  [__DynamicallyInvokable]
63  public static bool TryEnter(object obj)
64  {
65  bool lockTaken = false;
66  TryEnter(obj, 0, ref lockTaken);
67  return lockTaken;
68  }
69 
75  [__DynamicallyInvokable]
76  public static void TryEnter(object obj, ref bool lockTaken)
77  {
78  if (lockTaken)
79  {
80  ThrowLockTakenException();
81  }
82  ReliableEnterTimeout(obj, 0, ref lockTaken);
83  }
84 
93  [__DynamicallyInvokable]
94  public static bool TryEnter(object obj, int millisecondsTimeout)
95  {
96  bool lockTaken = false;
97  TryEnter(obj, millisecondsTimeout, ref lockTaken);
98  return lockTaken;
99  }
100 
101  private static int MillisecondsTimeoutFromTimeSpan(TimeSpan timeout)
102  {
103  long num = (long)timeout.TotalMilliseconds;
104  if (num < -1 || num > int.MaxValue)
105  {
106  throw new ArgumentOutOfRangeException("timeout", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
107  }
108  return (int)num;
109  }
110 
118  [__DynamicallyInvokable]
119  public static bool TryEnter(object obj, TimeSpan timeout)
120  {
121  return TryEnter(obj, MillisecondsTimeoutFromTimeSpan(timeout));
122  }
123 
132  [__DynamicallyInvokable]
133  public static void TryEnter(object obj, int millisecondsTimeout, ref bool lockTaken)
134  {
135  if (lockTaken)
136  {
137  ThrowLockTakenException();
138  }
139  ReliableEnterTimeout(obj, millisecondsTimeout, ref lockTaken);
140  }
141 
149  [__DynamicallyInvokable]
150  public static void TryEnter(object obj, TimeSpan timeout, ref bool lockTaken)
151  {
152  if (lockTaken)
153  {
154  ThrowLockTakenException();
155  }
156  ReliableEnterTimeout(obj, MillisecondsTimeoutFromTimeSpan(timeout), ref lockTaken);
157  }
158 
159  [MethodImpl(MethodImplOptions.InternalCall)]
160  [SecuritySafeCritical]
161  private static extern void ReliableEnterTimeout(object obj, int timeout, ref bool lockTaken);
162 
169  [SecuritySafeCritical]
170  [__DynamicallyInvokable]
171  public static bool IsEntered(object obj)
172  {
173  if (obj == null)
174  {
175  throw new ArgumentNullException("obj");
176  }
177  return IsEnteredNative(obj);
178  }
179 
180  [MethodImpl(MethodImplOptions.InternalCall)]
181  [SecurityCritical]
182  private static extern bool IsEnteredNative(object obj);
183 
184  [MethodImpl(MethodImplOptions.InternalCall)]
185  [SecurityCritical]
186  private static extern bool ObjWait(bool exitContext, int millisecondsTimeout, object obj);
187 
200  [SecuritySafeCritical]
201  public static bool Wait(object obj, int millisecondsTimeout, bool exitContext)
202  {
203  if (obj == null)
204  {
205  throw new ArgumentNullException("obj");
206  }
207  return ObjWait(exitContext, millisecondsTimeout, obj);
208  }
209 
222  public static bool Wait(object obj, TimeSpan timeout, bool exitContext)
223  {
224  return Wait(obj, MillisecondsTimeoutFromTimeSpan(timeout), exitContext);
225  }
226 
236  [__DynamicallyInvokable]
237  public static bool Wait(object obj, int millisecondsTimeout)
238  {
239  return Wait(obj, millisecondsTimeout, exitContext: false);
240  }
241 
251  [__DynamicallyInvokable]
252  public static bool Wait(object obj, TimeSpan timeout)
253  {
254  return Wait(obj, MillisecondsTimeoutFromTimeSpan(timeout), exitContext: false);
255  }
256 
264  [__DynamicallyInvokable]
265  public static bool Wait(object obj)
266  {
267  return Wait(obj, -1, exitContext: false);
268  }
269 
270  [MethodImpl(MethodImplOptions.InternalCall)]
271  [SecurityCritical]
272  private static extern void ObjPulse(object obj);
273 
278  [SecuritySafeCritical]
279  [__DynamicallyInvokable]
280  public static void Pulse(object obj)
281  {
282  if (obj == null)
283  {
284  throw new ArgumentNullException("obj");
285  }
286  ObjPulse(obj);
287  }
288 
289  [MethodImpl(MethodImplOptions.InternalCall)]
290  [SecurityCritical]
291  private static extern void ObjPulseAll(object obj);
292 
297  [SecuritySafeCritical]
298  [__DynamicallyInvokable]
299  public static void PulseAll(object obj)
300  {
301  if (obj == null)
302  {
303  throw new ArgumentNullException("obj");
304  }
305  ObjPulseAll(obj);
306  }
307  }
308 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static void TryEnter(object obj, TimeSpan timeout, ref bool lockTaken)
Attempts, for the specified amount of time, to acquire an exclusive lock on the specified object,...
Definition: Monitor.cs:150
static bool Wait(object obj)
Releases the lock on an object and blocks the current thread until it reacquires the lock.
Definition: Monitor.cs:265
static void PulseAll(object obj)
Notifies all waiting threads of a change in the object's state.
Definition: Monitor.cs:299
static bool TryEnter(object obj)
Attempts to acquire an exclusive lock on the specified object.
Definition: Monitor.cs:63
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
Provides a mechanism that synchronizes access to objects.
Definition: Monitor.cs:13
static bool Wait(object obj, int millisecondsTimeout, bool exitContext)
Releases the lock on an object and blocks the current thread until it reacquires the lock....
Definition: Monitor.cs:201
double TotalMilliseconds
Gets the value of the current T:System.TimeSpan structure expressed in whole and fractional milliseco...
Definition: TimeSpan.cs:180
static bool Wait(object obj, TimeSpan timeout)
Releases the lock on an object and blocks the current thread until it reacquires the lock....
Definition: Monitor.cs:252
static bool TryEnter(object obj, TimeSpan timeout)
Attempts, for the specified amount of time, to acquire an exclusive lock on the specified object.
Definition: Monitor.cs:119
Cer
Specifies a method's behavior when called within a constrained execution region.
Definition: Cer.cs:5
SecurityAction
Specifies the security actions that can be performed using declarative security.
static void Enter(object obj)
Acquires an exclusive lock on the specified object.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static bool IsEntered(object obj)
Determines whether the current thread holds the lock on the specified object.
Definition: Monitor.cs:171
static void Enter(object obj, ref bool lockTaken)
Acquires an exclusive lock on the specified object, and atomically sets a value that indicates whethe...
Definition: Monitor.cs:29
static void Exit(object obj)
Releases an exclusive lock on the specified object.
static bool Wait(object obj, TimeSpan timeout, bool exitContext)
Releases the lock on an object and blocks the current thread until it reacquires the lock....
Definition: Monitor.cs:222
static bool Wait(object obj, int millisecondsTimeout)
Releases the lock on an object and blocks the current thread until it reacquires the lock....
Definition: Monitor.cs:237
MethodImplOptions
Defines the details of how a method is implemented.
static void TryEnter(object obj, ref bool lockTaken)
Attempts to acquire an exclusive lock on the specified object, and atomically sets a value that indic...
Definition: Monitor.cs:76
The exception that is thrown when one of the arguments provided to a method is not valid.
static void Pulse(object obj)
Notifies a thread in the waiting queue of a change in the locked object's state.
Definition: Monitor.cs:280
Represents a time interval.To browse the .NET Framework source code for this type,...
Definition: TimeSpan.cs:12
Consistency
Specifies a reliability contract.
Definition: Consistency.cs:5
static void TryEnter(object obj, int millisecondsTimeout, ref bool lockTaken)
Attempts, for the specified number of milliseconds, to acquire an exclusive lock on the specified obj...
Definition: Monitor.cs:133
static bool TryEnter(object obj, int millisecondsTimeout)
Attempts, for the specified number of milliseconds, to acquire an exclusive lock on the specified obj...
Definition: Monitor.cs:94