Skip to content

Commit 7ccd6a4

Browse files
committed
Improve acquireLock
1 parent 0a9075e commit 7ccd6a4

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

dbms/src/main/java/org/polypheny/db/transaction/locking/LockManager.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616

1717
package org.polypheny.db.transaction.locking;
1818

19-
import lombok.extern.slf4j.Slf4j;
20-
import org.polypheny.db.catalog.exceptions.GenericRuntimeException;
21-
import org.polypheny.db.transaction.Transaction;
22-
import org.polypheny.db.transaction.locking.Lockable.LockType;
23-
import org.polypheny.db.util.DeadlockException;
2419
import java.util.HashSet;
2520
import java.util.Map;
2621
import java.util.Set;
@@ -31,6 +26,11 @@
3126
import java.util.concurrent.locks.Lock;
3227
import java.util.concurrent.locks.ReentrantLock;
3328
import java.util.stream.Collectors;
29+
import lombok.extern.slf4j.Slf4j;
30+
import org.polypheny.db.catalog.exceptions.GenericRuntimeException;
31+
import org.polypheny.db.transaction.Transaction;
32+
import org.polypheny.db.transaction.locking.Lockable.LockType;
33+
import org.polypheny.db.util.DeadlockException;
3434

3535
@Slf4j
3636
public class LockManager {
@@ -46,7 +46,7 @@ public class LockManager {
4646
private final Map<Transaction, Lockable> waitFor = new ConcurrentHashMap<>();
4747

4848

49-
record LockEntry( Transaction transaction, Lockable lockable, LockType lockType ) {
49+
public record LockEntry( Transaction transaction, Lockable lockable, LockType lockType ) {
5050

5151
}
5252

@@ -135,12 +135,16 @@ private boolean acquireLock( Transaction transaction, Lockable lockable, LockTyp
135135

136136
Set<LockEntry> newLocks = tryAcquireLock( locks, entry );
137137

138-
if ( newLocks != null && (locks == newLocks || entries.compareAndSet( locks, newLocks )) ) {// TODO: Use equal
139-
Lockable l = waitFor.remove( transaction ); // No longer waiting
140-
if ( l != null && l != lockable ) {
141-
throw new AssertionError( "Wrong lockable" );
138+
if ( newLocks != null ) {
139+
if ( entries.compareAndSet( locks, newLocks ) ) {
140+
Lockable l = waitFor.remove( transaction ); // No longer waiting
141+
if ( l != null && l != lockable ) {
142+
throw new AssertionError( "Wrong lockable" );
143+
}
144+
return true;
145+
} else {
146+
return false; // Try again, someone else has acquired a lock
142147
}
143-
return true;
144148
} else {
145149
waitFor.put( transaction, lockable ); // TODO: Two transactions could abort at the same time
146150
// If there is a Deadlock, this means that other Transactions are already waiting for us, so the relevant parts won't change

0 commit comments

Comments
 (0)