User: xtoff Date: 2009/12/19 03:15 PM Added: /Core/trunk/src/Castle.Core.Tests/ MonitorLockTestCase.cs, SlimReadWriteLockTestCase.cs /Core/trunk/src/Castle.Core/ InternalsVisibleToTests.cs /Core/trunk/src/Castle.Core/Internal/ ILockHolder.cs, IUpgradeableLockHolder.cs, Lock.cs, MonitorLock.cs, MonitorLockHolder.cs, MonitorUpgradeableLockHolder.cs, NoOpLock.cs, NoOpUpgradeableLock.cs, SlimReadLockHolder.cs, SlimReadWriteLock.cs, SlimUpgradeableReadLockHolder.cs, SlimWriteLockHolder.cs
Removed: /Core/trunk/src/Castle.Core/Internal/ ReadLock.cs, SlimReaderWriterLock.cs, UpgradableLock.cs, WriteLock.cs Modified: /Core/trunk/src/Castle.Core.Tests/ Castle.Core.Tests-vs2008.csproj /Core/trunk/src/Castle.Core/ Castle.Core-vs2008.csproj Log: - BREAKING CHANGE - completely rebuilt lock mechanism wrapper both the default (using ReaderWriterLockSlim) and the fallback for Silverlight (using Monitor). It is "using friendly", immitates recursion, and takes care for tracking recursive calls itself. - I'm not an expert in these things so if someone can take a look to find some mistakes I may have made, it'd be appreciated. File Changes: Directory: /Core/trunk/src/Castle.Core/ ======================================= File [modified]: Castle.Core-vs2008.csproj Delta lines: +23 -0 =================================================================== --- Core/trunk/src/Castle.Core/Internal/ILockHolder.cs (rev 0) +++ Core/trunk/src/Castle.Core/Internal/ILockHolder.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -0,0 +1,23 @@ +// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Castle.Core.Internal +{ + using System; + + public interface ILockHolder:IDisposable + { + bool LockAcquired { get; } + } +} File [added]: InternalsVisibleToTests.cs Delta lines: +3 -1 =================================================================== --- Core/trunk/src/Castle.Core.Tests/Castle.Core.Tests-vs2008.csproj 2009-12-19 15:47:58 UTC (rev 6468) +++ Core/trunk/src/Castle.Core.Tests/Castle.Core.Tests-vs2008.csproj 2009-12-19 22:15:33 UTC (rev 6469) @@ -23,7 +23,7 @@ <DebugType>full</DebugType> <Optimize>false</Optimize> <OutputPath>bin\Debug\</OutputPath> - <DefineConstants>TRACE;DEBUG</DefineConstants> + <DefineConstants>TRACE;DEBUG;DOTNET35</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> <TreatWarningsAsErrors>true</TreatWarningsAsErrors> @@ -57,6 +57,8 @@ <Compile Include="GraphTestCase.cs" /> <Compile Include="LevelFilteredLoggerTests.cs" /> <Compile Include="LinkedListTestCase.cs" /> + <Compile Include="MonitorLockTestCase.cs" /> + <Compile Include="SlimReadWriteLockTestCase.cs" /> <Compile Include="ReflectionBasedDictionaryAdapterTestCase.cs" /> <Compile Include="Resources\AssemblyResourceFactoryTestCase.cs" /> Directory: /Core/trunk/src/Castle.Core.Tests/ ============================================= File [modified]: Castle.Core.Tests-vs2008.csproj Delta lines: +178 -0 =================================================================== --- Core/trunk/src/Castle.Core.Tests/MonitorLockTestCase.cs (rev 0) +++ Core/trunk/src/Castle.Core.Tests/MonitorLockTestCase.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -0,0 +1,178 @@ +// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Castle.Core.Tests +{ + using System.Threading; + + using Castle.Core.Internal; + + using NUnit.Framework; + + [TestFixture] + public class MonitorLockTestCase + { + private MonitorLock @lock; + + [SetUp] + public void SetUp() + { + @lock = new MonitorLock(); + } + + [Test] + public void Can_be_used_ForReading_multiple_nested_time() + { + using (@lock.ForReading()) + { + using (var holder = @lock.ForReading()) + { + Assert.IsTrue(holder.LockAcquired); + } + } + } + + [Test] + public void Can_be_used_ForWriting_multiple_nested_time() + { + + using (@lock.ForWriting()) + { + using (var holder = @lock.ForWriting()) + { + Assert.IsTrue(holder.LockAcquired); + } + } + } + + [Test] + public void Can_be_used_ForReadingUpgradeable_multiple_nested_time() + { + + using (@lock.ForReadingUpgradeable()) + { + using (var holder = @lock.ForReadingUpgradeable()) + { + Assert.IsTrue(holder.LockAcquired); + } + } + } + + [Test] + public void Can_be_upgraded_from_nested_ForReadingUpgradeable() + { + using (@lock.ForReadingUpgradeable()) + { + using (var holder = @lock.ForReadingUpgradeable()) + { + var upgrade = holder.Upgrade(); + Assert.IsTrue(holder.LockAcquired); + Assert.IsTrue(upgrade.LockAcquired); + } + } + } + + [Test] + public void Can_be_used_ForReading_when_used_ForWriting() + { + using (@lock.ForWriting()) + { + using (var holder = @lock.ForReading()) + { + Assert.IsTrue(holder.LockAcquired); + } + } + } + + [Test] + public void Can_be_used_ForReading_when_used_ForReadingUpgradeable() + { + using (@lock.ForReadingUpgradeable()) + { + using (var holder = @lock.ForReading()) + { + Assert.IsTrue(holder.LockAcquired); + } + } + } + + [Test,Ignore("Should we add this behavior for consistency?")] + public void Can_NOT_be_used_ForReadingUpgradeable_when_used_ForReading() + { + using (@lock.ForReading()) + { + Assert.Throws(typeof(LockRecursionException), () => @lock.ForReadingUpgradeable()); + } + } + + [Test] + public void Can_be_used_ForReadingUpgradeable_when_used_ForWriting() + { + using (@lock.ForWriting()) + { + using (var holder = @lock.ForReadingUpgradeable()) + { + Assert.IsTrue(holder.LockAcquired); + } + } + } + + [Test, Ignore("Should we add this behavior for consistency?")] + public void Can_NOT_be_used_ForWriting_when_used_ForReading() + { + using (@lock.ForReading()) + { + Assert.Throws(typeof(LockRecursionException), () => @lock.ForWriting()); + } + } + + [Test] + public void Can_be_used_ForWriting_when_used_ForReadingUpgradeable() + { + using (@lock.ForReadingUpgradeable()) + { + using (var holder = @lock.ForWriting()) + { + Assert.IsTrue(holder.LockAcquired); + } + } + } + + [Test] + public void Can_be_used_ForWriting_when_used_ForReadingUpgradeable_and_upgraded_after() + { + using (var upg = @lock.ForReadingUpgradeable()) + { + using (var holder = @lock.ForWriting()) + { + Assert.IsTrue(holder.LockAcquired); + upg.Upgrade(); + } + } + } + + [Test] + public void Can_be_used_ForWriting_when_used_ForReadingUpgradeable_and_upgraded_before() + { + using (var upg = @lock.ForReadingUpgradeable()) + { + upg.Upgrade(); + using (var holder = @lock.ForWriting()) + { + Assert.IsTrue(holder.LockAcquired); + } + } + } + } +} File [added]: MonitorLockTestCase.cs Delta lines: +171 -0 =================================================================== --- Core/trunk/src/Castle.Core.Tests/SlimReadWriteLockTestCase.cs (rev 0) +++ Core/trunk/src/Castle.Core.Tests/SlimReadWriteLockTestCase.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -0,0 +1,171 @@ +namespace Castle.Core.Tests +{ + using System.Threading; + + using Castle.Core.Internal; + + using NUnit.Framework; + +#if DOTNET35 && !SILVERLIGHT + [TestFixture] + public class SlimReadWriteLockTestCase + { + private SlimReadWriteLock @lock; + + [SetUp] + public void SetUp() + { + @lock = new SlimReadWriteLock(); + } + + [Test] + public void Can_be_used_ForReading_multiple_nested_time() + { + using( @lock.ForReading()) + { + using(@lock.ForReading()) + { + Assert.IsTrue(@lock.IsReadLockHeld); + } + } + } + + [Test] + public void Can_be_used_ForWriting_multiple_nested_time() + { + + using (@lock.ForWriting()) + { + using (@lock.ForWriting()) + { + Assert.IsTrue(@lock.IsWriteLockHeld); + } + } + } + + [Test] + public void Can_be_used_ForReadingUpgradeable_multiple_nested_time() + { + + using (@lock.ForReadingUpgradeable()) + { + using (@lock.ForReadingUpgradeable()) + { + Assert.IsTrue(@lock.IsUpgradeableReadLockHeld); + } + } + } + + [Test] + public void Can_be_upgraded_from_nested_ForReadingUpgradeable() + { + using (@lock.ForReadingUpgradeable()) + { + using (var holder = @lock.ForReadingUpgradeable()) + { + holder.Upgrade(); + Assert.IsTrue(@lock.IsWriteLockHeld); + } + } + } + + [Test] + public void Can_be_used_ForReading_when_used_ForWriting() + { + using (@lock.ForWriting()) + { + using (var holder = @lock.ForReading()) + { + Assert.IsTrue(@lock.IsWriteLockHeld); + Assert.IsTrue(holder.LockAcquired); + } + } + } + + [Test] + public void Can_be_used_ForReading_when_used_ForReadingUpgradeable() + { + using (@lock.ForReadingUpgradeable()) + { + using (var holder = @lock.ForReading()) + { + Assert.IsTrue(@lock.IsUpgradeableReadLockHeld); + Assert.IsTrue(holder.LockAcquired); + } + } + } + + [Test] + public void Can_NOT_be_used_ForReadingUpgradeable_when_used_ForReading() + { + using (@lock.ForReading()) + { + Assert.Throws(typeof(LockRecursionException), () => @lock.ForReadingUpgradeable()); + } + } + + [Test] + public void Can_be_used_ForReadingUpgradeable_when_used_ForWriting() + { + using (@lock.ForWriting()) + { + using (var holder = @lock.ForReadingUpgradeable()) + { + Assert.IsTrue(@lock.IsWriteLockHeld); + Assert.IsTrue(holder.LockAcquired); + } + } + } + + [Test] + public void Can_NOT_be_used_ForWriting_when_used_ForReading() + { + using (@lock.ForReading()) + { + Assert.Throws(typeof(LockRecursionException), () => @lock.ForWriting()); + } + } + + [Test] + public void Can_be_used_ForWriting_when_used_ForReadingUpgradeable() + { + using (@lock.ForReadingUpgradeable()) + { + using (var holder = @lock.ForWriting()) + { + Assert.IsTrue(@lock.IsUpgradeableReadLockHeld); + Assert.IsTrue(holder.LockAcquired); + } + } + } + + [Test] + public void Can_be_used_ForWriting_when_used_ForReadingUpgradeable_and_upgraded_after() + { + using (var upg = @lock.ForReadingUpgradeable()) + { + using (var holder = @lock.ForWriting()) + { + Assert.IsTrue(@lock.IsUpgradeableReadLockHeld); + Assert.IsTrue(holder.LockAcquired); + upg.Upgrade(); + } + } + } + + [Test] + public void Can_be_used_ForWriting_when_used_ForReadingUpgradeable_and_upgraded_before() + { + using (var upg = @lock.ForReadingUpgradeable()) + { + upg.Upgrade(); + using (var holder = @lock.ForWriting()) + { + Assert.IsTrue(@lock.IsUpgradeableReadLockHeld); + Assert.IsTrue(holder.LockAcquired); + } + } + } + } +#endif +} File [added]: SlimReadWriteLockTestCase.cs Delta lines: +0 -0 =================================================================== Directory: /Core/trunk/src/Castle.Core/Internal/ ================================================ File [added]: ILockHolder.cs Delta lines: +22 -0 =================================================================== --- Core/trunk/src/Castle.Core/Internal/IUpgradeableLockHolder.cs (rev 0) +++ Core/trunk/src/Castle.Core/Internal/IUpgradeableLockHolder.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -0,0 +1,22 @@ +// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Castle.Core.Internal +{ + public interface IUpgradeableLockHolder : ILockHolder + { + ILockHolder Upgrade(); + ILockHolder Upgrade(bool waitForLock); + } +} File [added]: IUpgradeableLockHolder.cs Delta lines: +35 -0 =================================================================== --- Core/trunk/src/Castle.Core/Internal/Lock.cs (rev 0) +++ Core/trunk/src/Castle.Core/Internal/Lock.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -0,0 +1,36 @@ +// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Castle.Core.Internal +{ + public abstract class Lock + { + public abstract IUpgradeableLockHolder ForReadingUpgradeable(); + public abstract ILockHolder ForReading(); + public abstract ILockHolder ForWriting(); + + public abstract IUpgradeableLockHolder ForReadingUpgradeable(bool waitForLock); + public abstract ILockHolder ForReading(bool waitForLock); + public abstract ILockHolder ForWriting(bool waitForLock); + + public static Lock Create() + { +#if SILVERLIGHT || !DOTNET35 + return new MonitorUser(); +#else + return new SlimReadWriteLock(); +#endif + } + } File [added]: Lock.cs Delta lines: +51 -0 =================================================================== --- Core/trunk/src/Castle.Core/Internal/MonitorLock.cs (rev 0) +++ Core/trunk/src/Castle.Core/Internal/MonitorLock.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -0,0 +1,51 @@ +// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Castle.Core.Internal +{ + internal class MonitorLock : Lock + { + private readonly object locker = new object(); + + public override IUpgradeableLockHolder ForReadingUpgradeable() + { + return ForReadingUpgradeable(true); + } + + public override ILockHolder ForReading() + { + return ForReading(true); + } + + public override ILockHolder ForWriting() + { + return ForWriting(true); + } + + public override IUpgradeableLockHolder ForReadingUpgradeable(bool waitForLock) + { + return new MonitorUpgradeableLockHolder(locker, waitForLock); + } + + public override ILockHolder ForReading(bool waitForLock) + { + return new MonitorLockHolder(locker, waitForLock); + } + + public override ILockHolder ForWriting(bool waitForLock) + { + return new MonitorLockHolder(locker, waitForLock); + } + } +} File [added]: MonitorLock.cs Delta lines: +49 -0 =================================================================== --- Core/trunk/src/Castle.Core/Internal/MonitorLockHolder.cs (rev 0) +++ Core/trunk/src/Castle.Core/Internal/MonitorLockHolder.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -0,0 +1,49 @@ +// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Castle.Core.Internal +{ + using System.Threading; + + internal class MonitorLockHolder : ILockHolder + { + private readonly object locker; + private bool lockAcquired; + + public MonitorLockHolder(object locker, bool waitForLock) + { + this.locker = locker; + if(waitForLock) + { + Monitor.Enter(locker); + lockAcquired = true; + return; + } + + lockAcquired = Monitor.TryEnter(locker, 0); + } + + public void Dispose() + { + if (!LockAcquired) return; + Monitor.Exit(locker); + lockAcquired = false; + } + + public bool LockAcquired + { + get { return lockAcquired; } + } + } +} File [added]: MonitorLockHolder.cs Delta lines: +58 -0 =================================================================== --- Core/trunk/src/Castle.Core/Internal/MonitorUpgradeableLockHolder.cs (rev 0) +++ Core/trunk/src/Castle.Core/Internal/MonitorUpgradeableLockHolder.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -0,0 +1,58 @@ +// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Castle.Core.Internal +{ + using System.Threading; + + internal class MonitorUpgradeableLockHolder : IUpgradeableLockHolder + { + private readonly object locker; + private bool lockAcquired; + + public MonitorUpgradeableLockHolder(object locker, bool waitForLock) + { + this.locker = locker; + if(waitForLock) + { + Monitor.Enter(locker); + lockAcquired = true; + return; + } + lockAcquired = Monitor.TryEnter(locker, 0); + } + + public void Dispose() + { + if (!LockAcquired) return; + Monitor.Exit(locker); + lockAcquired = false; + } + + public ILockHolder Upgrade() + { + return NoOpLock.Lock; + } + + public ILockHolder Upgrade(bool waitForLock) + { + return NoOpLock.Lock; + } + + public bool LockAcquired + { + get { return lockAcquired; } + } + } +} File [added]: MonitorUpgradeableLockHolder.cs Delta lines: +33 -0 =================================================================== --- Core/trunk/src/Castle.Core/Internal/NoOpLock.cs (rev 0) +++ Core/trunk/src/Castle.Core/Internal/NoOpLock.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -0,0 +1,33 @@ +// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Castle.Core.Internal +{ + using System; + + internal class NoOpLock:ILockHolder + { + public static readonly ILockHolder Lock = new NoOpLock(); + + public void Dispose() + { + + } + + public bool LockAcquired + { + get { return true; } + } + } +} File [added]: NoOpLock.cs Delta lines: +41 -0 =================================================================== --- Core/trunk/src/Castle.Core/Internal/NoOpUpgradeableLock.cs (rev 0) +++ Core/trunk/src/Castle.Core/Internal/NoOpUpgradeableLock.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -0,0 +1,41 @@ +// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Castle.Core.Internal +{ + internal class NoOpUpgradeableLock : IUpgradeableLockHolder + { + public static readonly IUpgradeableLockHolder Lock = new NoOpUpgradeableLock(); + + public void Dispose() + { + + } + + public bool LockAcquired + { + get { return true; } + } + + public ILockHolder Upgrade() + { + return NoOpLock.Lock; + } + + public ILockHolder Upgrade(bool waitForLock) + { + return NoOpLock.Lock; + } + } +} File [added]: NoOpUpgradeableLock.cs Delta lines: +0 -33 =================================================================== --- Core/trunk/src/Castle.Core/Internal/ReadLock.cs 2009-12-19 15:47:58 UTC (rev 6468) +++ Core/trunk/src/Castle.Core/Internal/ReadLock.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -1,34 +0,0 @@ -// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -namespace Castle.Core.Internal -{ - using System; - - public struct ReadLock : IDisposable - { - private readonly SlimReaderWriterLock locker; - - public ReadLock(SlimReaderWriterLock locker) - { - this.locker = locker; - locker.EnterReadLock(); - } - - public void Dispose() - { - locker.ExitReadLock(); - } - } File [removed]: ReadLock.cs Delta lines: +51 -0 =================================================================== --- Core/trunk/src/Castle.Core/Internal/SlimReadLockHolder.cs (rev 0) +++ Core/trunk/src/Castle.Core/Internal/SlimReadLockHolder.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -0,0 +1,51 @@ +// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Castle.Core.Internal +{ + using System.Threading; + +#if DOTNET35 && !SILVERLIGHT + + internal class SlimReadLockHolder : ILockHolder + { + private readonly ReaderWriterLockSlim locker; + private bool lockAcquired; + + public SlimReadLockHolder(ReaderWriterLockSlim locker, bool waitForLock) + { + this.locker = locker; + if(waitForLock) + { + locker.EnterReadLock(); + lockAcquired = true; + return; + } + lockAcquired = locker.TryEnterReadLock(0); + } + + public void Dispose() + { + if (!LockAcquired) return; + locker.ExitReadLock(); + lockAcquired = false; + } + + public bool LockAcquired + { + get { return lockAcquired; } + } + } +#endif +} File [added]: SlimReadLockHolder.cs Delta lines: +82 -0 =================================================================== --- Core/trunk/src/Castle.Core/Internal/SlimReadWriteLock.cs (rev 0) +++ Core/trunk/src/Castle.Core/Internal/SlimReadWriteLock.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -0,0 +1,82 @@ +// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Castle.Core.Internal +{ + using System.Threading; + +#if DOTNET35 && !SILVERLIGHT + + internal class SlimReadWriteLock : Lock + { + private readonly ReaderWriterLockSlim locker = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion); + + public override IUpgradeableLockHolder ForReadingUpgradeable() + { + return ForReadingUpgradeable(true); + } + + public override ILockHolder ForReading() + { + return ForReading(true); + } + + public override ILockHolder ForWriting() + { + return ForWriting(true); + } + + public override IUpgradeableLockHolder ForReadingUpgradeable(bool waitForLock) + { + return new SlimUpgradeableReadLockHolder(locker, waitForLock, locker.IsUpgradeableReadLockHeld || locker.IsWriteLockHeld); + } + + public override ILockHolder ForReading(bool waitForLock) + { + if (locker.IsReadLockHeld || locker.IsUpgradeableReadLockHeld || locker.IsWriteLockHeld) + { + return NoOpLock.Lock; + } + + return new SlimReadLockHolder(locker, waitForLock); + } + + public override ILockHolder ForWriting(bool waitForLock) + { + if (locker.IsWriteLockHeld) + { + return NoOpLock.Lock; + } + + return new SlimWriteLockHolder(locker, waitForLock); + } + + public bool IsReadLockHeld + { + get { return locker.IsReadLockHeld; } + } + + public bool IsUpgradeableReadLockHeld + { + get { return locker.IsUpgradeableReadLockHeld; } + } + + public bool IsWriteLockHeld + { + get { return locker.IsWriteLockHeld; } + } + } + +#endif +} File [added]: SlimReadWriteLock.cs Delta lines: +0 -105 =================================================================== --- Core/trunk/src/Castle.Core/Internal/SlimReaderWriterLock.cs 2009-12-19 15:47:58 UTC (rev 6468) +++ Core/trunk/src/Castle.Core/Internal/SlimReaderWriterLock.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -1,106 +0,0 @@ -// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -namespace Castle.Core.Internal -{ - using System.Threading; - - public class SlimReaderWriterLock - { -#if SILVERLIGHT || !DOTNET35 - private readonly object locker = new object(); - - public void EnterReadLock() - { - Monitor.Enter(locker); - } - - public bool TryEnterReadLock() - { - return Monitor.TryEnter(locker,0); - } - - public void EnterWriteLock() - { - Monitor.Enter(locker); - } - - public bool TryEnterWriteLock() - { - return Monitor.TryEnter(locker,0); - } - - public void ExitReadLock() - { - Monitor.Exit(locker); - } - - public void ExitWriteLock() - { - Monitor.Exit(locker); - } - - public void EnterUpgradeableReadLock() - { - EnterWriteLock(); - } - - public void ExitUpgradeableReadLock() - { - ExitWriteLock(); - } - -#else - private readonly ReaderWriterLockSlim locker = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion); - - public void EnterReadLock() - { - locker.EnterReadLock(); - } - public bool TryEnterReadLock() - { - return locker.TryEnterReadLock(0); - } - - public void EnterWriteLock() - { - locker.EnterWriteLock(); - } - public bool TryEnterWriteLock() - { - return locker.TryEnterWriteLock(0); - } - - public void EnterUpgradeableReadLock() - { - locker.EnterUpgradeableReadLock(); - } - - public void ExitReadLock() - { - locker.ExitReadLock(); - } - - public void ExitWriteLock() - { - locker.ExitWriteLock(); - } - - public void ExitUpgradeableReadLock() - { - locker.ExitUpgradeableReadLock(); - } -#endif - } File [removed]: SlimReaderWriterLock.cs Delta lines: +86 -0 =================================================================== --- Core/trunk/src/Castle.Core/Internal/SlimUpgradeableReadLockHolder.cs (rev 0) +++ Core/trunk/src/Castle.Core/Internal/SlimUpgradeableReadLockHolder.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -0,0 +1,86 @@ +// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Castle.Core.Internal +{ + using System.Threading; + +#if DOTNET35 && !SILVERLIGHT + + internal class SlimUpgradeableReadLockHolder : IUpgradeableLockHolder + { + private readonly ReaderWriterLockSlim locker; + private bool lockAcquired; + private SlimWriteLockHolder writerLock; + private bool wasLockAlreadyHeld; + + public SlimUpgradeableReadLockHolder(ReaderWriterLockSlim locker, bool waitForLock, bool wasLockAlreadyHelf) + { + this.locker = locker; + if (wasLockAlreadyHelf) + { + lockAcquired = true; + wasLockAlreadyHeld = true; + return; + } + + if(waitForLock) + { + locker.EnterUpgradeableReadLock(); + lockAcquired = true; + return; + } + + lockAcquired = locker.TryEnterUpgradeableReadLock(0); + } + + public void Dispose() + { + if (writerLock != null && writerLock.LockAcquired) + { + writerLock.Dispose(); + writerLock = null; + } + if (!LockAcquired) return; + if (!wasLockAlreadyHeld) + { + locker.ExitUpgradeableReadLock(); + } + lockAcquired = false; + + } + + public ILockHolder Upgrade() + { + return Upgrade(true); + } + + public ILockHolder Upgrade(bool waitForLock) + { + if(locker.IsWriteLockHeld) + { + return NoOpLock.Lock; + } + + writerLock = new SlimWriteLockHolder(locker, waitForLock); + return writerLock; + } + + public bool LockAcquired + { + get { return lockAcquired; } + } + } +#endif +} File [added]: SlimUpgradeableReadLockHolder.cs Delta lines: +51 -0 =================================================================== --- Core/trunk/src/Castle.Core/Internal/SlimWriteLockHolder.cs (rev 0) +++ Core/trunk/src/Castle.Core/Internal/SlimWriteLockHolder.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -0,0 +1,51 @@ +// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Castle.Core.Internal +{ + using System.Threading; + +#if DOTNET35 && !SILVERLIGHT + internal class SlimWriteLockHolder : ILockHolder + { + private readonly ReaderWriterLockSlim locker; + + private bool lockAcquired; + + public SlimWriteLockHolder(ReaderWriterLockSlim locker, bool waitForLock) + { + this.locker = locker; + if(waitForLock) + { + locker.EnterWriteLock(); + lockAcquired = true; + return; + } + lockAcquired = locker.TryEnterWriteLock(0); + } + + public void Dispose() + { + if(!LockAcquired) return; + locker.ExitWriteLock(); + lockAcquired = false; + } + + public bool LockAcquired + { + get { return lockAcquired; } + } + } +#endif +} File [added]: SlimWriteLockHolder.cs Delta lines: +0 -46 =================================================================== --- Core/trunk/src/Castle.Core/Internal/UpgradableLock.cs 2009-12-19 15:47:58 UTC (rev 6468) +++ Core/trunk/src/Castle.Core/Internal/UpgradableLock.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -1,47 +0,0 @@ -// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -namespace Castle.Core.Internal -{ - using System; - - public struct UpgradableLock : IDisposable - { - private readonly SlimReaderWriterLock locker; - private bool lockWasUpgraded; - - public UpgradableLock(SlimReaderWriterLock locker) - { - this.locker = locker; - locker.EnterUpgradeableReadLock(); - lockWasUpgraded = false; - } - - public void Upgrade() - { - locker.EnterWriteLock(); - lockWasUpgraded = true; - } - - public void Dispose() - { - if (lockWasUpgraded) - { - locker.ExitWriteLock(); - } - - locker.ExitUpgradeableReadLock(); - } - } File [removed]: UpgradableLock.cs Delta lines: +0 -33 =================================================================== --- Core/trunk/src/Castle.Core/Internal/WriteLock.cs 2009-12-19 15:47:58 UTC (rev 6468) +++ Core/trunk/src/Castle.Core/Internal/WriteLock.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -1,34 +0,0 @@ -// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -namespace Castle.Core.Internal -{ - using System; - - public struct WriteLock : IDisposable - { - private readonly SlimReaderWriterLock locker; - - public WriteLock(SlimReaderWriterLock locker) - { - this.locker = locker; - locker.EnterWriteLock(); - } - - public void Dispose() - { - locker.ExitWriteLock(); - } - } File [removed]: WriteLock.cs Delta lines: +16 -0 =================================================================== --- Core/trunk/src/Castle.Core/InternalsVisibleToTests.cs (rev 0) +++ Core/trunk/src/Castle.Core/InternalsVisibleToTests.cs 2009-12-19 22:15:33 UTC (rev 6469) @@ -0,0 +1,17 @@ +// Copyright 2004-2009 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System.Runtime.CompilerServices; + -- You received this message because you are subscribed to the Google Groups "Castle Project Commits" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/castle-project-commits?hl=en.
