WaitHandle.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. static int WaitMultiple(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext, bool WaitAll)
  46. {
  47. int release_last = -1;
  48. try {
  49. #if !DISABLE_REMOTING
  50. if (exitContext)
  51. SynchronizationAttribute.ExitContext ();
  52. #endif
  53. for (int i = 0; i < waitHandles.Length; ++i) {
  54. try {
  55. } finally {
  56. /* we have to put it in a finally block, to avoid having a ThreadAbortException
  57. * between the return from DangerousAddRef and the assignement to release_last */
  58. bool release = false;
  59. waitHandles [i].SafeWaitHandle.DangerousAddRef (ref release);
  60. release_last = i;
  61. }
  62. }
  63. if (WaitAll)
  64. return WaitAll_internal (waitHandles, millisecondsTimeout);
  65. else
  66. return WaitAny_internal (waitHandles, millisecondsTimeout);
  67. } finally {
  68. for (int i = release_last; i >= 0; --i) {
  69. waitHandles [i].SafeWaitHandle.DangerousRelease ();
  70. }
  71. #if !DISABLE_REMOTING
  72. if (exitContext)
  73. SynchronizationAttribute.EnterContext ();
  74. #endif
  75. }
  76. }
  77. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  78. private static extern int WaitAll_internal(WaitHandle[] handles, int ms);
  79. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  80. private static extern int WaitAny_internal(WaitHandle[] handles, int ms);
  81. static int WaitOneNative (SafeHandle waitableSafeHandle, uint millisecondsTimeout, bool hasThreadAffinity, bool exitContext)
  82. {
  83. bool release = false;
  84. try {
  85. #if !DISABLE_REMOTING
  86. if (exitContext)
  87. SynchronizationAttribute.ExitContext ();
  88. #endif
  89. waitableSafeHandle.DangerousAddRef (ref release);
  90. return WaitOne_internal (waitableSafeHandle.DangerousGetHandle (), (int) millisecondsTimeout);
  91. } finally {
  92. if (release)
  93. waitableSafeHandle.DangerousRelease ();
  94. #if !DISABLE_REMOTING
  95. if (exitContext)
  96. SynchronizationAttribute.EnterContext ();
  97. #endif
  98. }
  99. }
  100. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  101. static extern int WaitOne_internal(IntPtr handle, int ms);
  102. static int SignalAndWaitOne (SafeWaitHandle waitHandleToSignal,SafeWaitHandle waitHandleToWaitOn, int millisecondsTimeout, bool hasThreadAffinity, bool exitContext)
  103. {
  104. bool releaseHandleToSignal = false, releaseHandleToWaitOn = false;
  105. try {
  106. waitHandleToSignal.DangerousAddRef (ref releaseHandleToSignal);
  107. waitHandleToWaitOn.DangerousAddRef (ref releaseHandleToWaitOn);
  108. return SignalAndWait_Internal (waitHandleToSignal.DangerousGetHandle (), waitHandleToWaitOn.DangerousGetHandle (), millisecondsTimeout);
  109. } finally {
  110. if (releaseHandleToSignal)
  111. waitHandleToSignal.DangerousRelease ();
  112. if (releaseHandleToWaitOn)
  113. waitHandleToWaitOn.DangerousRelease ();
  114. }
  115. }
  116. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  117. static extern int SignalAndWait_Internal (IntPtr toSignal, IntPtr toWaitOn, int ms);
  118. }
  119. }