// Assignment 4 - Pipe-and-Filter KWIC Indexing.
// Concurrent Programming, CS 498-598
// Summer 2002
//
// This code is unmaintained and may be obsolete (as well as being incorrect).
// See the answer to assignment 4 for possible updates.
import java.util.Arrays;
class
PAFTermination
implements TerminationInterface {
final static int
unterminated = 0,
normalt = 1,
abnormalt = 2;
final private int status[];
public void
abnormal(int id) {
obit(id, abnormalt);
}
private int
check() {
// Determine the state of the termination invariant.
int i = 0;
while ((i < status.length) && (status[i] == normalt))
i++;
if (i == status.length)
return normalt;
while ((i < status.length) && (status[i] == unterminated))
i++;
return i == status.length ? unterminated : abnormalt;
}
synchronized int
disposition() {
// Wait until all the threads have terminated normally, or one of the
// threads have terminated abnormally.
while (true) {
try { wait(); }
catch (InterruptedException ie) {
return abnormalt;
}
final int i = check();
if (i != unterminated)
return i;
}
}
public void
normal(int id) {
obit(id, normalt);
}
private synchronized void
obit(int id, int stat) {
assert (0 <= id) && (id < status.length);
assert status[id] == unterminated;
assert (stat == normalt) || (stat == abnormalt);
status[id] = stat;
notify();
}
PAFTermination(int count) {
assert 0 < count;
status = new int[count];
Arrays.fill(status, unterminated);
}
}
syntax highlighted by Code2HTML, v. 0.9