Monitor.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. using System.Runtime.Remoting.Contexts;
  33. using System.Runtime.ConstrainedExecution;
  34. using System.Runtime.InteropServices;
  35. namespace System.Threading
  36. {
  37. public static partial class Monitor
  38. {
  39. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  40. extern static bool Monitor_test_synchronised(object obj);
  41. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  42. extern static void Monitor_pulse(object obj);
  43. static void ObjPulse(Object obj)
  44. {
  45. if (!Monitor_test_synchronised (obj))
  46. throw new SynchronizationLockException("Object is not synchronized");
  47. Monitor_pulse (obj);
  48. }
  49. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  50. extern static void Monitor_pulse_all(object obj);
  51. static void ObjPulseAll(Object obj)
  52. {
  53. if (!Monitor_test_synchronised (obj))
  54. throw new SynchronizationLockException("Object is not synchronized");
  55. Monitor_pulse_all (obj);
  56. }
  57. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  58. extern static bool Monitor_wait(object obj, int ms);
  59. static bool ObjWait(bool exitContext, int millisecondsTimeout, Object obj)
  60. {
  61. if (millisecondsTimeout < 0 && millisecondsTimeout != (int) Timeout.Infinite)
  62. throw new ArgumentOutOfRangeException ("millisecondsTimeout");
  63. if (!Monitor_test_synchronised (obj))
  64. throw new SynchronizationLockException ("Object is not synchronized");
  65. try {
  66. #if !DISABLE_REMOTING
  67. if (exitContext)
  68. SynchronizationAttribute.ExitContext ();
  69. #endif
  70. return Monitor_wait (obj, millisecondsTimeout);
  71. } finally {
  72. #if !DISABLE_REMOTING
  73. if (exitContext)
  74. SynchronizationAttribute.EnterContext ();
  75. #endif
  76. }
  77. }
  78. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  79. extern static void try_enter_with_atomic_var (object obj, int millisecondsTimeout, ref bool lockTaken);
  80. static void ReliableEnterTimeout(Object obj, int timeout, ref bool lockTaken)
  81. {
  82. if (obj == null)
  83. throw new ArgumentNullException ("obj");
  84. if (timeout < 0 && timeout != (int) Timeout.Infinite)
  85. throw new ArgumentOutOfRangeException ("millisecondsTimeout");
  86. try_enter_with_atomic_var (obj, timeout, ref lockTaken);
  87. }
  88. static void ReliableEnter(Object obj, ref bool lockTaken)
  89. {
  90. ReliableEnterTimeout (obj, (int) Timeout.Infinite, ref lockTaken);
  91. }
  92. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  93. extern static bool Monitor_test_owner (object obj);
  94. static bool IsEnteredNative(Object obj)
  95. {
  96. return Monitor_test_owner (obj);
  97. }
  98. }
  99. }