Mutex.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #if !NET_2_1
  36. using System.Security.AccessControl;
  37. #endif
  38. namespace System.Threading
  39. {
  40. [ComVisible (true)]
  41. public sealed class Mutex : WaitHandle
  42. {
  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 bool ReleaseMutex_internal(IntPtr handle);
  50. #if !NET_2_1
  51. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  52. private static extern IntPtr OpenMutex_internal (string name, MutexRights rights, out MonoIOError error);
  53. private Mutex (IntPtr handle)
  54. {
  55. Handle = handle;
  56. }
  57. #endif
  58. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  59. public Mutex() {
  60. bool created;
  61. Handle=CreateMutex_internal(false, null, out created);
  62. }
  63. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  64. public Mutex(bool initiallyOwned) {
  65. bool created;
  66. Handle=CreateMutex_internal(initiallyOwned, null,
  67. out created);
  68. }
  69. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  70. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  71. public Mutex (bool initiallyOwned, string name)
  72. {
  73. bool created;
  74. Handle = CreateMutex_internal (initiallyOwned, name, out created);
  75. }
  76. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  77. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  78. public Mutex (bool initiallyOwned, string name, out bool createdNew)
  79. {
  80. Handle = CreateMutex_internal (initiallyOwned, name, out createdNew);
  81. }
  82. #if !NET_2_1
  83. [MonoTODO ("Implement MutexSecurity")]
  84. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  85. public Mutex (bool initiallyOwned, string name, out bool createdNew, MutexSecurity mutexSecurity)
  86. {
  87. Handle = CreateMutex_internal (initiallyOwned, name, out createdNew);
  88. }
  89. public MutexSecurity GetAccessControl ()
  90. {
  91. throw new NotImplementedException ();
  92. }
  93. public static Mutex OpenExisting (string name)
  94. {
  95. return(OpenExisting (name, MutexRights.Synchronize |
  96. MutexRights.Modify));
  97. }
  98. public static Mutex OpenExisting (string name,
  99. MutexRights rights)
  100. {
  101. if (name == null) {
  102. throw new ArgumentNullException ("name");
  103. }
  104. if ((name.Length == 0) ||
  105. (name.Length > 260)) {
  106. throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
  107. }
  108. MonoIOError error;
  109. IntPtr handle = OpenMutex_internal (name, rights,
  110. out error);
  111. if (handle == (IntPtr)null) {
  112. if (error == MonoIOError.ERROR_FILE_NOT_FOUND) {
  113. throw new WaitHandleCannotBeOpenedException (Locale.GetText ("Named Mutex handle does not exist: ") + name);
  114. } else if (error == MonoIOError.ERROR_ACCESS_DENIED) {
  115. throw new UnauthorizedAccessException ();
  116. } else {
  117. throw new IOException (Locale.GetText ("Win32 IO error: ") + error.ToString ());
  118. }
  119. }
  120. return(new Mutex (handle));
  121. }
  122. #endif
  123. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  124. public void ReleaseMutex() {
  125. bool success = ReleaseMutex_internal(Handle);
  126. if (!success) {
  127. throw new ApplicationException ("Mutex is not owned");
  128. }
  129. }
  130. #if !NET_2_1
  131. public void SetAccessControl (MutexSecurity mutexSecurity)
  132. {
  133. throw new NotImplementedException ();
  134. }
  135. #endif
  136. }
  137. }