For an assigment, I have to create basic processor modules and allow them to exchange double values between each other. The modules will have the public function step()
, which advances the execution of their program by one instruction. The modules are held in a HashMap, which the main program iterates over, ensuring that each (active) module can execute one instruction per cycle.
The way I plan to handle the inter-module communication is to create input-/output data fields inside each module, which are linked by the main program to all the other modules, acting as pipes between the modules. So when a processor module sets an ouput data field, the main program is notified, which then finds the module that output field is linked to and writes the value into the corresponding input data field of the receiving module and clears the output field from the sending module.
If a module wants to read from an input field that hasn't been set yet (== Double.NaN
), or if a module wants to write to an output field that still holds a value (!= Double.NaN
), the main program is notified as well and puts the module in question to sleep until the data field that is blocking the module is set/reset.
My question is, is this a reasonable approach to handling inter-process communication or should I change something. And if it is, how do I go about notifiying the main program in a way that allows it to put a module to sleep and resume it later on?