提交 b196558d authored 作者: noelgrandin@gmail.com's avatar noelgrandin@gmail.com

some minor renaming and code movement around table locking to make it easier for…

some minor renaming and code movement around table locking to make it easier for me to follow the code
上级 de2dfbc4
......@@ -50,8 +50,8 @@ public class MVTable extends TableBase {
private MVPrimaryIndex primaryIndex;
private ArrayList<Index> indexes = New.arrayList();
private long lastModificationId;
private volatile Session lockExclusive;
private HashSet<Session> lockShared = New.hashSet();
private volatile Session lockExclusiveSession;
private HashSet<Session> lockSharedSessions = New.hashSet();
private final Trace traceLock;
private int changesSinceAnalyze;
private int nextAnalyze;
......@@ -99,12 +99,12 @@ public class MVTable extends TableBase {
}
@Override
public void lock(Session session, boolean exclusive, boolean force) {
public void lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) {
int lockMode = database.getLockMode();
if (lockMode == Constants.LOCK_MODE_OFF) {
return;
}
if (!force && database.isMultiVersion()) {
if (!forceLockEvenInMvcc && database.isMultiVersion()) {
// MVCC: update, delete, and insert use a shared lock.
// Select doesn't lock except when using FOR UPDATE and
// the system property h2.selectForUpdateMvcc
......@@ -112,15 +112,19 @@ public class MVTable extends TableBase {
if (exclusive) {
exclusive = false;
} else {
if (lockExclusive == null) {
if (lockExclusiveSession == null) {
return;
}
}
}
if (lockExclusive == session) {
if (lockExclusiveSession == session) {
return;
}
synchronized (database) {
if (lockExclusiveSession == session) {
return;
}
session.setWaitForLock(this, Thread.currentThread());
try {
doLock(session, lockMode, exclusive);
} finally {
......@@ -135,24 +139,21 @@ public class MVTable extends TableBase {
long max = 0;
boolean checkDeadlock = false;
while (true) {
if (lockExclusive == session) {
return;
}
if (exclusive) {
if (lockExclusive == null) {
if (lockShared.isEmpty()) {
if (lockExclusiveSession == null) {
if (lockSharedSessions.isEmpty()) {
traceLock(session, exclusive, "added for");
session.addLock(this);
lockExclusive = session;
lockExclusiveSession = session;
return;
} else if (lockShared.size() == 1 && lockShared.contains(session)) {
} else if (lockSharedSessions.size() == 1 && lockSharedSessions.contains(session)) {
traceLock(session, exclusive, "add (upgraded) for ");
lockExclusive = session;
lockExclusiveSession = session;
return;
}
}
} else {
if (lockExclusive == null) {
if (lockExclusiveSession == null) {
if (lockMode == Constants.LOCK_MODE_READ_COMMITTED) {
if (!database.isMultiThreaded() && !database.isMultiVersion()) {
// READ_COMMITTED: a read lock is acquired,
......@@ -164,15 +165,14 @@ public class MVTable extends TableBase {
return;
}
}
if (!lockShared.contains(session)) {
if (!lockSharedSessions.contains(session)) {
traceLock(session, exclusive, "ok");
session.addLock(this);
lockShared.add(session);
lockSharedSessions.add(session);
}
return;
}
}
session.setWaitForLock(this, Thread.currentThread());
if (checkDeadlock) {
ArrayList<Session> sessions = checkDeadlock(session, null, null);
if (sessions != null) {
......@@ -239,7 +239,7 @@ public class MVTable extends TableBase {
}
buff.append(t.toString());
if (t instanceof RegularTable) {
if (((MVTable) t).lockExclusive == s) {
if (((MVTable) t).lockExclusiveSession == s) {
buff.append(" (exclusive)");
} else {
buff.append(" (shared)");
......@@ -271,7 +271,7 @@ public class MVTable extends TableBase {
}
visited.add(session);
ArrayList<Session> error = null;
for (Session s : lockShared) {
for (Session s : lockSharedSessions) {
if (s == session) {
// it doesn't matter if we have locked the object already
continue;
......@@ -285,10 +285,10 @@ public class MVTable extends TableBase {
}
}
}
if (error == null && lockExclusive != null) {
Table t = lockExclusive.getWaitForLock();
if (error == null && lockExclusiveSession != null) {
Table t = lockExclusiveSession.getWaitForLock();
if (t != null) {
error = t.checkDeadlock(lockExclusive, clash, visited);
error = t.checkDeadlock(lockExclusiveSession, clash, visited);
if (error != null) {
error.add(session);
}
......@@ -308,23 +308,23 @@ public class MVTable extends TableBase {
@Override
public boolean isLockedExclusively() {
return lockExclusive != null;
return lockExclusiveSession != null;
}
@Override
public boolean isLockedExclusivelyBy(Session session) {
return lockExclusive == session;
return lockExclusiveSession == session;
}
@Override
public void unlock(Session s) {
if (database != null) {
traceLock(s, lockExclusive == s, "unlock");
if (lockExclusive == s) {
lockExclusive = null;
traceLock(s, lockExclusiveSession == s, "unlock");
if (lockExclusiveSession == s) {
lockExclusiveSession = null;
}
if (lockShared.size() > 0) {
lockShared.remove(s);
if (lockSharedSessions.size() > 0) {
lockSharedSessions.remove(s);
}
// TODO lock: maybe we need we fifo-queue to make sure nobody
// starves. check what other databases do
......
......@@ -87,7 +87,7 @@ public class FunctionTable extends Table {
}
@Override
public void lock(Session session, boolean exclusive, boolean force) {
public void lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) {
// nothing to do
}
......
......@@ -590,7 +590,7 @@ public class MetaTable extends Table {
}
@Override
public void lock(Session session, boolean exclusive, boolean force) {
public void lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) {
// nothing to do
}
......
......@@ -64,7 +64,7 @@ public class RangeTable extends Table {
}
@Override
public void lock(Session session, boolean exclusive, boolean force) {
public void lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) {
// nothing to do
}
......
......@@ -52,8 +52,8 @@ public class RegularTable extends TableBase {
private Index scanIndex;
private long rowCount;
private volatile Session lockExclusive;
private HashSet<Session> lockShared = New.hashSet();
private volatile Session lockExclusiveSession;
private HashSet<Session> lockSharedSessions = New.hashSet();
private final Trace traceLock;
private final ArrayList<Index> indexes = New.arrayList();
private long lastModificationId;
......@@ -435,30 +435,34 @@ public class RegularTable extends TableBase {
@Override
public boolean isLockedExclusivelyBy(Session session) {
return lockExclusive == session;
return lockExclusiveSession == session;
}
@Override
public void lock(Session session, boolean exclusive, boolean force) {
public void lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) {
int lockMode = database.getLockMode();
if (lockMode == Constants.LOCK_MODE_OFF) {
return;
}
if (!force && database.isMultiVersion()) {
if (!forceLockEvenInMvcc && database.isMultiVersion()) {
// MVCC: update, delete, and insert use a shared lock.
// Select doesn't lock except when using FOR UPDATE
if (exclusive) {
exclusive = false;
} else {
if (lockExclusive == null) {
if (lockExclusiveSession == null) {
return;
}
}
}
if (lockExclusive == session) {
if (lockExclusiveSession == session) {
return;
}
synchronized (database) {
if (lockExclusiveSession == session) {
return;
}
session.setWaitForLock(this, Thread.currentThread());
try {
doLock(session, lockMode, exclusive);
} finally {
......@@ -473,24 +477,21 @@ public class RegularTable extends TableBase {
long max = 0;
boolean checkDeadlock = false;
while (true) {
if (lockExclusive == session) {
return;
}
if (exclusive) {
if (lockExclusive == null) {
if (lockShared.isEmpty()) {
if (lockExclusiveSession == null) {
if (lockSharedSessions.isEmpty()) {
traceLock(session, exclusive, "added for");
session.addLock(this);
lockExclusive = session;
lockExclusiveSession = session;
return;
} else if (lockShared.size() == 1 && lockShared.contains(session)) {
} else if (lockSharedSessions.size() == 1 && lockSharedSessions.contains(session)) {
traceLock(session, exclusive, "add (upgraded) for ");
lockExclusive = session;
lockExclusiveSession = session;
return;
}
}
} else {
if (lockExclusive == null) {
if (lockExclusiveSession == null) {
if (lockMode == Constants.LOCK_MODE_READ_COMMITTED) {
if (!database.isMultiThreaded() && !database.isMultiVersion()) {
// READ_COMMITTED: a read lock is acquired,
......@@ -502,15 +503,14 @@ public class RegularTable extends TableBase {
return;
}
}
if (!lockShared.contains(session)) {
if (!lockSharedSessions.contains(session)) {
traceLock(session, exclusive, "ok");
session.addLock(this);
lockShared.add(session);
lockSharedSessions.add(session);
}
return;
}
}
session.setWaitForLock(this, Thread.currentThread());
if (checkDeadlock) {
ArrayList<Session> sessions = checkDeadlock(session, null, null);
if (sessions != null) {
......@@ -574,7 +574,7 @@ public class RegularTable extends TableBase {
}
buff.append(t.toString());
if (t instanceof RegularTable) {
if (((RegularTable) t).lockExclusive == s) {
if (((RegularTable) t).lockExclusiveSession == s) {
buff.append(" (exclusive)");
} else {
buff.append(" (shared)");
......@@ -606,7 +606,7 @@ public class RegularTable extends TableBase {
}
visited.add(session);
ArrayList<Session> error = null;
for (Session s : lockShared) {
for (Session s : lockSharedSessions) {
if (s == session) {
// it doesn't matter if we have locked the object already
continue;
......@@ -620,10 +620,10 @@ public class RegularTable extends TableBase {
}
}
}
if (error == null && lockExclusive != null) {
Table t = lockExclusive.getWaitForLock();
if (error == null && lockExclusiveSession != null) {
Table t = lockExclusiveSession.getWaitForLock();
if (t != null) {
error = t.checkDeadlock(lockExclusive, clash, visited);
error = t.checkDeadlock(lockExclusiveSession, clash, visited);
if (error != null) {
error.add(session);
}
......@@ -642,18 +642,18 @@ public class RegularTable extends TableBase {
@Override
public boolean isLockedExclusively() {
return lockExclusive != null;
return lockExclusiveSession != null;
}
@Override
public void unlock(Session s) {
if (database != null) {
traceLock(s, lockExclusive == s, "unlock");
if (lockExclusive == s) {
lockExclusive = null;
traceLock(s, lockExclusiveSession == s, "unlock");
if (lockExclusiveSession == s) {
lockExclusiveSession = null;
}
if (lockShared.size() > 0) {
lockShared.remove(s);
if (lockSharedSessions.size() > 0) {
lockSharedSessions.remove(s);
}
// TODO lock: maybe we need we fifo-queue to make sure nobody
// starves. check what other databases do
......@@ -713,8 +713,8 @@ public class RegularTable extends TableBase {
scanIndex.remove(session);
database.removeMeta(session, getId());
scanIndex = null;
lockExclusive = null;
lockShared = null;
lockExclusiveSession = null;
lockSharedSessions = null;
invalidate();
}
......
......@@ -133,10 +133,10 @@ public abstract class Table extends SchemaObjectBase {
*
* @param session the session
* @param exclusive true for write locks, false for read locks
* @param force lock even in the MVCC mode
* @param forceLockEvenInMvcc lock even in the MVCC mode
* @throws DbException if a lock timeout occurred
*/
public abstract void lock(Session session, boolean exclusive, boolean force);
public abstract void lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc);
/**
* Close the table object and flush changes.
......
......@@ -140,12 +140,12 @@ public class TableFilter implements ColumnResolver {
*
* @param s the session
* @param exclusive true if an exclusive lock is required
* @param force lock even in the MVCC mode
* @param forceLockEvenInMvcc lock even in the MVCC mode
*/
public void lock(Session s, boolean exclusive, boolean force) {
table.lock(s, exclusive, force);
public void lock(Session s, boolean exclusive, boolean forceLockEvenInMvcc) {
table.lock(s, exclusive, forceLockEvenInMvcc);
if (join != null) {
join.lock(s, exclusive, force);
join.lock(s, exclusive, forceLockEvenInMvcc);
}
}
......
......@@ -390,7 +390,7 @@ public class TableLink extends Table {
}
@Override
public void lock(Session session, boolean exclusive, boolean force) {
public void lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) {
// nothing to do
}
......
......@@ -320,7 +320,7 @@ public class TableView extends Table {
}
@Override
public void lock(Session session, boolean exclusive, boolean force) {
public void lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) {
// exclusive lock means: the view will be dropped
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论