Mutex.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #if NET_2_0
  33. using System.Runtime.ConstrainedExecution;
  34. using System.Security.AccessControl;
  35. using System.IO;
  36. using System.Runtime.InteropServices;
  37. #endif
  38. namespace System.Threading
  39. {
  40. #if NET_2_0
  41. [ComVisible (true)]
  42. #endif
  43. public sealed class Mutex : WaitHandle
  44. {
  45. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  46. private static extern IntPtr CreateMutex_internal(
  47. bool initiallyOwned,
  48. string name,
  49. out bool created);
  50. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  51. private static extern bool ReleaseMutex_internal(IntPtr handle);
  52. #if NET_2_0
  53. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  54. private static extern IntPtr OpenMutex_internal (string name, MutexRights rights, out MonoIOError error);
  55. private Mutex (IntPtr handle)
  56. {
  57. Handle = handle;
  58. }
  59. #endif
  60. #if NET_2_0
  61. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  62. #endif
  63. public Mutex() {
  64. bool created;
  65. Handle=CreateMutex_internal(false, null, out created);
  66. }
  67. #if NET_2_0
  68. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  69. #endif
  70. public Mutex(bool initiallyOwned) {
  71. bool created;
  72. Handle=CreateMutex_internal(initiallyOwned, null,
  73. out created);
  74. }
  75. #if NET_2_0
  76. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  77. #endif
  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. #if NET_2_0
  85. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  86. #endif
  87. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  88. public Mutex (bool initiallyOwned, string name, out bool createdNew)
  89. {
  90. Handle = CreateMutex_internal (initiallyOwned, name, out createdNew);
  91. }
  92. #if NET_2_0
  93. [MonoTODO ("Implement MutexSecurity")]
  94. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  95. public Mutex (bool initiallyOwned, string name, out bool createdNew, MutexSecurity mutexSecurity)
  96. {
  97. Handle = CreateMutex_internal (initiallyOwned, name, out createdNew);
  98. }
  99. public MutexSecurity GetAccessControl ()
  100. {
  101. throw new NotImplementedException ();
  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. #endif
  133. #if NET_2_0
  134. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  135. #endif
  136. public void ReleaseMutex() {
  137. bool success = ReleaseMutex_internal(Handle);
  138. if (!success) {
  139. throw new ApplicationException ("Mutex is not owned");
  140. }
  141. }
  142. #if NET_2_0
  143. public void SetAccessControl (MutexSecurity mutexSecurity)
  144. {
  145. throw new NotImplementedException ();
  146. }
  147. #endif
  148. }
  149. }