提交 9caa3a87 authored 作者: Noel Grandin's avatar Noel Grandin

fix TestMultiThreadedKernel in pagestore mode

by essentially copying over various fixes we made, from the MVStore class, to the RegularTable class
上级 3bb5aa60
...@@ -449,7 +449,7 @@ public class MVTable extends TableBase { ...@@ -449,7 +449,7 @@ public class MVTable extends TableBase {
} }
} }
synchronized (getLockSyncObject()) { synchronized (getLockSyncObject()) {
if (lockSharedSessions.size() > 0) { if (!lockSharedSessions.isEmpty()) {
lockSharedSessions.remove(s); lockSharedSessions.remove(s);
if (SysProperties.THREAD_DEADLOCK_DETECTOR) { if (SysProperties.THREAD_DEADLOCK_DETECTOR) {
if (SHARED_LOCKS.get() != null) { if (SHARED_LOCKS.get() != null) {
......
...@@ -11,8 +11,8 @@ import java.util.Collections; ...@@ -11,8 +11,8 @@ import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.h2.api.DatabaseEventListener; import org.h2.api.DatabaseEventListener;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.command.ddl.CreateTableData; import org.h2.command.ddl.CreateTableData;
...@@ -55,7 +55,10 @@ public class RegularTable extends TableBase { ...@@ -55,7 +55,10 @@ public class RegularTable extends TableBase {
private Index scanIndex; private Index scanIndex;
private long rowCount; private long rowCount;
private volatile Session lockExclusiveSession; private volatile Session lockExclusiveSession;
private HashSet<Session> lockSharedSessions = new HashSet<>();
// using a ConcurrentHashMap as a set
private ConcurrentHashMap<Session, Session> lockSharedSessions =
new ConcurrentHashMap<>();
/** /**
* The queue of sessions waiting to lock the table. It is a FIFO queue to * The queue of sessions waiting to lock the table. It is a FIFO queue to
...@@ -434,11 +437,6 @@ public class RegularTable extends TableBase { ...@@ -434,11 +437,6 @@ public class RegularTable extends TableBase {
session.markTableForAnalyze(this); session.markTableForAnalyze(this);
} }
@Override
public boolean isLockedExclusivelyBy(Session session) {
return lockExclusiveSession == session;
}
@Override @Override
public boolean lock(Session session, boolean exclusive, public boolean lock(Session session, boolean exclusive,
boolean forceLockEvenInMvcc) { boolean forceLockEvenInMvcc) {
...@@ -460,10 +458,10 @@ public class RegularTable extends TableBase { ...@@ -460,10 +458,10 @@ public class RegularTable extends TableBase {
if (lockExclusiveSession == session) { if (lockExclusiveSession == session) {
return true; return true;
} }
if (!exclusive && lockSharedSessions.containsKey(session)) {
return true;
}
synchronized (database) { synchronized (database) {
if (lockExclusiveSession == session) {
return true;
}
if (!exclusive && lockSharedSessions.contains(session)) { if (!exclusive && lockSharedSessions.contains(session)) {
return true; return true;
} }
...@@ -543,7 +541,7 @@ public class RegularTable extends TableBase { ...@@ -543,7 +541,7 @@ public class RegularTable extends TableBase {
lockExclusiveSession = session; lockExclusiveSession = session;
return true; return true;
} else if (lockSharedSessions.size() == 1 && } else if (lockSharedSessions.size() == 1 &&
lockSharedSessions.contains(session)) { lockSharedSessions.containsKey(session)) {
traceLock(session, exclusive, "add (upgraded) for "); traceLock(session, exclusive, "add (upgraded) for ");
lockExclusiveSession = session; lockExclusiveSession = session;
return true; return true;
...@@ -562,10 +560,10 @@ public class RegularTable extends TableBase { ...@@ -562,10 +560,10 @@ public class RegularTable extends TableBase {
return true; return true;
} }
} }
if (!lockSharedSessions.contains(session)) { if (!lockSharedSessions.containsKey(session)) {
traceLock(session, exclusive, "ok"); traceLock(session, exclusive, "ok");
session.addLock(this); session.addLock(this);
lockSharedSessions.add(session); lockSharedSessions.put(session, session);
} }
return true; return true;
} }
...@@ -626,7 +624,7 @@ public class RegularTable extends TableBase { ...@@ -626,7 +624,7 @@ public class RegularTable extends TableBase {
} }
visited.add(session); visited.add(session);
ArrayList<Session> error = null; ArrayList<Session> error = null;
for (Session s : lockSharedSessions) { for (Session s : lockSharedSessions.keySet()) {
if (s == session) { if (s == session) {
// it doesn't matter if we have locked the object already // it doesn't matter if we have locked the object already
continue; continue;
...@@ -640,10 +638,13 @@ public class RegularTable extends TableBase { ...@@ -640,10 +638,13 @@ public class RegularTable extends TableBase {
} }
} }
} }
if (error == null && lockExclusiveSession != null) { // take a local copy so we don't see inconsistent data, since we are
Table t = lockExclusiveSession.getWaitForLock(); // not locked while checking the lockExclusiveSession value
Session copyOfLockExclusiveSession = lockExclusiveSession;
if (error == null && copyOfLockExclusiveSession != null) {
Table t = copyOfLockExclusiveSession.getWaitForLock();
if (t != null) { if (t != null) {
error = t.checkDeadlock(lockExclusiveSession, clash, visited); error = t.checkDeadlock(copyOfLockExclusiveSession, clash, visited);
if (error != null) { if (error != null) {
error.add(session); error.add(session);
} }
...@@ -665,11 +666,17 @@ public class RegularTable extends TableBase { ...@@ -665,11 +666,17 @@ public class RegularTable extends TableBase {
return lockExclusiveSession != null; return lockExclusiveSession != null;
} }
@Override
public boolean isLockedExclusivelyBy(Session session) {
return lockExclusiveSession == session;
}
@Override @Override
public void unlock(Session s) { public void unlock(Session s) {
if (database != null) { if (database != null) {
traceLock(s, lockExclusiveSession == s, "unlock"); traceLock(s, lockExclusiveSession == s, "unlock");
if (lockExclusiveSession == s) { if (lockExclusiveSession == s) {
lockSharedSessions.remove(s);
lockExclusiveSession = null; lockExclusiveSession = null;
} }
synchronized (database) { synchronized (database) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论