Mutex.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 static extern bool ReleaseMutex_internal(IntPtr handle);
  43. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  44. private static extern IntPtr CreateMutex_internal(
  45. bool initiallyOwned,
  46. string name,
  47. out bool created);
  48. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  49. private static extern IntPtr OpenMutex_internal (string name, MutexRights rights, out MonoIOError error);
  50. private Mutex (IntPtr handle)
  51. {
  52. Handle = handle;
  53. }
  54. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  55. public Mutex() {
  56. bool created;
  57. Handle=CreateMutex_internal(false, null, out created);
  58. }
  59. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  60. public Mutex(bool initiallyOwned) {
  61. bool created;
  62. Handle=CreateMutex_internal(initiallyOwned, null,
  63. out created);
  64. }
  65. #if !MOBILE
  66. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  67. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  68. public Mutex (bool initiallyOwned, string name)
  69. {
  70. bool created;
  71. Handle = CreateMutex_internal (initiallyOwned, name, out created);
  72. }
  73. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  74. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  75. public Mutex (bool initiallyOwned, string name, out bool createdNew)
  76. {
  77. Handle = CreateMutex_internal (initiallyOwned, name, out createdNew);
  78. }
  79. [MonoTODO ("Use MutexSecurity in CreateMutex_internal")]
  80. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  81. public Mutex (bool initiallyOwned, string name, out bool createdNew, MutexSecurity mutexSecurity)
  82. {
  83. Handle = CreateMutex_internal (initiallyOwned, name, out createdNew);
  84. }
  85. public MutexSecurity GetAccessControl ()
  86. {
  87. return new MutexSecurity (SafeWaitHandle,
  88. AccessControlSections.Owner |
  89. AccessControlSections.Group |
  90. AccessControlSections.Access);
  91. }
  92. public static Mutex OpenExisting (string name)
  93. {
  94. return(OpenExisting (name, MutexRights.Synchronize |
  95. MutexRights.Modify));
  96. }
  97. public static Mutex OpenExisting (string name,
  98. MutexRights rights)
  99. {
  100. if (name == null) {
  101. throw new ArgumentNullException ("name");
  102. }
  103. if ((name.Length == 0) ||
  104. (name.Length > 260)) {
  105. throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
  106. }
  107. MonoIOError error;
  108. IntPtr handle = OpenMutex_internal (name, rights,
  109. out error);
  110. if (handle == (IntPtr)null) {
  111. if (error == MonoIOError.ERROR_FILE_NOT_FOUND) {
  112. throw new WaitHandleCannotBeOpenedException (Locale.GetText ("Named Mutex handle does not exist: ") + name);
  113. } else if (error == MonoIOError.ERROR_ACCESS_DENIED) {
  114. throw new UnauthorizedAccessException ();
  115. } else {
  116. throw new IOException (Locale.GetText ("Win32 IO error: ") + error.ToString ());
  117. }
  118. }
  119. return(new Mutex (handle));
  120. }
  121. public static bool TryOpenExisting (string name, out Mutex result)
  122. {
  123. return TryOpenExisting (name, MutexRights.Synchronize | MutexRights.Modify, out result);
  124. }
  125. public static bool TryOpenExisting (string name, MutexRights rights, out Mutex result)
  126. {
  127. if (name == null) {
  128. throw new ArgumentNullException ("name");
  129. }
  130. if ((name.Length == 0) || (name.Length > 260)) {
  131. throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
  132. }
  133. MonoIOError error;
  134. IntPtr handle = OpenMutex_internal (name, rights, out error);
  135. if (handle == (IntPtr)null) {
  136. result = null;
  137. return false;
  138. }
  139. result = new Mutex (handle);
  140. return true;
  141. }
  142. #else
  143. public Mutex (bool initiallyOwned, string name)
  144. {
  145. throw new NotSupportedException ();
  146. }
  147. public Mutex (bool initiallyOwned, string name, out bool createdNew)
  148. {
  149. throw new NotSupportedException ();
  150. }
  151. public Mutex (bool initiallyOwned, string name, out bool createdNew, MutexSecurity mutexSecurity)
  152. {
  153. throw new NotSupportedException ();
  154. }
  155. public static Mutex OpenExisting (string name)
  156. {
  157. throw new NotSupportedException ();
  158. }
  159. public static Mutex OpenExisting (string name, MutexRights rights)
  160. {
  161. throw new NotSupportedException ();
  162. }
  163. public static bool TryOpenExisting (string name, out Mutex result)
  164. {
  165. throw new NotSupportedException ();
  166. }
  167. public static bool TryOpenExisting (string name, MutexRights rights, out Mutex result)
  168. {
  169. throw new NotSupportedException ();
  170. }
  171. #endif
  172. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  173. public void ReleaseMutex() {
  174. bool success = ReleaseMutex_internal(Handle);
  175. if (!success) {
  176. throw new ApplicationException ("Mutex is not owned");
  177. }
  178. }
  179. #if !NET_2_1
  180. public void SetAccessControl (MutexSecurity mutexSecurity)
  181. {
  182. if (null == mutexSecurity)
  183. throw new ArgumentNullException ("mutexSecurity");
  184. mutexSecurity.PersistModifications (SafeWaitHandle);
  185. }
  186. #endif
  187. }
  188. }