1 24 25 package com.mckoi.database; 26 27 import com.mckoi.debug.*; 28 import java.util.ArrayList ; 29 30 47 48 final class LockingQueue { 49 50 53 private DataTable parent_table; 54 55 58 private ArrayList queue; 59 60 63 LockingQueue(DataTable table) { 64 parent_table = table; 65 queue = new ArrayList (); 66 } 67 68 71 DataTable getTable() { 72 return parent_table; 73 } 74 75 82 synchronized void addLock(Lock lock) { 83 queue.add(lock); 84 } 85 86 92 synchronized void removeLock(Lock lock) { 93 queue.remove(lock); 94 lock.getTable().notifyReleaseRWLock(lock.getType()); 96 notifyAll(); 98 } 99 100 111 void checkAccess(Lock lock) { 112 boolean blocked; 113 int index, i; 114 Lock test_lock; 115 116 synchronized (this) { 117 118 if (!queue.contains(lock)) { 120 throw new Error ("Queue does not contain the given lock"); 121 } 122 123 if (lock.getType() == Lock.READ) { 125 126 do { 127 blocked = false; 128 129 index = queue.indexOf(lock); 130 for (i = index - 1; i >= 0 && blocked == false; --i) { 131 test_lock = (Lock) queue.get(i); 132 if (test_lock.getType() == Lock.WRITE) { 133 blocked = true; 134 } 135 } 136 137 if (blocked == true) { 138 getTable().Debug().write(Lvl.INFORMATION, this, 139 "Blocking on read."); 140 try { 142 wait(); 143 } catch (InterruptedException ignore) {} 144 } 145 146 } while (blocked == true); 147 148 } 149 150 else { 152 153 do { 154 blocked = false; 155 156 index = queue.indexOf(lock); 157 if (index != 0) { 158 blocked = true; 159 getTable().Debug().write(Lvl.INFORMATION, this, 160 "Blocking on write."); 161 try { 163 wait(); 164 } catch (InterruptedException ignore) {} 165 } 166 167 } while (blocked == true); 168 169 } 170 171 lock.getTable().notifyAddRWLock(lock.getType()); 173 174 } 175 176 } 177 178 public synchronized String toString() { 179 StringBuffer str = new StringBuffer ("[LockingQueue]: ("); 180 for (int i = 0; i < queue.size(); ++i) { 181 str.append(queue.get(i)); 182 str.append(", "); 183 } 184 str.append(")"); 185 return new String (str); 186 } 187 188 } 189 | Popular Tags |