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
;
35 using System
.Runtime
.Remoting
.Contexts
;
37 using System
.Runtime
.ConstrainedExecution
;
38 using System
.Runtime
.InteropServices
;
40 namespace System
.Threading
42 public static partial class Monitor
44 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
45 extern static bool Monitor_test_synchronised(object obj
);
47 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
48 extern static void Monitor_pulse(object obj
);
50 static void ObjPulse(Object obj
)
52 if (!Monitor_test_synchronised (obj
))
53 throw new SynchronizationLockException("Object is not synchronized");
58 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
59 extern static void Monitor_pulse_all(object obj
);
61 static void ObjPulseAll(Object obj
)
63 if (!Monitor_test_synchronised (obj
))
64 throw new SynchronizationLockException("Object is not synchronized");
66 Monitor_pulse_all (obj
);
69 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
70 extern static bool Monitor_wait(object obj
, int ms
);
72 static bool ObjWait(bool exitContext
, int millisecondsTimeout
, Object obj
)
74 if (millisecondsTimeout
< 0 && millisecondsTimeout
!= (int) Timeout
.Infinite
)
75 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
76 if (!Monitor_test_synchronised (obj
))
77 throw new SynchronizationLockException ("Object is not synchronized");
82 SynchronizationAttribute
.ExitContext ();
85 return Monitor_wait (obj
, millisecondsTimeout
);
89 SynchronizationAttribute
.EnterContext ();
94 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
95 extern static void try_enter_with_atomic_var (object obj
, int millisecondsTimeout
, ref bool lockTaken
);
97 static void ReliableEnterTimeout(Object obj
, int timeout
, ref bool lockTaken
)
100 throw new ArgumentNullException ("obj");
101 if (timeout
< 0 && timeout
!= (int) Timeout
.Infinite
)
102 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
104 try_enter_with_atomic_var (obj
, timeout
, ref lockTaken
);
107 static void ReliableEnter(Object obj
, ref bool lockTaken
)
109 ReliableEnterTimeout (obj
, (int) Timeout
.Infinite
, ref lockTaken
);
112 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
113 extern static bool Monitor_test_owner (object obj
);
115 static bool IsEnteredNative(Object obj
)
117 return Monitor_test_owner (obj
);
121 public static long LockContentionCount
=> throw new PlatformNotSupportedException ();