Skip to content

Commit ab01f42

Browse files
committed
Fixed concurrency issues in cur & tran managers
Closes #38
1 parent 8b9cabd commit ab01f42

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

src/LightningDB/Factories/CursorManager.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -9,17 +10,16 @@ namespace LightningDB.Factories
910
class CursorManager
1011
{
1112
private readonly LightningTransaction _transaction;
12-
13-
private readonly HashSet<IntPtr> _cursors;
14-
13+
private readonly ConcurrentDictionary<IntPtr, bool> _cursors;
14+
1515
public CursorManager(LightningTransaction transaction)
1616
{
1717
if (transaction == null)
1818
throw new ArgumentNullException("transaction");
1919

2020
_transaction = transaction;
2121

22-
_cursors = new HashSet<IntPtr>();
22+
_cursors = new ConcurrentDictionary<IntPtr, bool>();
2323
}
2424

2525
private IntPtr CreateCursorHandle(uint dbHandle)
@@ -38,7 +38,7 @@ private void CloseCursor(IntPtr cursorHandle)
3838
public LightningCursor OpenCursor(LightningDatabase db)
3939
{
4040
var handle = CreateCursorHandle(db._handle);
41-
_cursors.Add(handle);
41+
_cursors.TryAdd(handle, true);
4242

4343
return new LightningCursor(db, _transaction, handle);
4444
}
@@ -51,14 +51,15 @@ public void CloseCursor(LightningCursor cursor)
5151
}
5252
finally
5353
{
54-
_cursors.Remove(cursor._handle);
54+
bool value;
55+
_cursors.TryRemove(cursor._handle, out value);
5556
}
5657
}
5758

5859
public void CloseAll()
5960
{
60-
foreach (var handle in _cursors)
61-
CloseCursor(handle);
61+
foreach (var p in _cursors)
62+
CloseCursor(p.Key);
6263

6364
_cursors.Clear();
6465
}

src/LightningDB/Factories/TransactionManager.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -12,7 +13,7 @@ class TransactionManager
1213
private readonly LightningTransaction _parentTransaction;
1314
private readonly IntPtr _parentHandle;
1415

15-
private readonly HashSet<LightningTransaction> _transactions;
16+
private readonly ConcurrentDictionary<LightningTransaction, bool> _transactions;
1617

1718
public TransactionManager(LightningEnvironment environment, LightningTransaction parentTransaction)
1819
{
@@ -26,7 +27,7 @@ public TransactionManager(LightningEnvironment environment, LightningTransaction
2627
? parentTransaction._handle
2728
: IntPtr.Zero;
2829

29-
_transactions = new HashSet<LightningTransaction>();
30+
_transactions = new ConcurrentDictionary<LightningTransaction, bool>();
3031
}
3132

3233
private void EnsureEnvironmentOpened()
@@ -44,23 +45,24 @@ public LightningTransaction Create(TransactionBeginFlags beginFlags)
4445

4546
var tran = new LightningTransaction(_environment, handle, _parentTransaction, beginFlags);
4647

47-
_transactions.Add(tran);
48+
_transactions.TryAdd(tran, true);
4849

4950
return tran;
5051
}
5152

5253
public void WasDiscarded(LightningTransaction tn)
5354
{
54-
_transactions.Remove(tn);
55+
bool value;
56+
_transactions.TryRemove(tn, out value);
5557
}
5658

5759
private static void AbortAll(TransactionManager manager)
5860
{
5961

60-
foreach (var tn in manager._transactions.ToList())
62+
foreach (var p in manager._transactions.ToList())
6163
{
62-
AbortAll(tn.SubTransactionsManager);
63-
tn.Abort();
64+
AbortAll(p.Key.SubTransactionsManager);
65+
p.Key.Abort();
6466
}
6567
}
6668

0 commit comments

Comments
 (0)