Skip to content

Commit 240bf02

Browse files
committed
Moved Castle.Core Lock into Castle.Core.Internal.Locking
castleproject#565
1 parent e6a1963 commit 240bf02

19 files changed

+399
-13
lines changed

src/Castle.Facilities.WcfIntegration/Client/Discovery/DiscoveryProxyCache.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Castle.Facilities.WcfIntegration
1616
{
1717
using System;
1818
using System.ServiceModel.Discovery;
19-
using Castle.Core.Internal;
19+
using Castle.Core.Internal.Locking;
2020

2121
public class DiscoveryProxyCache : DiscoveryEndpointProvider, IDisposable
2222
{

src/Castle.Facilities.WcfIntegration/Client/Proxy/WcfChannelHolder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Castle.Facilities.WcfIntegration
2222
using System.ServiceModel;
2323
using System.ServiceModel.Channels;
2424
using System.Threading;
25-
using Castle.Core.Internal;
25+
using Castle.Core.Internal.Locking;
2626
using Castle.Facilities.WcfIntegration.Internal;
2727

2828
public class WcfChannelHolder : IWcfChannelHolder

src/Castle.Facilities.WcfIntegration/Service/Discovery/LoadBalance/ListBasedLoadBalancePolicy.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Castle.Facilities.WcfIntegration
1717
using System;
1818
using System.Collections.Generic;
1919
using System.ServiceModel.Discovery;
20-
using Castle.Core.Internal;
20+
using Castle.Core.Internal.Locking;
2121

2222
public delegate bool PolicyMembership(EndpointDiscoveryMetadata endpoint);
2323

src/Castle.Facilities.WcfIntegration/Service/Discovery/ServiceCatalog/InMemoryServiceCatalog.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace Castle.Facilities.WcfIntegration
2020
using System.ServiceModel.Discovery;
2121

2222
using Castle.Core;
23-
using Castle.Core.Internal;
23+
using Castle.Core.Internal.Locking;
2424
using System;
2525

2626
public class InMemoryServiceCatalog : IServiceCatalogImplementation
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace Castle.Core.Internal.Locking
16+
{
17+
using System;
18+
19+
public interface ILockHolder:IDisposable
20+
{
21+
bool LockAcquired { get; }
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace Castle.Core.Internal.Locking
16+
{
17+
using System;
18+
using System.ComponentModel;
19+
20+
[EditorBrowsable(EditorBrowsableState.Never)]
21+
public interface IUpgradeableLockHolder : ILockHolder
22+
{
23+
ILockHolder Upgrade();
24+
ILockHolder Upgrade(bool waitForLock);
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace Castle.Core.Internal.Locking
16+
{
17+
public abstract class Lock
18+
{
19+
public abstract IUpgradeableLockHolder ForReadingUpgradeable();
20+
public abstract ILockHolder ForReading();
21+
public abstract ILockHolder ForWriting();
22+
23+
public abstract IUpgradeableLockHolder ForReadingUpgradeable(bool waitForLock);
24+
public abstract ILockHolder ForReading(bool waitForLock);
25+
public abstract ILockHolder ForWriting(bool waitForLock);
26+
27+
/// <summary>
28+
/// Creates a new lock.
29+
/// </summary>
30+
/// <returns></returns>
31+
public static Lock Create()
32+
{
33+
return new SlimReadWriteLock();
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2004-2010 Castle Project - http://www.castleproject.org/
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace Castle.Core.Internal.Locking
16+
{
17+
using System.ComponentModel;
18+
19+
[EditorBrowsable(EditorBrowsableState.Never)]
20+
internal class NoOpLock : ILockHolder
21+
{
22+
public static readonly ILockHolder Lock = new NoOpLock();
23+
24+
public void Dispose()
25+
{
26+
27+
}
28+
29+
public bool LockAcquired
30+
{
31+
get { return true; }
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace Castle.Core.Internal.Locking
16+
{
17+
using System;
18+
using System.ComponentModel;
19+
using System.Threading;
20+
21+
[EditorBrowsable(EditorBrowsableState.Never)]
22+
internal class SlimReadLockHolder : ILockHolder
23+
{
24+
private readonly ReaderWriterLockSlim locker;
25+
private bool lockAcquired;
26+
27+
public SlimReadLockHolder(ReaderWriterLockSlim locker, bool waitForLock)
28+
{
29+
this.locker = locker;
30+
if(waitForLock)
31+
{
32+
locker.EnterReadLock();
33+
lockAcquired = true;
34+
return;
35+
}
36+
lockAcquired = locker.TryEnterReadLock(0);
37+
}
38+
39+
public void Dispose()
40+
{
41+
if (!LockAcquired) return;
42+
locker.ExitReadLock();
43+
lockAcquired = false;
44+
}
45+
46+
public bool LockAcquired
47+
{
48+
get { return lockAcquired; }
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace Castle.Core.Internal.Locking
16+
{
17+
using System.Threading;
18+
19+
internal class SlimReadWriteLock : Lock
20+
{
21+
private readonly ReaderWriterLockSlim locker = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
22+
23+
public override IUpgradeableLockHolder ForReadingUpgradeable()
24+
{
25+
return ForReadingUpgradeable(true);
26+
}
27+
28+
public override ILockHolder ForReading()
29+
{
30+
return ForReading(true);
31+
}
32+
33+
public override ILockHolder ForWriting()
34+
{
35+
return ForWriting(true);
36+
}
37+
38+
public override IUpgradeableLockHolder ForReadingUpgradeable(bool waitForLock)
39+
{
40+
return new SlimUpgradeableReadLockHolder(locker, waitForLock, locker.IsUpgradeableReadLockHeld || locker.IsWriteLockHeld);
41+
}
42+
43+
public override ILockHolder ForReading(bool waitForLock)
44+
{
45+
if (locker.IsReadLockHeld || locker.IsUpgradeableReadLockHeld || locker.IsWriteLockHeld)
46+
{
47+
return NoOpLock.Lock;
48+
}
49+
50+
return new SlimReadLockHolder(locker, waitForLock);
51+
}
52+
53+
public override ILockHolder ForWriting(bool waitForLock)
54+
{
55+
if (locker.IsWriteLockHeld)
56+
{
57+
return NoOpLock.Lock;
58+
}
59+
60+
return new SlimWriteLockHolder(locker, waitForLock);
61+
}
62+
63+
public bool IsReadLockHeld
64+
{
65+
get { return locker.IsReadLockHeld; }
66+
}
67+
68+
public bool IsUpgradeableReadLockHeld
69+
{
70+
get { return locker.IsUpgradeableReadLockHeld; }
71+
}
72+
73+
public bool IsWriteLockHeld
74+
{
75+
get { return locker.IsWriteLockHeld; }
76+
}
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace Castle.Core.Internal.Locking
16+
{
17+
using System.Threading;
18+
19+
internal class SlimUpgradeableReadLockHolder : IUpgradeableLockHolder
20+
{
21+
private readonly ReaderWriterLockSlim locker;
22+
private bool lockAcquired;
23+
private SlimWriteLockHolder writerLock;
24+
private bool wasLockAlreadyHeld;
25+
26+
public SlimUpgradeableReadLockHolder(ReaderWriterLockSlim locker, bool waitForLock, bool wasLockAlreadyHelf)
27+
{
28+
this.locker = locker;
29+
if (wasLockAlreadyHelf)
30+
{
31+
lockAcquired = true;
32+
wasLockAlreadyHeld = true;
33+
return;
34+
}
35+
36+
if(waitForLock)
37+
{
38+
locker.EnterUpgradeableReadLock();
39+
lockAcquired = true;
40+
return;
41+
}
42+
43+
lockAcquired = locker.TryEnterUpgradeableReadLock(0);
44+
}
45+
46+
public void Dispose()
47+
{
48+
if (writerLock != null && writerLock.LockAcquired)
49+
{
50+
writerLock.Dispose();
51+
writerLock = null;
52+
}
53+
if (!LockAcquired) return;
54+
if (!wasLockAlreadyHeld)
55+
{
56+
locker.ExitUpgradeableReadLock();
57+
}
58+
lockAcquired = false;
59+
60+
}
61+
62+
public ILockHolder Upgrade()
63+
{
64+
return Upgrade(true);
65+
}
66+
67+
public ILockHolder Upgrade(bool waitForLock)
68+
{
69+
if(locker.IsWriteLockHeld)
70+
{
71+
return NoOpLock.Lock;
72+
}
73+
74+
writerLock = new SlimWriteLockHolder(locker, waitForLock);
75+
return writerLock;
76+
}
77+
78+
public bool LockAcquired
79+
{
80+
get { return lockAcquired; }
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)