Files
acceptance-tests
data
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
Ix
Rx
NET
Resources
Samples
Source
.nuget
Microsoft.Reactive.Testing
Playground
References
Rx_Xamarin
System.Reactive.Core
System.Reactive.Debugger
System.Reactive.Experimental
System.Reactive.Interfaces
System.Reactive.Linq
System.Reactive.Observable.Aliases
System.Reactive.PlatformServices
System.Reactive.Providers
System.Reactive.Runtime.Remoting
System.Reactive.Windows.Forms
System.Reactive.Windows.Threading
System.Reactive.WindowsRuntime
Tests.System.Reactive
Dummies
Properties
Stress
Tests
App.cs
DispatcherHelpers.cs
Extensions.cs
MockDisposable.cs
MockEnumerable.cs
NullErrorObservable.cs
Semaphore.cs
TestBase.cs
TestLongRunningScheduler.cs
TestTaskScheduler.cs
Tests.System.Reactive.csproj
Utils.cs
packages
.gitattributes
.gitignore
35MSSharedLib1024.snk
Build.bat
BuildAll.proj
BuildSetup.bat
Clean.bat
Common.targets
Import.targets
Local.testsettings
README.txt
Rx.ruleset
Rx.sln.REMOVED.git-id
Test.ruleset
TraceAndTestImpact.testsettings
license.txt
packages.config
Test
tools
component
xpkg
.gitignore
README-microsoft-original.md
README.md
Rakefile
mono.patch
replacer.sh
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
libgc
llvm
m4
man
mcs
mk
mono
msvc
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
linux-packaging-mono/external/rx/Rx/NET/Source/Tests.System.Reactive/Semaphore.cs
Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

116 lines
3.3 KiB
C#

// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
#if SILVERLIGHT
using System;
namespace System.Threading
{
//Monitor based implementation of Semaphore
//that mimicks the .NET Semaphore class (System.Threading.Semaphore)
internal sealed class Semaphore : IDisposable
{
private int m_currentCount;
private int m_maximumCount;
private object m_lockObject;
private bool m_disposed;
public Semaphore(int initialCount, int maximumCount)
{
if (initialCount < 0)
{
throw new ArgumentOutOfRangeException("initialCount", "Non-negative number required.");
}
if (maximumCount < 1)
{
throw new ArgumentOutOfRangeException("maximumCount", "Positive number required.");
}
if (initialCount > maximumCount)
{
throw new ArgumentException("Initial count must be smaller than maximum");
}
m_currentCount = initialCount;
m_maximumCount = maximumCount;
m_lockObject = new object();
}
public int Release()
{
return this.Release(1);
}
public int Release(int releaseCount)
{
if (releaseCount < 1)
{
throw new ArgumentOutOfRangeException("releaseCount", "Positive number required.");
}
if (m_disposed)
{
throw new ObjectDisposedException("Semaphore");
}
var oldCount = default(int);
lock (m_lockObject)
{
oldCount = m_currentCount;
if (releaseCount + m_currentCount > m_maximumCount)
{
throw new ArgumentOutOfRangeException("releaseCount", "Amount of releases would overflow maximum");
}
m_currentCount += releaseCount;
//PulseAll makes sure all waiting threads get queued for acquiring the lock
//Pulse would only queue one thread.
Monitor.PulseAll(m_lockObject);
}
return oldCount;
}
public bool WaitOne()
{
return WaitOne(Timeout.Infinite);
}
public bool WaitOne(int millisecondsTimeout)
{
if (m_disposed)
{
throw new ObjectDisposedException("Semaphore");
}
lock (m_lockObject)
{
while (m_currentCount == 0)
{
if (!Monitor.Wait(m_lockObject, millisecondsTimeout))
{
return false;
}
}
m_currentCount--;
return true;
}
}
public bool WaitOne(TimeSpan timeout)
{
return WaitOne((int)timeout.TotalMilliseconds);
}
public void Close()
{
Dispose();
}
public void Dispose()
{
//the .NET CLR semaphore does not release waits upon dispose
//so we don't do that either.
m_disposed = true;
m_lockObject = null;
}
}
}
#endif