Mutex.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // System.Threading.Mutex.cs
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. // Veronica De Santis ([email protected])
  7. //
  8. // (C) 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.Runtime.CompilerServices;
  31. using System.Security.Permissions;
  32. using System.Runtime.ConstrainedExecution;
  33. using System.IO;
  34. using System.Runtime.InteropServices;
  35. using System.Security.AccessControl;
  36. namespace System.Threading
  37. {
  38. [ComVisible (true)]
  39. public sealed class Mutex : WaitHandle
  40. {
  41. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  42. private unsafe static extern IntPtr CreateMutex_icall (bool initiallyOwned, char *name,
  43. int name_length, out bool created);
  44. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  45. private unsafe static extern IntPtr OpenMutex_icall (char *name, int name_length,
  46. MutexRights rights, out MonoIOError error);
  47. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  48. private static extern bool ReleaseMutex_internal(IntPtr handle);
  49. private unsafe static IntPtr CreateMutex_internal (bool initiallyOwned,
  50. string name, out bool created)
  51. {
  52. fixed (char *fixed_name = name)
  53. return CreateMutex_icall (initiallyOwned, fixed_name,
  54. name?.Length ?? 0, out created);
  55. }
  56. private unsafe static IntPtr OpenMutex_internal (string name, MutexRights rights, out MonoIOError error)
  57. {
  58. fixed (char *fixed_name = name)
  59. return OpenMutex_icall (fixed_name, name?.Length ?? 0, rights, out error);
  60. }
  61. private Mutex (IntPtr handle)
  62. {
  63. Handle = handle;
  64. }
  65. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  66. public Mutex() {
  67. bool created;
  68. Handle=CreateMutex_internal(false, null, out created);
  69. }
  70. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  71. public Mutex(bool initiallyOwned) {
  72. bool created;
  73. Handle=CreateMutex_internal(initiallyOwned, null,
  74. out created);
  75. }
  76. #if !MOBILE
  77. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  78. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  79. public Mutex (bool initiallyOwned, string name)
  80. {
  81. bool created;
  82. Handle = CreateMutex_internal (initiallyOwned, name, out created);
  83. }
  84. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  85. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  86. public Mutex (bool initiallyOwned, string name, out bool createdNew)
  87. {
  88. Handle = CreateMutex_internal (initiallyOwned, name, out createdNew);
  89. }
  90. [MonoTODO ("Use MutexSecurity in CreateMutex_internal")]
  91. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  92. public Mutex (bool initiallyOwned, string name, out bool createdNew, MutexSecurity mutexSecurity)
  93. {
  94. Handle = CreateMutex_internal (initiallyOwned, name, out createdNew);
  95. }
  96. public MutexSecurity GetAccessControl ()
  97. {
  98. return new MutexSecurity (SafeWaitHandle,
  99. AccessControlSections.Owner |
  100. AccessControlSections.Group |
  101. AccessControlSections.Access);
  102. }
  103. public static Mutex OpenExisting (string name)
  104. {
  105. return(OpenExisting (name, MutexRights.Synchronize |
  106. MutexRights.Modify));
  107. }
  108. public static Mutex OpenExisting (string name,
  109. MutexRights rights)
  110. {
  111. if (name == null) {
  112. throw new ArgumentNullException ("name");
  113. }
  114. if ((name.Length == 0) ||
  115. (name.Length > 260)) {
  116. throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
  117. }
  118. MonoIOError error;
  119. IntPtr handle = OpenMutex_internal (name, rights,
  120. out error);
  121. if (handle == (IntPtr)null) {
  122. if (error == MonoIOError.ERROR_FILE_NOT_FOUND) {
  123. throw new WaitHandleCannotBeOpenedException (Locale.GetText ("Named Mutex handle does not exist: ") + name);
  124. } else if (error == MonoIOError.ERROR_ACCESS_DENIED) {
  125. throw new UnauthorizedAccessException ();
  126. } else {
  127. throw new IOException (Locale.GetText ("Win32 IO error: ") + error.ToString ());
  128. }
  129. }
  130. return(new Mutex (handle));
  131. }
  132. public static bool TryOpenExisting (string name, out Mutex result)
  133. {
  134. return TryOpenExisting (name, MutexRights.Synchronize | MutexRights.Modify, out result);
  135. }
  136. public static bool TryOpenExisting (string name, MutexRights rights, out Mutex result)
  137. {
  138. if (name == null) {
  139. throw new ArgumentNullException ("name");
  140. }
  141. if ((name.Length == 0) || (name.Length > 260)) {
  142. throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
  143. }
  144. MonoIOError error;
  145. IntPtr handle = OpenMutex_internal (name, rights, out error);
  146. if (handle == (IntPtr)null) {
  147. result = null;
  148. return false;
  149. }
  150. result = new Mutex (handle);
  151. return true;
  152. }
  153. #else
  154. public Mutex (bool initiallyOwned, string name)
  155. {
  156. throw new NotSupportedException ();
  157. }
  158. public Mutex (bool initiallyOwned, string name, out bool createdNew)
  159. {
  160. throw new NotSupportedException ();
  161. }
  162. public Mutex (bool initiallyOwned, string name, out bool createdNew, MutexSecurity mutexSecurity)
  163. {
  164. throw new NotSupportedException ();
  165. }
  166. public static Mutex OpenExisting (string name)
  167. {
  168. throw new NotSupportedException ();
  169. }
  170. public static Mutex OpenExisting (string name, MutexRights rights)
  171. {
  172. throw new NotSupportedException ();
  173. }
  174. public static bool TryOpenExisting (string name, out Mutex result)
  175. {
  176. throw new NotSupportedException ();
  177. }
  178. public static bool TryOpenExisting (string name, MutexRights rights, out Mutex result)
  179. {
  180. throw new NotSupportedException ();
  181. }
  182. #endif
  183. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  184. public void ReleaseMutex() {
  185. bool success = ReleaseMutex_internal(Handle);
  186. if (!success) {
  187. throw new ApplicationException ("Mutex is not owned");
  188. }
  189. }
  190. #if !MOBILE
  191. public void SetAccessControl (MutexSecurity mutexSecurity)
  192. {
  193. if (null == mutexSecurity)
  194. throw new ArgumentNullException ("mutexSecurity");
  195. mutexSecurity.PersistModifications (SafeWaitHandle);
  196. }
  197. #endif
  198. }
  199. }