Remove NotEnoughInputException and returnNoElement()
Current situation:
If no element was received by any input port, the executing thread checks for termination. The check checks whether all of the stage's input ports are closed.
Resulting disadvantages:
Each stage must be implemented so that it invokes returnNoElement()
if it does not receive any input element. Thus, the stage developer must ensure this contract which cannot statically be analyzed (with low programming effort). Moreover, returnNoElement()
interrupts the usual control flow although the stage developer has not written a return
statement and has not thrown an exception by his own. And all of this is only necessary to check for termination by consumer stage threads.
Proposed Improvement:
Each stage holds a synchronized counter which indicates the current number of its opened input ports. The update of the counter is performed within the synchronized pipes. (a) and (b) are two examples for realization.
public final void close() {
// (a) numOpenInputPorts = getTargetPort().getOwningStage().getOwningThread().getNumOpenInputPorts().decrementAndGet();
// (b) numOpenInputPorts = getTargetPort().getOwningStage().getNumOpenInputPorts().decrementAndGet();
if (numOpenInputPorts == 0) { getTargetPort().getOwningStage().terminateStage(); }
closed = true; // perhaps not necessary anymore
}
Open problem:
After termination, the stage must consume all remaining elements of its input ports. How to know when the consumption has finished?