1 24 25 package org.objectweb.perseus.concurrency.distributed.globallock.lib; 26 27 import org.objectweb.perseus.concurrency.lib.LockValue; 28 import org.objectweb.perseus.concurrency.distributed.globallock.api.GlobalLockWaiter; 29 30 31 public class GlobalLockWaiterImpl implements GlobalLockWaiter 32 { 33 static final boolean trace = false; 34 35 private byte lck; 36 private Handled handled = new Handled(); 37 GlobalLockWaiterImpl waitFor; 38 private class Handled { 39 boolean ok = false; 40 41 public synchronized void waitHandled(long timeout) throws InterruptedException { 42 while (!ok) wait(timeout); 43 } 45 46 public synchronized void signalHandled() { 47 ok = true; 48 notify(); 49 } 50 } 51 GlobalLockWaiterImpl(byte lck) { 52 this.lck = lck; 53 } 54 55 public void waitHandled(long timeout) throws InterruptedException { 56 handled.waitHandled(timeout); 57 } 58 59 public void signalHandled() { 60 handled.signalHandled(); 61 } 62 63 public synchronized boolean waitLock(long timeout) 64 throws InterruptedException { 65 if (trace) System.out.println("WAITER WAITS LOCK: " + this); 66 if (lck != LockValue.NOLOCK) { 67 if (trace) System.out.println("WAITER SLEEPS: " + this); 68 wait(timeout); 69 if (waitFor != null) { 70 if (trace) System.out.println("TIMEOUT " + this); 71 waitFor.waitHandled(timeout); 72 } 73 if (trace) System.out.println("WAITER WAKES UP: " + this); 74 if (lck != LockValue.NOLOCK) { 75 if (trace) System.out.println("TIMEOUT " + this); 77 lck = LockValue.NOLOCK; 78 return false; 79 } 80 } else 81 if (trace) System.out.println("WAITER PASS THROUGH: " + this); 82 return true; 83 } 84 85 public synchronized boolean signalLock(byte signaledLck, 86 GlobalLockWaiterImpl waitFor) { 87 if (trace) System.out.println("WAITER SIGNAL LOCK: " + this); 88 89 if (signaledLck >= this.lck) { 90 this.lck = LockValue.NOLOCK; 91 this.waitFor = waitFor; 92 if (trace) System.out.println("WAITER NOTIFIES ALL: " + this); 93 notifyAll(); 94 return true; 95 } 96 else if (trace) System.out.println("WAITER SIGNALED W/ LOCK TOO WEAK (" 97 + signaledLck + "/" + lck + "): " + this); 98 return false; 99 } 100 101 public byte getLockLevel() { 102 return lck; 103 } 104 } 105 | Popular Tags |