Semaphore.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. namespace System.Threading {
  34. [ComVisible (false)]
  35. public sealed class Semaphore : WaitHandle {
  36. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  37. private static extern IntPtr CreateSemaphore_internal (
  38. int initialCount, int maximumCount, string name,
  39. out bool createdNew);
  40. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  41. private static extern int ReleaseSemaphore_internal (
  42. IntPtr handle, int releaseCount, out bool fail);
  43. public Semaphore (int initialCount, int maximumCount)
  44. : this (initialCount, maximumCount, null)
  45. {
  46. }
  47. public Semaphore (int initialCount, int maximumCount, string name)
  48. {
  49. if (initialCount < 0)
  50. throw new ArgumentOutOfRangeException ("initialCount", "< 0");
  51. if (maximumCount < 1)
  52. throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
  53. if (initialCount > maximumCount)
  54. throw new ArgumentException ("initialCount > maximumCount");
  55. bool created;
  56. Handle = CreateSemaphore_internal (initialCount,
  57. maximumCount, name,
  58. out created);
  59. }
  60. public Semaphore (int initialCount, int maximumCount, string name, out bool createdNew)
  61. : this (initialCount, maximumCount, name, out createdNew, null)
  62. {
  63. }
  64. [MonoTODO ("Implement access control")]
  65. public Semaphore (int initialCount, int maximumCount, string name, out bool createdNew,
  66. SemaphoreSecurity semaphoreSecurity)
  67. {
  68. if (initialCount < 0)
  69. throw new ArgumentOutOfRangeException ("initialCount", "< 0");
  70. if (maximumCount < 1)
  71. throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
  72. if (initialCount > maximumCount)
  73. throw new ArgumentException ("initialCount > maximumCount");
  74. Handle = CreateSemaphore_internal (initialCount,
  75. maximumCount, name,
  76. out createdNew);
  77. }
  78. [MonoTODO]
  79. public SemaphoreSecurity GetAccessControl ()
  80. {
  81. throw new NotImplementedException ();
  82. }
  83. [PrePrepareMethod]
  84. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
  85. public int Release ()
  86. {
  87. return (Release (1));
  88. }
  89. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
  90. public int Release (int releaseCount)
  91. {
  92. if (releaseCount < 1)
  93. throw new ArgumentOutOfRangeException ("releaseCount");
  94. int ret;
  95. bool fail;
  96. ret = ReleaseSemaphore_internal (Handle, releaseCount,
  97. out fail);
  98. if (fail) {
  99. throw new SemaphoreFullException ();
  100. }
  101. return (ret);
  102. }
  103. [MonoTODO]
  104. public void SetAccessControl (SemaphoreSecurity semaphoreSecurity)
  105. {
  106. if (semaphoreSecurity == null)
  107. throw new ArgumentNullException ("semaphoreSecurity");
  108. throw new NotImplementedException ();
  109. }
  110. // static methods
  111. public static Semaphore OpenExisting (string name)
  112. {
  113. return OpenExisting (name, SemaphoreRights.Synchronize | SemaphoreRights.Modify);
  114. }
  115. [MonoTODO]
  116. public static Semaphore OpenExisting (string name, SemaphoreRights rights)
  117. {
  118. if (name == null)
  119. throw new ArgumentNullException ("name");
  120. if ((name.Length ==0) || (name.Length > 260))
  121. throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
  122. throw new NotImplementedException ();
  123. }
  124. }
  125. }
  126. #endif