1 23 24 package org.continuent.sequoia.controller.scheduler.schema; 25 26 import java.util.Iterator ; 27 import java.util.LinkedList ; 28 29 import org.continuent.sequoia.controller.requests.AbstractRequest; 30 31 40 public class TransactionExclusiveLock 41 { 42 private boolean isLocked = false; 43 44 45 private long locker; 46 47 48 private LinkedList waitingList = new LinkedList (); 49 50 54 private class WaitingListElement 55 { 56 57 Thread thread; 58 59 60 long transactionId; 61 62 68 WaitingListElement(Thread thread, long transactionId) 69 { 70 this.thread = thread; 71 this.transactionId = transactionId; 72 } 73 74 79 public long getTransactionId() 80 { 81 return transactionId; 82 } 83 84 89 public Thread getThread() 90 { 91 return thread; 92 } 93 } 94 95 107 public boolean acquire(AbstractRequest request) 108 { 109 long tid = request.getTransactionId(); 110 111 synchronized (Thread.currentThread()) 112 { 113 WaitingListElement wle = null; 114 synchronized (this) 115 { 116 if (!isLocked) 117 { locker = tid; 119 isLocked = true; 120 return true; 121 } 122 else 123 { 124 if (locker == tid) 125 return true; else 127 { wle = new WaitingListElement(Thread.currentThread(), tid); 129 waitingList.addLast(wle); 130 } 131 } 132 } 133 try 135 { 136 int timeout = request.getTimeout(); 137 if (timeout == 0) 138 { 139 Thread.currentThread().wait(); return true; 142 } 143 else 144 { long start = System.currentTimeMillis(); 146 long lTimeout = timeout * 1000L; 148 Thread.currentThread().wait(lTimeout); 149 long end = System.currentTimeMillis(); 150 int remaining = (int) (lTimeout - (end - start)); 151 if (remaining > 0) 152 { request.setTimeout(remaining); 154 return true; 156 } 157 else 158 { synchronized (this) 160 { 161 for (Iterator iter = waitingList.iterator(); iter.hasNext();) 162 { 163 WaitingListElement e = (WaitingListElement) iter.next(); 164 if (e.equals(wle)) 165 { 166 iter.remove(); 167 return false; 168 } 169 } 170 release(); 173 } 174 return false; 175 } 176 } 177 } 178 catch (InterruptedException ie) 179 { 180 synchronized (this) 181 { waitingList.remove(Thread.currentThread()); 183 } 184 return false; 185 } 186 } 187 } 188 189 194 public synchronized void release() 195 { 196 if (!waitingList.isEmpty()) 197 { 198 WaitingListElement e = (WaitingListElement) waitingList.removeFirst(); 200 Thread thread = e.getThread(); 201 locker = e.getTransactionId(); 202 synchronized (thread) 203 { 204 thread.notify(); 205 } 207 } 208 else 209 isLocked = false; 210 } 211 212 217 public boolean isLocked() 218 { 219 return isLocked; 220 } 221 222 228 public long getLocker() 229 { 230 return locker; 231 } 232 233 238 public LinkedList getWaitingList() 239 { 240 return waitingList; 241 } 242 243 250 public synchronized boolean isWaiting(long transactionId) 251 { 252 for (Iterator iter = waitingList.iterator(); iter.hasNext();) 253 { 254 WaitingListElement e = (WaitingListElement) iter.next(); 255 if (e.getTransactionId() == transactionId) 256 return true; 257 } 258 return false; 259 } 260 } | Popular Tags |