Semaphore.Silverlight.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. #if NO_SEMAPHORE && (SILVERLIGHT || PLIB_LITE)
  5. using System;
  6. using System.Threading;
  7. namespace System.Reactive.Threading
  8. {
  9. //Monitor based implementation of Semaphore
  10. //that mimicks the .NET Semaphore class (System.Threading.Semaphore)
  11. internal sealed class Semaphore : IDisposable
  12. {
  13. private int m_currentCount;
  14. private readonly int m_maximumCount;
  15. private readonly object m_lockObject;
  16. private bool m_disposed;
  17. public Semaphore(int initialCount, int maximumCount)
  18. {
  19. if (initialCount < 0)
  20. {
  21. throw new ArgumentOutOfRangeException("initialCount", "Non-negative number required.");
  22. }
  23. if (maximumCount < 1)
  24. {
  25. throw new ArgumentOutOfRangeException("maximumCount", "Positive number required.");
  26. }
  27. if (initialCount > maximumCount)
  28. {
  29. throw new ArgumentException("Initial count must be smaller than maximum");
  30. }
  31. m_currentCount = initialCount;
  32. m_maximumCount = maximumCount;
  33. m_lockObject = new object();
  34. }
  35. public int Release()
  36. {
  37. return this.Release(1);
  38. }
  39. public int Release(int releaseCount)
  40. {
  41. if (releaseCount < 1)
  42. {
  43. throw new ArgumentOutOfRangeException("releaseCount", "Positive number required.");
  44. }
  45. if (m_disposed)
  46. {
  47. throw new ObjectDisposedException("Semaphore");
  48. }
  49. var oldCount = default(int);
  50. lock (m_lockObject)
  51. {
  52. oldCount = m_currentCount;
  53. if (releaseCount + m_currentCount > m_maximumCount)
  54. {
  55. throw new ArgumentOutOfRangeException("releaseCount", "Amount of releases would overflow maximum");
  56. }
  57. m_currentCount += releaseCount;
  58. //PulseAll makes sure all waiting threads get queued for acquiring the lock
  59. //Pulse would only queue one thread.
  60. Monitor.PulseAll(m_lockObject);
  61. }
  62. return oldCount;
  63. }
  64. public bool WaitOne()
  65. {
  66. return WaitOne(Timeout.Infinite);
  67. }
  68. public bool WaitOne(int millisecondsTimeout)
  69. {
  70. if (m_disposed)
  71. {
  72. throw new ObjectDisposedException("Semaphore");
  73. }
  74. lock (m_lockObject)
  75. {
  76. while (m_currentCount == 0)
  77. {
  78. if (!Monitor.Wait(m_lockObject, millisecondsTimeout))
  79. {
  80. return false;
  81. }
  82. }
  83. m_currentCount--;
  84. return true;
  85. }
  86. }
  87. public bool WaitOne(TimeSpan timeout)
  88. {
  89. return WaitOne((int)timeout.TotalMilliseconds);
  90. }
  91. public void Close()
  92. {
  93. Dispose();
  94. }
  95. public void Dispose()
  96. {
  97. //the .NET CLR semaphore does not release waits upon dispose
  98. //so we don't do that either.
  99. m_disposed = true;
  100. }
  101. }
  102. }
  103. #endif