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

since we have removed the synchronization from Database#lockMeta,

it now makes more sense to return a boolean for isLockedExclusivelyBy() from the lock()
method, to make it easier to reason about the atomic-ness of the operation
上级 0c8ca6e6
...@@ -884,8 +884,7 @@ public class Database implements DataHandler { ...@@ -884,8 +884,7 @@ public class Database implements DataHandler {
if (meta == null) { if (meta == null) {
return true; return true;
} }
boolean wasLocked = meta.isLockedExclusivelyBy(session); boolean wasLocked = meta.lock(session, true, true);
meta.lock(session, true, true);
return wasLocked; return wasLocked;
} }
......
...@@ -94,11 +94,11 @@ public class MVTable extends TableBase { ...@@ -94,11 +94,11 @@ public class MVTable extends TableBase {
} }
@Override @Override
public void lock(Session session, boolean exclusive, public boolean lock(Session session, boolean exclusive,
boolean forceLockEvenInMvcc) { boolean forceLockEvenInMvcc) {
int lockMode = database.getLockMode(); int lockMode = database.getLockMode();
if (lockMode == Constants.LOCK_MODE_OFF) { if (lockMode == Constants.LOCK_MODE_OFF) {
return; return false;
} }
if (!forceLockEvenInMvcc && database.isMultiVersion()) { if (!forceLockEvenInMvcc && database.isMultiVersion()) {
// MVCC: update, delete, and insert use a shared lock. // MVCC: update, delete, and insert use a shared lock.
...@@ -109,12 +109,12 @@ public class MVTable extends TableBase { ...@@ -109,12 +109,12 @@ public class MVTable extends TableBase {
exclusive = false; exclusive = false;
} else { } else {
if (lockExclusiveSession == null) { if (lockExclusiveSession == null) {
return; return false;
} }
} }
} }
if (lockExclusiveSession == session) { if (lockExclusiveSession == session) {
return; return true;
} }
synchronized (getLockSyncObject()) { synchronized (getLockSyncObject()) {
session.setWaitForLock(this, Thread.currentThread()); session.setWaitForLock(this, Thread.currentThread());
...@@ -126,6 +126,7 @@ public class MVTable extends TableBase { ...@@ -126,6 +126,7 @@ public class MVTable extends TableBase {
waitingSessions.remove(session); waitingSessions.remove(session);
} }
} }
return false;
} }
/** /**
......
...@@ -87,8 +87,9 @@ public class FunctionTable extends Table { ...@@ -87,8 +87,9 @@ public class FunctionTable extends Table {
} }
@Override @Override
public void lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) { public boolean lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) {
// nothing to do // nothing to do
return false;
} }
@Override @Override
......
...@@ -591,8 +591,9 @@ public class MetaTable extends Table { ...@@ -591,8 +591,9 @@ public class MetaTable extends Table {
} }
@Override @Override
public void lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) { public boolean lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) {
// nothing to do // nothing to do
return false;
} }
@Override @Override
......
...@@ -64,8 +64,9 @@ public class RangeTable extends Table { ...@@ -64,8 +64,9 @@ public class RangeTable extends Table {
} }
@Override @Override
public void lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) { public boolean lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) {
// nothing to do // nothing to do
return false;
} }
@Override @Override
......
...@@ -439,11 +439,11 @@ public class RegularTable extends TableBase { ...@@ -439,11 +439,11 @@ public class RegularTable extends TableBase {
} }
@Override @Override
public void lock(Session session, boolean exclusive, public boolean lock(Session session, boolean exclusive,
boolean forceLockEvenInMvcc) { boolean forceLockEvenInMvcc) {
int lockMode = database.getLockMode(); int lockMode = database.getLockMode();
if (lockMode == Constants.LOCK_MODE_OFF) { if (lockMode == Constants.LOCK_MODE_OFF) {
return; return lockExclusiveSession != null;
} }
if (!forceLockEvenInMvcc && database.isMultiVersion()) { if (!forceLockEvenInMvcc && database.isMultiVersion()) {
// MVCC: update, delete, and insert use a shared lock. // MVCC: update, delete, and insert use a shared lock.
...@@ -452,16 +452,16 @@ public class RegularTable extends TableBase { ...@@ -452,16 +452,16 @@ public class RegularTable extends TableBase {
exclusive = false; exclusive = false;
} else { } else {
if (lockExclusiveSession == null) { if (lockExclusiveSession == null) {
return; return false;
} }
} }
} }
if (lockExclusiveSession == session) { if (lockExclusiveSession == session) {
return; return true;
} }
synchronized (database) { synchronized (database) {
if (lockExclusiveSession == session) { if (lockExclusiveSession == session) {
return; return true;
} }
session.setWaitForLock(this, Thread.currentThread()); session.setWaitForLock(this, Thread.currentThread());
waitingSessions.addLast(session); waitingSessions.addLast(session);
...@@ -472,6 +472,7 @@ public class RegularTable extends TableBase { ...@@ -472,6 +472,7 @@ public class RegularTable extends TableBase {
waitingSessions.remove(session); waitingSessions.remove(session);
} }
} }
return false;
} }
private void doLock1(Session session, int lockMode, boolean exclusive) { private void doLock1(Session session, int lockMode, boolean exclusive) {
......
...@@ -134,9 +134,10 @@ public abstract class Table extends SchemaObjectBase { ...@@ -134,9 +134,10 @@ public abstract class Table extends SchemaObjectBase {
* @param session the session * @param session the session
* @param exclusive true for write locks, false for read locks * @param exclusive true for write locks, false for read locks
* @param forceLockEvenInMvcc lock even in the MVCC mode * @param forceLockEvenInMvcc lock even in the MVCC mode
* @return true if the table was already exclusively locked by this session.
* @throws DbException if a lock timeout occurred * @throws DbException if a lock timeout occurred
*/ */
public abstract void lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc); public abstract boolean lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc);
/** /**
* Close the table object and flush changes. * Close the table object and flush changes.
......
...@@ -390,8 +390,9 @@ public class TableLink extends Table { ...@@ -390,8 +390,9 @@ public class TableLink extends Table {
} }
@Override @Override
public void lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) { public boolean lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) {
// nothing to do // nothing to do
return false;
} }
@Override @Override
......
...@@ -320,8 +320,9 @@ public class TableView extends Table { ...@@ -320,8 +320,9 @@ public class TableView extends Table {
} }
@Override @Override
public void lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) { public boolean lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) {
// exclusive lock means: the view will be dropped // exclusive lock means: the view will be dropped
return false;
} }
@Override @Override
......
...@@ -341,8 +341,9 @@ public class TestTableEngines extends TestBase { ...@@ -341,8 +341,9 @@ public class TestTableEngines extends TestBase {
} }
@Override @Override
public void lock(Session session, boolean exclusive, boolean force) { public boolean lock(Session session, boolean exclusive, boolean force) {
// do nothing // do nothing
return false;
} }
@Override @Override
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论