Skip to content

Commit a3635db

Browse files
bahusoidfredericDelaporte
authored andcommitted
Avoid allocations on lock in SyncCacheLock
1 parent 2b47d3e commit a3635db

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/NHibernate/Cache/SyncCacheLock.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,46 @@ namespace NHibernate.Cache
66
{
77
class SyncCacheLock : ICacheLock
88
{
9+
private readonly MonitorLock _monitorLock;
10+
911
class MonitorLock : IDisposable
1012
{
1113
private readonly object _lockObj;
1214

1315
public MonitorLock(object lockObj)
1416
{
15-
Monitor.Enter(lockObj);
1617
_lockObj = lockObj;
1718
}
1819

20+
public IDisposable Lock()
21+
{
22+
Monitor.Enter(_lockObj);
23+
return this;
24+
}
25+
1926
public void Dispose()
2027
{
2128
Monitor.Exit(_lockObj);
2229
}
2330
}
2431

32+
public SyncCacheLock()
33+
{
34+
_monitorLock = new MonitorLock(this);
35+
}
36+
2537
public void Dispose()
2638
{
2739
}
2840

2941
public IDisposable ReadLock()
3042
{
31-
return new MonitorLock(this);
43+
return _monitorLock.Lock();
3244
}
3345

3446
public IDisposable WriteLock()
3547
{
36-
return new MonitorLock(this);
48+
return _monitorLock.Lock();
3749
}
3850

3951
public Task<IDisposable> ReadLockAsync()

0 commit comments

Comments
 (0)