Mutex.cs 6.3 KB

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