Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / derby-10.3.2.1 / java / engine / org / apache / derby / impl / services / locks / LockTableVTI.java
diff --git a/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/impl/services/locks/LockTableVTI.java b/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/impl/services/locks/LockTableVTI.java
new file mode 100644 (file)
index 0000000..18b5f13
--- /dev/null
@@ -0,0 +1,138 @@
+/*\r
+\r
+   Derby - Class org.apache.derby.impl.services.locks.LockTableVTI\r
+\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to you under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+      http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.services.locks;\r
+\r
+import org.apache.derby.iapi.services.locks.Latch;\r
+\r
+import java.util.Enumeration;\r
+import java.util.NoSuchElementException;\r
+\r
+import java.util.Iterator;\r
+import java.util.ListIterator;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+/**\r
+       This provides an Enumeration of Latch's\r
+       from a clone of the lock table. A Latch is badly named,\r
+       it represents lock information.\r
+ */\r
+class LockTableVTI implements Enumeration\r
+{\r
+       // the clonedLockTable temporarily holds a copy of the lock table.\r
+       //\r
+       // The copy is necessary because the real lock manager needs to be single\r
+       // threaded while a snap shot is made.  After the copy is made, it can take\r
+       // its time digesting the information without blocking the real lock\r
+       // manager.\r
+\r
+       private final Iterator outerControl;\r
+       private Control control;\r
+       private ListIterator grantedList;\r
+       private ListIterator waitingList;\r
+       private Latch nextLock;\r
+\r
+       LockTableVTI(Map clonedLockTable)\r
+       {\r
+               outerControl = clonedLockTable.values().iterator();\r
+       }\r
+\r
+\r
+       public boolean hasMoreElements() {\r
+\r
+               if (nextLock != null)\r
+                       return true;\r
+\r
+               for (;;) {\r
+\r
+                       if (control == null) {\r
+                               if (!outerControl.hasNext())\r
+                                       return false;\r
+//System.out.println("new control lock ");\r
+\r
+                               control = (Control) outerControl.next();\r
+\r
+                               List granted = control.getGranted();\r
+                               if (granted != null)\r
+                                       grantedList = granted.listIterator();\r
+\r
+\r
+                               List waiting = control.getWaiting();\r
+                               if (waiting != null)\r
+                                       waitingList = waiting.listIterator();\r
+\r
+                               nextLock = control.getFirstGrant();\r
+                               if (nextLock == null) {\r
+\r
+                                       nextLock = getNextLock(control);\r
+                               }\r
+                               \r
+                       } else {\r
+                               nextLock = getNextLock(control);\r
+                       }\r
+\r
+\r
+                       if (nextLock != null)\r
+                               return true;\r
+\r
+                       control = null;\r
+               }\r
+       }\r
+\r
+       private Latch getNextLock(Control control) {\r
+               Latch lock = null;\r
+//System.out.println("next lock ");\r
+               if (grantedList != null) {\r
+                       if (grantedList.hasNext()) {\r
+                               lock = (Lock) grantedList.next();\r
+                       }\r
+                       else\r
+                               grantedList = null;\r
+               }\r
+\r
+               if (lock == null) {\r
+                       if (waitingList != null) {\r
+                               if (waitingList.hasNext()) {\r
+                                       lock = (Lock) waitingList.next();\r
+                               }\r
+                               else\r
+                                       waitingList = null;\r
+                       }\r
+               }\r
+\r
+               return lock;\r
+       }\r
+\r
+       public Object nextElement() {\r
+\r
+               if (!hasMoreElements())\r
+                       throw new NoSuchElementException();\r
+\r
+               Latch ret = nextLock;\r
+\r
+               nextLock = null;\r
+               return ret;\r
+       }\r
+}\r
+\r
+\r
+\r