Semaphore.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #if NET_2_0
  29. using System.Runtime.ConstrainedExecution;
  30. using System.Runtime.InteropServices;
  31. using System.Security.AccessControl;
  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 ("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. [MonoTODO]
  86. public SemaphoreSecurity GetAccessControl ()
  87. {
  88. throw new NotImplementedException ();
  89. }
  90. [PrePrepareMethod]
  91. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
  92. public int Release ()
  93. {
  94. return (Release (1));
  95. }
  96. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
  97. public int Release (int releaseCount)
  98. {
  99. if (releaseCount < 1)
  100. throw new ArgumentOutOfRangeException ("releaseCount");
  101. int ret;
  102. bool fail;
  103. ret = ReleaseSemaphore_internal (Handle, releaseCount,
  104. out fail);
  105. if (fail) {
  106. throw new SemaphoreFullException ();
  107. }
  108. return (ret);
  109. }
  110. [MonoTODO]
  111. public void SetAccessControl (SemaphoreSecurity semaphoreSecurity)
  112. {
  113. if (semaphoreSecurity == null)
  114. throw new ArgumentNullException ("semaphoreSecurity");
  115. throw new NotImplementedException ();
  116. }
  117. // static methods
  118. public static Semaphore OpenExisting (string name)
  119. {
  120. return OpenExisting (name, SemaphoreRights.Synchronize | SemaphoreRights.Modify);
  121. }
  122. public static Semaphore OpenExisting (string name, SemaphoreRights rights)
  123. {
  124. if (name == null)
  125. throw new ArgumentNullException ("name");
  126. if ((name.Length ==0) || (name.Length > 260))
  127. throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
  128. MonoIOError error;
  129. IntPtr handle = OpenSemaphore_internal (name, rights,
  130. out error);
  131. if (handle == (IntPtr)null) {
  132. if (error == MonoIOError.ERROR_FILE_NOT_FOUND) {
  133. throw new WaitHandleCannotBeOpenedException (Locale.GetText ("Named Semaphore handle does not exist: ") + name);
  134. } else if (error == MonoIOError.ERROR_ACCESS_DENIED) {
  135. throw new UnauthorizedAccessException ();
  136. } else {
  137. throw new IOException (Locale.GetText ("Win32 IO error: ") + error.ToString ());
  138. }
  139. }
  140. return(new Semaphore (handle));
  141. }
  142. }
  143. }
  144. #endif