KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > concurrency > distributed > globallock > lib > GlobalLockWaiterImpl


1 /**
2  * Copyright (C) 2003-2004
3  * - France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR AE PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Release: 1.0
20  *
21  * Authors: Olivier Lobry (olivier.lobry@rd.francetelecom.com)
22  *
23  */

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 JavaDoc {
42             while (!ok) wait(timeout);
43             //return ok;
44
}
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 JavaDoc {
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 JavaDoc {
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                 // this mean that wait exit on timeout
76
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