Semaphore.Silverlight.cs 3.4 KB

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