WaitHandle.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // System.Threading.WaitHandle.cs
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected]
  7. //
  8. // (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
  9. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Reflection;
  32. using System.Runtime.CompilerServices;
  33. using System.Runtime.Remoting.Contexts;
  34. using System.Security.Permissions;
  35. using System.Runtime.InteropServices;
  36. using Microsoft.Win32.SafeHandles;
  37. using System.Runtime.ConstrainedExecution;
  38. namespace System.Threading
  39. {
  40. [StructLayout (LayoutKind.Sequential)]
  41. public abstract partial class WaitHandle
  42. : MarshalByRefObject, IDisposable
  43. {
  44. protected static readonly IntPtr InvalidHandle = (IntPtr) (-1);
  45. internal const int MaxWaitHandles = 64;
  46. static int WaitMultiple(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext, bool WaitAll)
  47. {
  48. int release_last = -1;
  49. try {
  50. #if !DISABLE_REMOTING
  51. if (exitContext)
  52. SynchronizationAttribute.ExitContext ();
  53. #endif
  54. for (int i = 0; i < waitHandles.Length; ++i) {
  55. try {
  56. } finally {
  57. /* we have to put it in a finally block, to avoid having a ThreadAbortException
  58. * between the return from DangerousAddRef and the assignement to release_last */
  59. bool release = false;
  60. waitHandles [i].SafeWaitHandle.DangerousAddRef (ref release);
  61. release_last = i;
  62. }
  63. }
  64. if (WaitAll)
  65. return WaitAll_internal (waitHandles, millisecondsTimeout);
  66. else
  67. return WaitAny_internal (waitHandles, millisecondsTimeout);
  68. } finally {
  69. for (int i = release_last; i >= 0; --i) {
  70. waitHandles [i].SafeWaitHandle.DangerousRelease ();
  71. }
  72. #if !DISABLE_REMOTING
  73. if (exitContext)
  74. SynchronizationAttribute.EnterContext ();
  75. #endif
  76. }
  77. }
  78. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  79. private static extern int WaitAll_internal(WaitHandle[] handles, int ms);
  80. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  81. private static extern int WaitAny_internal(WaitHandle[] handles, int ms);
  82. static int WaitOneNative (SafeHandle waitableSafeHandle, uint millisecondsTimeout, bool hasThreadAffinity, bool exitContext)
  83. {
  84. bool release = false;
  85. try {
  86. #if !DISABLE_REMOTING
  87. if (exitContext)
  88. SynchronizationAttribute.ExitContext ();
  89. #endif
  90. waitableSafeHandle.DangerousAddRef (ref release);
  91. return WaitOne_internal (waitableSafeHandle.DangerousGetHandle (), (int) millisecondsTimeout);
  92. } finally {
  93. if (release)
  94. waitableSafeHandle.DangerousRelease ();
  95. #if !DISABLE_REMOTING
  96. if (exitContext)
  97. SynchronizationAttribute.EnterContext ();
  98. #endif
  99. }
  100. }
  101. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  102. static extern int WaitOne_internal(IntPtr handle, int ms);
  103. static int SignalAndWaitOne (SafeWaitHandle waitHandleToSignal,SafeWaitHandle waitHandleToWaitOn, int millisecondsTimeout, bool hasThreadAffinity, bool exitContext)
  104. {
  105. bool releaseHandleToSignal = false, releaseHandleToWaitOn = false;
  106. try {
  107. waitHandleToSignal.DangerousAddRef (ref releaseHandleToSignal);
  108. waitHandleToWaitOn.DangerousAddRef (ref releaseHandleToWaitOn);
  109. return SignalAndWait_Internal (waitHandleToSignal.DangerousGetHandle (), waitHandleToWaitOn.DangerousGetHandle (), millisecondsTimeout);
  110. } finally {
  111. if (releaseHandleToSignal)
  112. waitHandleToSignal.DangerousRelease ();
  113. if (releaseHandleToWaitOn)
  114. waitHandleToWaitOn.DangerousRelease ();
  115. }
  116. }
  117. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  118. static extern int SignalAndWait_Internal (IntPtr toSignal, IntPtr toWaitOn, int ms);
  119. }
  120. }