WaitHandle.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #if MONOTOUCH
  48. if (exitContext)
  49. throw new NotSupportedException ("exitContext == true is not supported");
  50. #endif
  51. try {
  52. if (exitContext)
  53. SynchronizationAttribute.ExitContext ();
  54. if (WaitAll)
  55. return WaitAll_internal (waitHandles, millisecondsTimeout, exitContext);
  56. else
  57. return WaitAny_internal (waitHandles, millisecondsTimeout, exitContext);
  58. } finally {
  59. if (exitContext)
  60. SynchronizationAttribute.EnterContext ();
  61. }
  62. }
  63. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  64. private static extern int WaitAll_internal(WaitHandle[] handles, int ms, bool exitContext);
  65. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  66. private static extern int WaitAny_internal(WaitHandle[] handles, int ms, bool exitContext);
  67. static int WaitOneNative (SafeHandle waitableSafeHandle, uint millisecondsTimeout, bool hasThreadAffinity, bool exitContext)
  68. {
  69. #if MONOTOUCH
  70. if (exitContext)
  71. throw new NotSupportedException ("exitContext == true is not supported");
  72. #endif
  73. bool release = false;
  74. try {
  75. if (exitContext)
  76. SynchronizationAttribute.ExitContext ();
  77. waitableSafeHandle.DangerousAddRef (ref release);
  78. return WaitOne_internal (waitableSafeHandle.DangerousGetHandle (), (int) millisecondsTimeout, exitContext);
  79. } finally {
  80. if (release)
  81. waitableSafeHandle.DangerousRelease ();
  82. if (exitContext)
  83. SynchronizationAttribute.EnterContext ();
  84. }
  85. }
  86. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  87. static extern int WaitOne_internal(IntPtr handle, int ms, bool exitContext);
  88. static int SignalAndWaitOne (SafeWaitHandle waitHandleToSignal,SafeWaitHandle waitHandleToWaitOn, int millisecondsTimeout, bool hasThreadAffinity, bool exitContext)
  89. {
  90. bool releaseHandleToSignal = false, releaseHandleToWaitOn = false;
  91. try {
  92. waitHandleToSignal.DangerousAddRef (ref releaseHandleToSignal);
  93. waitHandleToWaitOn.DangerousAddRef (ref releaseHandleToWaitOn);
  94. return SignalAndWait_Internal (waitHandleToSignal.DangerousGetHandle (), waitHandleToWaitOn.DangerousGetHandle (), millisecondsTimeout, exitContext);
  95. } finally {
  96. if (releaseHandleToSignal)
  97. waitHandleToSignal.DangerousRelease ();
  98. if (releaseHandleToWaitOn)
  99. waitHandleToWaitOn.DangerousRelease ();
  100. }
  101. }
  102. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  103. static extern int SignalAndWait_Internal (IntPtr toSignal, IntPtr toWaitOn, int ms, bool exitContext);
  104. }
  105. }