Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void acquire(Transaction transaction, String tableName, LockType lockType)
- throws IllegalArgumentException {
- if (!transaction.getStatus().equals(Transaction.Status.Running)) {
- throw new IllegalArgumentException();
- }
- TableLock tLock = tableToTableLock.get(tableName);
- if (tLock == null || tLock.lockOwners.isEmpty()) {
- tLock = new TableLock(lockType);
- tLock.lockOwners.add(transaction);
- tableToTableLock.put(tableName, tLock);
- return;
- }
- boolean granted = true;
- boolean promote = false;
- for (Request r : tLock.requestersQueue) {
- if (r.transaction.equals(transaction)) {
- if (r.lockType.equals(LockType.Exclusive) || r.lockType.equals(lockType))
- throw new IllegalArgumentException();
- else {
- if (tLock.requestersQueue.size() == 1) {
- // promotion granted
- tLock.lockType = LockType.Exclusive;
- tableToTableLock.put(tableName, tLock);
- return;
- } else {
- promote = true;
- }
- }
- } else if (r.lockType.equals(LockType.Exclusive) && tLock.lockOwners.contains(r.transaction)) {
- transaction.sleep();
- granted = false;
- break;
- } else if (r.lockType.equals(LockType.Shared) && tLock.lockOwners.contains(r.transaction) && lockType.equals(LockType.Exclusive)) {
- transaction.sleep();
- granted = false;
- break;
- }
- }
- if (granted) {
- tLock.lockType = lockType;
- tLock.lockOwners.add(transaction);
- } else {
- // TODO check lockType waitDie woundWait
- if (promote)
- tLock.requestersQueue.addFirst(new Request(transaction, lockType));
- else
- tLock.requestersQueue.add(new Request(transaction, lockType));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement