001: /*
002:
003: Derby - Class org.apache.derby.impl.services.locks.LockTableVTI
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.services.locks;
023:
024: import org.apache.derby.iapi.services.locks.Latch;
025:
026: import java.util.Hashtable;
027: import java.util.Vector;
028: import java.util.Enumeration;
029: import java.util.NoSuchElementException;
030:
031: import java.util.ListIterator;
032: import java.util.List;
033:
034: /**
035: This provides an Enumeration of Latch's
036: from a clone of the lock table. A Latch is badly named,
037: it represents lock information.
038: */
039: class LockTableVTI implements Enumeration {
040: // the clonedLockTable temporarily holds a copy of the lock table.
041: //
042: // The copy is necessary because the real lock manager needs to be single
043: // threaded while a snap shot is made. After the copy is made, it can take
044: // its time digesting the information without blocking the real lock
045: // manager.
046:
047: private final LockSet clonedLockTable;
048: private final Enumeration outerControl;
049: private Control control;
050: private ListIterator grantedList;
051: private ListIterator waitingList;
052: private Latch nextLock;
053:
054: LockTableVTI(LockSet clonedLockTable) {
055: this .clonedLockTable = clonedLockTable;
056:
057: outerControl = clonedLockTable.elements();
058: }
059:
060: public boolean hasMoreElements() {
061:
062: if (nextLock != null)
063: return true;
064:
065: for (;;) {
066:
067: if (control == null) {
068: if (!outerControl.hasMoreElements())
069: return false;
070: //System.out.println("new control lock ");
071:
072: control = (Control) outerControl.nextElement();
073:
074: List granted = control.getGranted();
075: if (granted != null)
076: grantedList = granted.listIterator();
077:
078: List waiting = control.getWaiting();
079: if (waiting != null)
080: waitingList = waiting.listIterator();
081:
082: nextLock = control.getFirstGrant();
083: if (nextLock == null) {
084:
085: nextLock = getNextLock(control);
086: }
087:
088: } else {
089: nextLock = getNextLock(control);
090: }
091:
092: if (nextLock != null)
093: return true;
094:
095: control = null;
096: }
097: }
098:
099: private Latch getNextLock(Control control) {
100: Latch lock = null;
101: //System.out.println("next lock ");
102: if (grantedList != null) {
103: if (grantedList.hasNext()) {
104: lock = (Lock) grantedList.next();
105: } else
106: grantedList = null;
107: }
108:
109: if (lock == null) {
110: if (waitingList != null) {
111: if (waitingList.hasNext()) {
112: lock = (Lock) waitingList.next();
113: } else
114: waitingList = null;
115: }
116: }
117:
118: return lock;
119: }
120:
121: public Object nextElement() {
122:
123: if (!hasMoreElements())
124: throw new NoSuchElementException();
125:
126: Latch ret = nextLock;
127:
128: nextLock = null;
129: return ret;
130: }
131: }
|