WaitHandle.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // System.Threading.WaitHandle.cs
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. //
  7. // (C) Ximian, Inc. http://www.ximian.com
  8. //
  9. using System.Runtime.CompilerServices;
  10. namespace System.Threading
  11. {
  12. public abstract class WaitHandle : MarshalByRefObject, IDisposable
  13. {
  14. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  15. private static extern bool WaitAll_internal(WaitHandle[] handles, int ms, bool exitContext);
  16. public static bool WaitAll(WaitHandle[] waitHandles) {
  17. if(waitHandles.Length>64) {
  18. throw new NotSupportedException("Too many handles");
  19. }
  20. for(int i=0; i<waitHandles.Length; i++) {
  21. if(waitHandles[i]==null) {
  22. throw new ArgumentNullException("null handle");
  23. }
  24. }
  25. return(WaitAll_internal(waitHandles, 0, false));
  26. }
  27. public static bool WaitAll(WaitHandle[] waitHandles,
  28. int millisecondsTimeout,
  29. bool exitContext) {
  30. if(waitHandles.Length>64) {
  31. throw new NotSupportedException("Too many handles");
  32. }
  33. for(int i=0; i<waitHandles.Length; i++) {
  34. if(waitHandles[i]==null) {
  35. throw new ArgumentNullException("null handle");
  36. }
  37. }
  38. return(WaitAll_internal(waitHandles, millisecondsTimeout, false));
  39. }
  40. public static bool WaitAll(WaitHandle[] waitHandles,
  41. TimeSpan timeout,
  42. bool exitContext) {
  43. if(timeout.Milliseconds < 0 ||
  44. timeout.Milliseconds > Int32.MaxValue) {
  45. throw new ArgumentOutOfRangeException("Timeout out of range");
  46. }
  47. if(waitHandles.Length>64) {
  48. throw new NotSupportedException("Too many handles");
  49. }
  50. for(int i=0; i<waitHandles.Length; i++) {
  51. if(waitHandles[i]==null) {
  52. throw new ArgumentNullException("null handle");
  53. }
  54. }
  55. return(WaitAll_internal(waitHandles, timeout.Milliseconds, exitContext));
  56. }
  57. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  58. private static extern int WaitAny_internal(WaitHandle[] handles, int ms, bool exitContext);
  59. // LAMESPEC: Doesn't specify how to signal failures
  60. public static int WaitAny(WaitHandle[] waitHandles) {
  61. if(waitHandles.Length>64) {
  62. throw new NotSupportedException("Too many handles");
  63. }
  64. for(int i=0; i<waitHandles.Length; i++) {
  65. if(waitHandles[i]==null) {
  66. throw new ArgumentNullException("null handle");
  67. }
  68. }
  69. return(WaitAny_internal(waitHandles, 0, false));
  70. }
  71. public static int WaitAny(WaitHandle[] waitHandles,
  72. int millisecondsTimeout,
  73. bool exitContext) {
  74. if(waitHandles.Length>64) {
  75. throw new NotSupportedException("Too many handles");
  76. }
  77. for(int i=0; i<waitHandles.Length; i++) {
  78. if(waitHandles[i]==null) {
  79. throw new ArgumentNullException("null handle");
  80. }
  81. }
  82. return(WaitAny_internal(waitHandles, millisecondsTimeout, exitContext));
  83. }
  84. public static int WaitAny(WaitHandle[] waitHandles,
  85. TimeSpan timeout, bool exitContext) {
  86. if(timeout.Milliseconds < 0 ||
  87. timeout.Milliseconds > Int32.MaxValue) {
  88. throw new ArgumentOutOfRangeException("Timeout out of range");
  89. }
  90. if(waitHandles.Length>64) {
  91. throw new NotSupportedException("Too many handles");
  92. }
  93. for(int i=0; i<waitHandles.Length; i++) {
  94. if(waitHandles[i]==null) {
  95. throw new ArgumentNullException("null handle");
  96. }
  97. }
  98. return(WaitAny_internal(waitHandles, timeout.Milliseconds, exitContext));
  99. }
  100. [MonoTODO]
  101. public WaitHandle() {
  102. // FIXME
  103. }
  104. public const int WaitTimeout = 258;
  105. private IntPtr handle = IntPtr.Zero;
  106. public virtual IntPtr Handle {
  107. get {
  108. return(handle);
  109. }
  110. set {
  111. handle=value;
  112. }
  113. }
  114. public virtual void Close() {
  115. Dispose(false);
  116. }
  117. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  118. protected virtual extern bool WaitOne_internal(IntPtr handle, int ms, bool exitContext);
  119. public virtual bool WaitOne() {
  120. return(WaitOne_internal(handle, 0, false));
  121. }
  122. public virtual bool WaitOne(int millisecondsTimeout, bool exitContext) {
  123. return(WaitOne_internal(handle, millisecondsTimeout, exitContext));
  124. }
  125. public virtual bool WaitOne(TimeSpan timeout, bool exitContext) {
  126. return(WaitOne_internal(handle, timeout.Milliseconds, exitContext));
  127. }
  128. protected static readonly IntPtr InvalidHandle;
  129. private bool disposed = false;
  130. public void Dispose() {
  131. Dispose(true);
  132. // Take yourself off the Finalization queue
  133. GC.SuppressFinalize(this);
  134. }
  135. protected virtual void Dispose(bool explicitDisposing) {
  136. // Check to see if Dispose has already been called.
  137. if(!this.disposed) {
  138. // If this is a call to Dispose,
  139. // dispose all managed resources.
  140. if(explicitDisposing) {
  141. // Free up stuff here
  142. //Components.Dispose();
  143. }
  144. // Release unmanaged resources
  145. // Note that this is not thread safe.
  146. // Another thread could start
  147. // disposing the object after the
  148. // managed resources are disposed, but
  149. // before the disposed flag is set to
  150. // true.
  151. this.disposed=true;
  152. //Release(handle);
  153. //handle=IntPtr.Zero;
  154. }
  155. }
  156. ~WaitHandle() {
  157. Dispose(false);
  158. }
  159. }
  160. }