Monitor.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // System.Threading.Monitor.cs
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. //
  7. // (C) Ximian, Inc. http://www.ximian.com
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Runtime.CompilerServices;
  32. #if !DISABLE_REMOTING
  33. using System.Runtime.Remoting.Contexts;
  34. #endif
  35. using System.Runtime.ConstrainedExecution;
  36. using System.Runtime.InteropServices;
  37. namespace System.Threading
  38. {
  39. public static partial class Monitor
  40. {
  41. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  42. extern static bool Monitor_test_synchronised(object obj);
  43. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  44. extern static void Monitor_pulse(object obj);
  45. static void ObjPulse(Object obj)
  46. {
  47. if (!Monitor_test_synchronised (obj))
  48. throw new SynchronizationLockException("Object is not synchronized");
  49. Monitor_pulse (obj);
  50. }
  51. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  52. extern static void Monitor_pulse_all(object obj);
  53. static void ObjPulseAll(Object obj)
  54. {
  55. if (!Monitor_test_synchronised (obj))
  56. throw new SynchronizationLockException("Object is not synchronized");
  57. Monitor_pulse_all (obj);
  58. }
  59. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  60. extern static bool Monitor_wait(object obj, int ms);
  61. static bool ObjWait(bool exitContext, int millisecondsTimeout, Object obj)
  62. {
  63. if (millisecondsTimeout < 0 && millisecondsTimeout != (int) Timeout.Infinite)
  64. throw new ArgumentOutOfRangeException ("millisecondsTimeout");
  65. if (!Monitor_test_synchronised (obj))
  66. throw new SynchronizationLockException ("Object is not synchronized");
  67. try {
  68. #if !DISABLE_REMOTING
  69. if (exitContext)
  70. SynchronizationAttribute.ExitContext ();
  71. #endif
  72. return Monitor_wait (obj, millisecondsTimeout);
  73. } finally {
  74. #if !DISABLE_REMOTING
  75. if (exitContext)
  76. SynchronizationAttribute.EnterContext ();
  77. #endif
  78. }
  79. }
  80. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  81. extern static void try_enter_with_atomic_var (object obj, int millisecondsTimeout, ref bool lockTaken);
  82. static void ReliableEnterTimeout(Object obj, int timeout, ref bool lockTaken)
  83. {
  84. if (obj == null)
  85. throw new ArgumentNullException ("obj");
  86. if (timeout < 0 && timeout != (int) Timeout.Infinite)
  87. throw new ArgumentOutOfRangeException ("millisecondsTimeout");
  88. try_enter_with_atomic_var (obj, timeout, ref lockTaken);
  89. }
  90. static void ReliableEnter(Object obj, ref bool lockTaken)
  91. {
  92. ReliableEnterTimeout (obj, (int) Timeout.Infinite, ref lockTaken);
  93. }
  94. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  95. extern static bool Monitor_test_owner (object obj);
  96. static bool IsEnteredNative(Object obj)
  97. {
  98. return Monitor_test_owner (obj);
  99. }
  100. }
  101. }