Semaphore.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // System.Threading.Semaphore.cs
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Runtime.ConstrainedExecution;
  29. using System.Runtime.InteropServices;
  30. using System.Security.AccessControl;
  31. using System.Security.Permissions;
  32. using System.Runtime.CompilerServices;
  33. using System.IO;
  34. namespace System.Threading {
  35. [ComVisible (false)]
  36. public sealed class Semaphore : WaitHandle {
  37. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  38. private static extern IntPtr CreateSemaphore_internal (
  39. int initialCount, int maximumCount, string name,
  40. out bool createdNew);
  41. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  42. private static extern int ReleaseSemaphore_internal (
  43. IntPtr handle, int releaseCount, out bool fail);
  44. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  45. private static extern IntPtr OpenSemaphore_internal (string name, SemaphoreRights rights, out MonoIOError error);
  46. private Semaphore (IntPtr handle)
  47. {
  48. Handle = handle;
  49. }
  50. public Semaphore (int initialCount, int maximumCount)
  51. : this (initialCount, maximumCount, null)
  52. {
  53. }
  54. public Semaphore (int initialCount, int maximumCount, string name)
  55. {
  56. if (initialCount < 0)
  57. throw new ArgumentOutOfRangeException ("initialCount", "< 0");
  58. if (maximumCount < 1)
  59. throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
  60. if (initialCount > maximumCount)
  61. throw new ArgumentException ("initialCount > maximumCount");
  62. bool created;
  63. Handle = CreateSemaphore_internal (initialCount,
  64. maximumCount, name,
  65. out created);
  66. }
  67. public Semaphore (int initialCount, int maximumCount, string name, out bool createdNew)
  68. : this (initialCount, maximumCount, name, out createdNew, null)
  69. {
  70. }
  71. [MonoTODO ("CreateSemaphore_internal does not support access control, semaphoreSecurity is ignored")]
  72. public Semaphore (int initialCount, int maximumCount, string name, out bool createdNew,
  73. SemaphoreSecurity semaphoreSecurity)
  74. {
  75. if (initialCount < 0)
  76. throw new ArgumentOutOfRangeException ("initialCount", "< 0");
  77. if (maximumCount < 1)
  78. throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
  79. if (initialCount > maximumCount)
  80. throw new ArgumentException ("initialCount > maximumCount");
  81. Handle = CreateSemaphore_internal (initialCount,
  82. maximumCount, name,
  83. out createdNew);
  84. }
  85. public SemaphoreSecurity GetAccessControl ()
  86. {
  87. return new SemaphoreSecurity (SafeWaitHandle,
  88. AccessControlSections.Owner |
  89. AccessControlSections.Group |
  90. AccessControlSections.Access);
  91. }
  92. [PrePrepareMethod]
  93. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
  94. public int Release ()
  95. {
  96. return (Release (1));
  97. }
  98. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
  99. public int Release (int releaseCount)
  100. {
  101. if (releaseCount < 1)
  102. throw new ArgumentOutOfRangeException ("releaseCount");
  103. int ret;
  104. bool fail;
  105. ret = ReleaseSemaphore_internal (Handle, releaseCount,
  106. out fail);
  107. if (fail) {
  108. throw new SemaphoreFullException ();
  109. }
  110. return (ret);
  111. }
  112. public void SetAccessControl (SemaphoreSecurity semaphoreSecurity)
  113. {
  114. if (semaphoreSecurity == null)
  115. throw new ArgumentNullException ("semaphoreSecurity");
  116. semaphoreSecurity.PersistModifications (SafeWaitHandle);
  117. }
  118. // static methods
  119. #if !MOBILE
  120. public static Semaphore OpenExisting (string name)
  121. {
  122. return OpenExisting (name, SemaphoreRights.Synchronize | SemaphoreRights.Modify);
  123. }
  124. public static Semaphore OpenExisting (string name, SemaphoreRights rights)
  125. {
  126. if (name == null)
  127. throw new ArgumentNullException ("name");
  128. if ((name.Length ==0) || (name.Length > 260))
  129. throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
  130. MonoIOError error;
  131. IntPtr handle = OpenSemaphore_internal (name, rights,
  132. out error);
  133. if (handle == (IntPtr)null) {
  134. if (error == MonoIOError.ERROR_FILE_NOT_FOUND) {
  135. throw new WaitHandleCannotBeOpenedException (Locale.GetText ("Named Semaphore handle does not exist: ") + name);
  136. } else if (error == MonoIOError.ERROR_ACCESS_DENIED) {
  137. throw new UnauthorizedAccessException ();
  138. } else {
  139. throw new IOException (Locale.GetText ("Win32 IO error: ") + error.ToString ());
  140. }
  141. }
  142. return(new Semaphore (handle));
  143. }
  144. [SecurityPermissionAttribute (SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  145. public static bool TryOpenExisting (string name, out Semaphore result)
  146. {
  147. return TryOpenExisting (name, SemaphoreRights.Synchronize | SemaphoreRights.Modify, out result);
  148. }
  149. [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  150. public static bool TryOpenExisting (string name, SemaphoreRights rights, out Semaphore result)
  151. {
  152. if (name == null)
  153. throw new ArgumentNullException ("name");
  154. if ((name.Length == 0) || (name.Length > 260))
  155. throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
  156. MonoIOError error;
  157. IntPtr handle = OpenSemaphore_internal (name, rights, out error);
  158. if (handle == (IntPtr)null) {
  159. result = null;
  160. return false;
  161. }
  162. result = new Semaphore (handle);
  163. return true;
  164. }
  165. #else
  166. public static Semaphore OpenExisting (string name)
  167. {
  168. throw new NotSupportedException ();
  169. }
  170. public static Semaphore OpenExisting (string name, SemaphoreRights rights)
  171. {
  172. throw new NotSupportedException ();
  173. }
  174. public static bool TryOpenExisting (string name, out Semaphore result)
  175. {
  176. throw new NotSupportedException ();
  177. }
  178. public static bool TryOpenExisting (string name, SemaphoreRights rights, out Semaphore result)
  179. {
  180. throw new NotSupportedException ();
  181. }
  182. #endif
  183. }
  184. }