KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > services > locks > LockTableVTI


1 /*
2
3    Derby - Class org.apache.derby.impl.services.locks.LockTableVTI
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.services.locks;
23
24 import org.apache.derby.iapi.services.locks.Latch;
25
26 import java.util.Hashtable JavaDoc;
27 import java.util.Vector JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.NoSuchElementException JavaDoc;
30
31 import java.util.ListIterator JavaDoc;
32 import java.util.List JavaDoc;
33
34 /**
35     This provides an Enumeration of Latch's
36     from a clone of the lock table. A Latch is badly named,
37     it represents lock information.
38  */

39 class LockTableVTI implements Enumeration JavaDoc
40 {
41     // the clonedLockTable temporarily holds a copy of the lock table.
42
//
43
// The copy is necessary because the real lock manager needs to be single
44
// threaded while a snap shot is made. After the copy is made, it can take
45
// its time digesting the information without blocking the real lock
46
// manager.
47

48     private final LockSet clonedLockTable;
49     private final Enumeration JavaDoc outerControl;
50     private Control control;
51     private ListIterator JavaDoc grantedList;
52     private ListIterator JavaDoc waitingList;
53     private Latch nextLock;
54
55     LockTableVTI(LockSet clonedLockTable)
56     {
57         this.clonedLockTable = clonedLockTable;
58
59         outerControl = clonedLockTable.elements();
60     }
61
62
63     public boolean hasMoreElements() {
64
65         if (nextLock != null)
66             return true;
67
68         for (;;) {
69
70             if (control == null) {
71                 if (!outerControl.hasMoreElements())
72                     return false;
73 //System.out.println("new control lock ");
74

75                 control = (Control) outerControl.nextElement();
76
77                 List JavaDoc granted = control.getGranted();
78                 if (granted != null)
79                     grantedList = granted.listIterator();
80
81
82                 List JavaDoc waiting = control.getWaiting();
83                 if (waiting != null)
84                     waitingList = waiting.listIterator();
85
86                 nextLock = control.getFirstGrant();
87                 if (nextLock == null) {
88
89                     nextLock = getNextLock(control);
90                 }
91                 
92             } else {
93                 nextLock = getNextLock(control);
94             }
95
96
97             if (nextLock != null)
98                 return true;
99
100             control = null;
101         }
102     }
103
104     private Latch getNextLock(Control control) {
105         Latch lock = null;
106 //System.out.println("next lock ");
107
if (grantedList != null) {
108             if (grantedList.hasNext()) {
109                 lock = (Lock) grantedList.next();
110             }
111             else
112                 grantedList = null;
113         }
114
115         if (lock == null) {
116             if (waitingList != null) {
117                 if (waitingList.hasNext()) {
118                     lock = (Lock) waitingList.next();
119                 }
120                 else
121                     waitingList = null;
122             }
123         }
124
125         return lock;
126     }
127
128     public Object JavaDoc nextElement() {
129
130         if (!hasMoreElements())
131             throw new NoSuchElementException JavaDoc();
132
133         Latch ret = nextLock;
134
135         nextLock = null;
136         return ret;
137     }
138 }
139
140
141
142
Popular Tags