EventWaitHandle.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // System.Threading.EventWaitHandle.cs
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. //
  7. // (C) Ximian, Inc. (http://www.ximian.com)
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.IO;
  30. using System.Runtime.InteropServices;
  31. using System.Security.AccessControl;
  32. namespace System.Threading
  33. {
  34. [ComVisible (true)]
  35. public class EventWaitHandle : WaitHandle
  36. {
  37. private EventWaitHandle (IntPtr handle)
  38. {
  39. Handle = handle;
  40. }
  41. static bool IsManualReset (EventResetMode mode)
  42. {
  43. if ((mode < EventResetMode.AutoReset) || (mode > EventResetMode.ManualReset))
  44. throw new ArgumentException ("mode");
  45. return (mode == EventResetMode.ManualReset);
  46. }
  47. public EventWaitHandle (bool initialState, EventResetMode mode)
  48. {
  49. bool created;
  50. bool manual = IsManualReset (mode);
  51. Handle = NativeEventCalls.CreateEvent_internal (manual, initialState, null, out created);
  52. }
  53. #if !MOBILE
  54. public EventWaitHandle (bool initialState, EventResetMode mode,
  55. string name)
  56. {
  57. bool created;
  58. bool manual = IsManualReset (mode);
  59. Handle = NativeEventCalls.CreateEvent_internal (manual, initialState, name, out created);
  60. }
  61. public EventWaitHandle (bool initialState, EventResetMode mode,
  62. string name, out bool createdNew)
  63. {
  64. bool manual = IsManualReset (mode);
  65. Handle = NativeEventCalls.CreateEvent_internal (manual, initialState, name, out createdNew);
  66. }
  67. [MonoTODO ("Use access control in CreateEvent_internal")]
  68. public EventWaitHandle (bool initialState, EventResetMode mode,
  69. string name, out bool createdNew,
  70. EventWaitHandleSecurity eventSecurity)
  71. {
  72. bool manual = IsManualReset (mode);
  73. Handle = NativeEventCalls.CreateEvent_internal (manual, initialState, name, out createdNew);
  74. }
  75. public EventWaitHandleSecurity GetAccessControl ()
  76. {
  77. return new EventWaitHandleSecurity (SafeWaitHandle,
  78. AccessControlSections.Owner |
  79. AccessControlSections.Group |
  80. AccessControlSections.Access);
  81. }
  82. public static EventWaitHandle OpenExisting (string name)
  83. {
  84. return(OpenExisting (name, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify));
  85. }
  86. public static EventWaitHandle OpenExisting (string name, EventWaitHandleRights rights)
  87. {
  88. if (name == null) {
  89. throw new ArgumentNullException ("name");
  90. }
  91. if ((name.Length == 0) ||
  92. (name.Length > 260)) {
  93. throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
  94. }
  95. MonoIOError error;
  96. IntPtr handle = NativeEventCalls.OpenEvent_internal (name, rights, out error);
  97. if (handle == (IntPtr)null) {
  98. if (error == MonoIOError.ERROR_FILE_NOT_FOUND) {
  99. throw new WaitHandleCannotBeOpenedException (Locale.GetText ("Named Event handle does not exist: ") + name);
  100. } else if (error == MonoIOError.ERROR_ACCESS_DENIED) {
  101. throw new UnauthorizedAccessException ();
  102. } else {
  103. throw new IOException (Locale.GetText ("Win32 IO error: ") + error.ToString ());
  104. }
  105. }
  106. return(new EventWaitHandle (handle));
  107. }
  108. public static bool TryOpenExisting (string name, out EventWaitHandle result)
  109. {
  110. return TryOpenExisting (
  111. name, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify, out result);
  112. }
  113. public static bool TryOpenExisting (string name, EventWaitHandleRights rights,
  114. out EventWaitHandle result)
  115. {
  116. if (name == null) {
  117. throw new ArgumentNullException ("name");
  118. }
  119. if ((name.Length == 0) || (name.Length > 260)) {
  120. throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
  121. }
  122. MonoIOError error;
  123. IntPtr handle = NativeEventCalls.OpenEvent_internal (name, rights, out error);
  124. if (handle == (IntPtr)null) {
  125. result = null;
  126. return false;
  127. }
  128. result = new EventWaitHandle (handle);
  129. return true;
  130. }
  131. #else
  132. public EventWaitHandle (bool initialState, EventResetMode mode, string name)
  133. {
  134. throw new NotSupportedException ();
  135. }
  136. public EventWaitHandle (bool initialState, EventResetMode mode,
  137. string name, out bool createdNew)
  138. {
  139. throw new NotSupportedException ();
  140. }
  141. public EventWaitHandle (bool initialState, EventResetMode mode,
  142. string name, out bool createdNew,
  143. EventWaitHandleSecurity eventSecurity)
  144. {
  145. throw new NotSupportedException ();
  146. }
  147. public static EventWaitHandle OpenExisting (string name)
  148. {
  149. throw new NotSupportedException ();
  150. }
  151. public static EventWaitHandle OpenExisting (string name, EventWaitHandleRights rights)
  152. {
  153. throw new NotSupportedException ();
  154. }
  155. public static bool TryOpenExisting (string name, out EventWaitHandle result)
  156. {
  157. throw new NotSupportedException ();
  158. }
  159. public static bool TryOpenExisting (string name, EventWaitHandleRights rights,
  160. out EventWaitHandle result)
  161. {
  162. throw new NotSupportedException ();
  163. }
  164. #endif
  165. public bool Reset ()
  166. {
  167. /* This needs locking since another thread could dispose the handle */
  168. lock (this) {
  169. CheckDisposed ();
  170. return (NativeEventCalls.ResetEvent_internal (Handle));
  171. }
  172. }
  173. public bool Set ()
  174. {
  175. lock (this) {
  176. CheckDisposed ();
  177. return (NativeEventCalls.SetEvent_internal (Handle));
  178. }
  179. }
  180. #if !NET_2_1
  181. public void SetAccessControl (EventWaitHandleSecurity eventSecurity)
  182. {
  183. if (null == eventSecurity)
  184. throw new ArgumentNullException ("eventSecurity");
  185. eventSecurity.PersistModifications (SafeWaitHandle);
  186. }
  187. #endif
  188. }
  189. }