WaitHandle.cs 5.1 KB

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