WaitHandle.cs 5.1 KB

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