KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > dava > toolkits > base > DavaMonitor


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Jerome Miecznikowski
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.dava.toolkits.base.DavaMonitor;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.LinkedList JavaDoc;
24
25 public class DavaMonitor
26 {
27     private static DavaMonitor instance = new DavaMonitor();
28
29     private HashMap JavaDoc ref, lockTable;
30     private LinkedList JavaDoc q;
31
32     private DavaMonitor()
33     {
34     ref = new HashMap JavaDoc( 1, 0.7f);
35     lockTable = new HashMap JavaDoc( 1, 0.7f);
36     q = new LinkedList JavaDoc();
37     }
38
39     public static DavaMonitor v() { return instance; }
40
41     public synchronized void enter( Object JavaDoc o) throws NullPointerException JavaDoc
42     {
43     Thread JavaDoc currentThread = Thread.currentThread();
44
45     if (o == null)
46         throw new NullPointerException JavaDoc();
47
48     Lock lock = (Lock) ref.get( o);
49
50     if (lock == null) {
51         lock = new Lock();
52         ref.put( o, lock);
53     }
54
55     if (lock.level == 0) {
56         lock.level = 1;
57         lock.owner = currentThread;
58         return;
59     }
60     
61     if (lock.owner == currentThread) {
62         lock.level++;
63         return;
64     }
65     
66     lockTable.put( currentThread, lock);
67     lock.enQ( currentThread);
68     
69     while ((lock.level > 0) || (lock.nextThread() != currentThread)) {
70         try {
71         wait();
72         }
73         catch (InterruptedException JavaDoc e) {
74         e.printStackTrace();
75         System.exit(0);
76         }
77         
78         currentThread = Thread.currentThread();
79         lock = (Lock) lockTable.get( currentThread);
80     }
81     
82     lock.deQ( currentThread);
83     
84     lock.level = 1;
85     lock.owner = currentThread;
86     }
87
88     public synchronized void exit( Object JavaDoc o) throws NullPointerException JavaDoc, IllegalMonitorStateException JavaDoc
89     {
90     if (o == null)
91         throw new NullPointerException JavaDoc();
92
93     Lock lock = (Lock) ref.get( o);
94
95     if ((lock == null) || (lock.level == 0) || (lock.owner != Thread.currentThread()))
96         throw new IllegalMonitorStateException JavaDoc();
97     
98     lock.level--;
99     
100     if (lock.level == 0)
101         notifyAll();
102     }
103 }
104
Popular Tags