Skip to content

Commit 003f313

Browse files
committed
Remove Lock from DefaultPool in benefit of ReaderWriterLockSlim
castleproject#565
1 parent e85de2c commit 003f313

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/Castle.Windsor/MicroKernel/Lifestyle/Pool/DefaultPool.cs

+14-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ namespace Castle.MicroKernel.Lifestyle.Pool
1616
{
1717
using System;
1818
using System.Collections.Generic;
19+
using System.Threading;
1920

2021
using Castle.Core;
21-
using Castle.Core.Internal;
2222
using Castle.MicroKernel.Context;
2323

2424
[Serializable]
@@ -29,7 +29,7 @@ public class DefaultPool : IPool, IDisposable
2929
private readonly Dictionary<object, Burden> inUse = new Dictionary<object, Burden>();
3030
private readonly int initialSize;
3131
private readonly int maxsize;
32-
private readonly Lock rwlock = Lock.Create();
32+
private readonly ReaderWriterLockSlim rwlock = new ReaderWriterLockSlim();
3333
private bool initialized;
3434

3535
public DefaultPool(int initialSize, int maxsize, IComponentActivator componentActivator)
@@ -54,7 +54,8 @@ public virtual void Dispose()
5454

5555
public virtual bool Release(object instance)
5656
{
57-
using (rwlock.ForWriting())
57+
rwlock.EnterWriteLock();
58+
try
5859
{
5960
Burden burden;
6061

@@ -85,6 +86,10 @@ public virtual bool Release(object instance)
8586
}
8687
}
8788
}
89+
finally
90+
{
91+
rwlock.ExitWriteLock();
92+
}
8893

8994
// Pool is full or has been disposed.
9095

@@ -95,7 +100,8 @@ public virtual bool Release(object instance)
95100
public virtual object Request(CreationContext context, Func<CreationContext, Burden> creationCallback)
96101
{
97102
Burden burden;
98-
using (rwlock.ForWriting())
103+
rwlock.EnterWriteLock();
104+
try
99105
{
100106
if (!initialized)
101107
{
@@ -124,6 +130,10 @@ public virtual object Request(CreationContext context, Func<CreationContext, Bur
124130
throw new PoolException("burden returned by creationCallback does not have root instance associated with it (its Instance property is null).");
125131
}
126132
}
133+
finally
134+
{
135+
rwlock.ExitWriteLock();
136+
}
127137
return burden.Instance;
128138
}
129139

0 commit comments

Comments
 (0)