This chapter breaks down the combined behavior of the event queue and the context stack inside GPSS-Plus, and how they affect asynchronous behavior with SIGNAL and synchronous behavior with SIGNALNOW.
The event queue
When an entity executes an ADVANCE block, it is placed into the event queue, which is ordered chronologically. If multiple events share the same time, the entity that entered first has priority (FIFO). The queue only stores the execution time, a reference to the entity, and the element type.
The context stack
Each entity has its own private context stack, used to resume execution after jumps such as CALL or FOREACH. This stack is LIFO: the last context pushed is the first popped.
When calling other entities (agents) via SIGNAL or SIGNALNOW, the current execution state is saved on this stack so it can be resumed afterward.
What happens with SIGNAL?
SIGNAL is asynchronous. The sending entity does not stop; it merely schedules the execution of a procedure in another agent. If three SIGNALs are sent in sequence to the same agent, the sender continues immediately, while the receiving agent is scheduled three times in the event queue.
When the agent runs, it executes the pending procedures using its stack. Because the stack is LIFO, execution happens in reverse order, producing results such as: (((0 + 4) * 10) + 2) = 42.
What about SIGNALNOW?
SIGNALNOW is synchronous. It temporarily pauses the calling entity, immediately executes the agent procedure, and then resumes execution. Calls therefore run in the written order, yielding: (((0 + 2) * 10) + 4) = 24.
Example in action
This model defines an agent with three methods (add2, multiply10, add4). Two entities invoke it: one using SIGNAL and the other using SIGNALNOW. On screen, the different results clearly show the behavioral difference.
With SIGNAL, the result remains unchanged at first because the calls have not yet executed. A simple ADVANCE 0 (re-scheduling the entity in the event queue) allows the agent calls to run, after which the result becomes available.
Example
/*
SIGNAL vs SIGNALNOW
*/
SYSTEM {TYPE:PRE_RUN,TRIGGER:PRE_RUN}
Restroom {NAME:RestroomAgents,X:252,Y:60,visible:1}
POSITION {NAME:POS1,X:552,Y:194}
POSITION {NAME:POS2,X:552,Y:394}
Graphic {NAME:Text1,Type:TEXT,X:313,Y:252,Text:"Entity"}
Graphic {NAME:Text2,Type:TEXT,X:313,Y:232,Text:"Entity"}
Graphic {NAME:Text3,Type:TEXT,X:326,Y:444,Text:"Entity"}
Graphic {NAME:TextAgent,Type:TEXT,X:311,Y:325,Text:"---",color:#ff3333}
initial nAgent,0
START 2
;*****************************************************
PROCEDURE PRE_RUN
timeout agent.main,0
TERMINATE_VE
ENDPROCEDURE 1
;*****************************************************
GENERATE 10,0,0,1 {NAME:GEN1,X:100,Y:200}
assign result,0,X$nAgent
SIGNAL agent.add2,X$nAgent
SIGNAL agent.multiply10,X$nAgent
SIGNAL agent.add4,X$nAgent
move {name:Text1,TEXT:"SIGNAL Before: P$(result,X$nAgent)"}
advance 0
move {name:Text2,TEXT:"SIGNAL Result: P$(result,X$nAgent)"}
ADVANCE 20,0 {TO:POS1}
ENDGENERATE 1
GENERATE 60,0,0,1 {NAME:GEN2,X:100,Y:400}
assign result,0,X$nAgent
SIGNALNOW agent.add2,X$nAgent
SIGNALNOW agent.multiply10,X$nAgent
SIGNALNOW agent.add4,X$nAgent
move {name:Text3,TEXT:"SIGNALNOW Result: P$(result,X$nAgent)"}
ADVANCE 20,60 {TO:POS2}
ENDGENERATE 1
;*******************************
procedure agent.main
savevalue nAgent,D$N
assign result,0
REST RestroomAgents
terminate_ve
endprocedure
procedure agent.add2
assign result,P$result + 2
move {name:TextAgent,TEXT:"Result ADD2: P$result T= AC1$"}
RETURN_RESTORE
endprocedure
procedure agent.add4
assign result,P$result + 4
move {name:TextAgent,TEXT:"Result ADD4: P$result T= AC1$"}
RETURN_RESTORE
endprocedure
procedure agent.multiply10
assign result,P$result * 10
move {name:TextAgent,TEXT:"Result MULTIPLY10: P$result T= AC1$"}
RETURN_RESTORE
endprocedure