00001 package gov.nasa.arc.ase.jpf.jvm.runtime;
00002
00003 import javax.swing.tree.*;
00004 import java.util.*;
00005 import gov.nasa.arc.ase.jpf.*;
00006 import gov.nasa.arc.ase.jpf.jvm.*;
00007 import gov.nasa.arc.ase.util.Debug;
00008
00009 public class LockTree{
00010
00011 private ThreadInfo thread;
00012 private LockNode root;
00013 private LockNode current;
00014 private LockMap map;
00015
00016
00017 public LockTree(ThreadInfo thread){
00018 this.thread = thread;
00019 root = new LockNode(thread);
00020 current = root;
00021 map = new LockMap();
00022 }
00023 private int findChildIndex(Lock lock){
00024 Enumeration children = current.children();
00025 LockNode child;
00026 Lock child_lock;
00027 while(children.hasMoreElements()){
00028 child = (LockNode)children.nextElement();
00029 child_lock = child.getLockNodeInfo().getLock();
00030 if(child_lock.equals(lock)){
00031 return current.getIndex(child);
00032 }
00033 };
00034 return -1;
00035 }
00036 public LockMap getLockMap(){
00037 return map;
00038 }
00039 public LockNode getRoot(){
00040 return root;
00041 }
00042 public void lock(Lock lock){
00043 int index = findChildIndex(lock);
00044 if(index >= 0){
00045 current = (LockNode)current.getChildAt(index);
00046 } else{
00047 LockNode lock_node = new LockNode(lock);
00048 current.add(lock_node);
00049 current = lock_node;
00050 map.add(lock,current);
00051 current.getLockNodeInfo().addCallChain(thread);
00052 LockOrder.println("Thread " + thread.getClassInfo().getClassName() +
00053 " takes lock " + lock.getClassName());
00054 }
00055 }
00056 public void print(){
00057 String className = thread.getClassInfo().getClassName();
00058 LockOrder.println("");
00059 LockOrder.println("");
00060 LockOrder.println("Thread " + className + ":");
00061 LockOrder.println("----------");
00062 Enumeration nodes = root.preorderEnumeration();
00063 LockNode node;
00064 while (nodes.hasMoreElements()){
00065 node = (LockNode)nodes.nextElement();
00066 node.printTreeInfo();
00067 }
00068 }
00069 public void unlock(){
00070 if (current.isRoot()){
00071 Debug.println(Debug.ERROR, "*** Attempt to unlock root in Lock tree");
00072 System.exit(0);
00073 };
00074 current = (LockNode)current.getParent();
00075 }
00076 public void unMark(Lock lock){
00077 Iterator slaveNodes = map.getIterator(lock);
00078 LockNode slaveNode;
00079 while (slaveNodes.hasNext()){
00080 slaveNode = (LockNode)slaveNodes.next();
00081 slaveNode.getLockNodeInfo().unMark();
00082 }
00083 }
00084 }