A peer must already exist before it can participate in peer multitasking: (i.e. it has already been set up using ec_init (embedded) or ec_remote_init (remote)).
Peer multitasking occur in a multitasking phase, which is a special way for an ECLiPSe side to hand over control to the external side. Effectively, instead of handing over control to a single peer, control is handed over repeatedly to all the peers that are participating in peer multitasking. The control is shared between these peers in a round-robin fashion, giving rise to a form of co-operative multitasking. The multitasking is co-operative in that each peer has to give up control, so that the control can be passed to the next multitasking peer. A peer multitasking phase is started by calling the predicate peer_do_multitask/1 in ECLiPSe.
To participate in peer multitasking, a peer must be “registered” (initialised) for peer multitasking. This is done by executing the procedure ec_multi:peer_register from Tcl. Once registered, the peer will take part in subsequent multitasking phases.
The registration can set up three user-defined handlers: the start handler, the interact handler, and the end handler. During a multitasking phase, control is handed over to a multitasking peer, which invokes one of these handlers, and when the handler returns, control is handed to the next multitasking peer. The interact handler is normally invoked, except at the start (first) and end (last) of a multitasking phase. In these cases, the start and end handlers are invoked respectively.
A ‘type’ (specified in the call to peer_do_multitask/1) is associated with every multitasking phase to allow multitasking phases for different purposes (distinguished by different ‘type’s). This type is passed to the handlers as an argument. At the start of a multitasking phase, a peer should indicate if it is interested in this particular multitasking phase or not. If no peer is interested in the multitasking phase, then the multitasking phase enters the end phase after the initial round of passing control to all the multitasking peers, i.e. control is passed one more time to the multitasking peers, this time invoking the end handler. If at least one peer indicates that it is interested in the multitasking phase, then after the initial start phase, the control will be repeatedly handed over to each multitasking peer by invoking the interact handler. This phase is ended when one (or more) peer indicates that the multitasking phase should end (at which point the end phase will be entered in which the end handler will be invoked for each multitasking peer).
Here is a more detailed description of the Tcl procedures:
ec_multi:peer_register [list interact tkecl:multi_interact_handler start tkecl:multi_start_handler end tkecl:multi_end_handler]When control is handed over to the peer during a peer multitasking phase, the appropriate handler (if defined) is invoked. When the handler returns, control is handed back to ECLiPSe (and passed to the next multitasking peer). Note that events are not processed while the peer does not have control. The Tcl command update is therefore called each time the peer is given control, to allow any accumulated events to be processed. As long as the peer is given control frequently enough, then any interactions with the peer would appear to be continuous.
The handlers are invoked with the ‘type’ of the multitasking phase supplied as the first argument. This will for example allow the start handler to determine if it is interested in this multitasking phase. They can also set one of the following return codes:
For example, here is the start handler used in the Tk development tools:
proc tkecl:multi_start_handler {type} { switch $type { tracer { # multitasking phase is `tracer' # only do handling of port if the tracer window exists if [winfo exists .ec_tools.ec_tracer] { tkecl:handle_tracer_port_start set of_interest continue } } default { set of_interest no # do nothing } } return $of_interest }
An error is raised if this procedure is called while the peer is already registered for multitasking.
An error is raised if this procedure is called while the peer is not registered for multitasking.
Note that the peer multitasking code is implemented using peer queue handlers. Thus, the peer multitask status is set to ‘on’ before the multitask start handler is called, but after the ‘ECLiPSe end’ handler. Conversely, the peer multitask status is set to ‘off’ after the multitask end handler, but before the ECLiPSe start handler.