// set branch deadlock for all nodes between startNode
// and currNode
endNode = currNode.waitingOn;
currNode = startNode;
NodeList branchList = null;
while (currNode != endNode)
{
if (null == branchList)
{
branchList = new NodeList(currNode, nodeListNum++);
}
currNode.deadlock = MonitorNode.BRANCH_DEADLOCK;
currNode = currNode.waitingOn;
branchList.add(currNode);
if (currNode != endNode)
currNode.inList = branchList;
}
if (endNode.inList.isLoop())
{
lists.insertElementAt(branchList, lists.indexOf(endNode.inList));
}
else
{
NodeList oldList = endNode.inList;
// FIXME: the below line will cause problems with at least
// one case that was not considered when attachOrSplit was
// coded: if a NodeList n1 has already been split and another
// NodeList n2 tries to attach to the end of n1, then n1 will
// allow n2 to attach to n1, while what n1 should really do is
// just return n2 and not allow n2 to attach to itself
NodeList split = endNode.inList.attachOrSplit(branchList, nodeListNum++);
if (null != split)
{
lists.insertElementAt(split, lists.indexOf(oldList));
lists.insertElementAt(branchList, lists.indexOf(oldList));
}
}
break;
}
if (currNode.waitingOn.visit == visit)
{
// we've encountered a node in the same visit as the current
// visit, ie. we've found a loop; first flag the whole loop
// with a loop deadlock flag, then flag the rest of the nodes
// in the chain with a branch deadlock
endNode = currNode.waitingOn;
currNode = endNode;
NodeList loopList = new NodeList(currNode, nodeListNum++);
lists.insertElementAt(loopList, 0);
do
{
currNode.deadlock = MonitorNode.LOOP_DEADLOCK;
currNode = currNode.waitingOn;
loopList.add(currNode);
currNode.inList = loopList;
} while (currNode != endNode);
currNode = startNode;
NodeList branchList = null;
while (currNode != endNode)
{
if (null == branchList)
{
branchList = new NodeList(currNode, nodeListNum++);
lists.insertElementAt(branchList, 0);
}
currNode.deadlock = MonitorNode.BRANCH_DEADLOCK;
currNode = currNode.waitingOn;
branchList.add(currNode);
if (currNode != endNode)
currNode.inList = branchList;
}
break;
}
currNode = currNode.waitingOn;
}
visit++;
}
if (lists.isEmpty())
{
out.print("\n");
out.print("\t no deadlocks detected");
out.print("\n");
return;
}
boolean lastListWasLoop = true;
Iterator itList = lists.iterator();
// Step 5. print the lists
while (itList.hasNext())
{
NodeList list = (NodeList)itList.next();
if (list.isLoop()) {
out.print("\n deadlock loop:\n");
lastListWasLoop = true;
} else if (lastListWasLoop) { // && !list.isLoop()
out.print("\n\n deadlock branch(es):\n");
lastListWasLoop = false;
}
out.print("\t " + list.toString());
out.print("\n");
}
out.print("\n");
jrnum++;
}