Mutex.cs 4.7 KB

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