SIGNAL and SIGNALNOW

Updated: 2025-11-22

SIGNAL

SIGNAL invokes a PROCEDURE to be executed by another entity, but does not interrupt the calling entity.

  • The execution occurs at the same simulation time (AC1).
  • It is placed at the end of the event list for that same time.
  • The calling entity continues normally and finishes its cycle.
  • It is a deferred call.

In short: SIGNAL schedules the procedure execution but does not interrupt the caller.

SIGNALNOW

SIGNALNOW invokes a PROCEDURE to be executed immediately by another entity, interrupting the current one.

  • The invoked entity takes control immediately, within the same AC1.
  • The calling entity is suspended and later resumes depending on the return type.
  • The signaled entity becomes the first to execute at that time.
  • It is a preemptive call.

Typically, the procedure should end with:

  • RETURN_RESTORE: the interrupted entity resumes exactly where it was.
  • RETURN_RETRY: the interrupted entity repeats the operation it was performing (common with SEIZE, ENTER, or resources).

In summary: SIGNALNOW causes an immediate interruption; the target entity executes the procedure without waiting.

In the example:

This example demonstrates SIGNAL / SIGNALNOW with active and sleeping agents.

The final behavior depends on the state of the receiving agent:

  • aliveAgent: continuously active in a loop with ADVANCE.
  • sleepingAgent: stopped in a REST instruction.

The example highlights the real differences between RETURN, RETURN_RESTORE, and RETURN_RETRY, and when each one is safe or dangerous depending on the agent state.

Example

/* SIGNAL and SIGNALNOW in permanent agents */

SYSTEM {TYPE:PRE_RUN, TRIGGER:PRE_RUN}

POSITION {NAME:POS_1,   X:615, Y:398}

Graphic {NAME:T_resultado1, type:TEXT, X:425, Y:281,text:"result1"}
Graphic {NAME:T_resultado2, type:TEXT, X:425, Y:256,text:"result2"}
Graphic {NAME:T2, type:TEXT, X:430, Y:501}
Graphic {NAME:T3, type:TEXT, X:430, Y:531}
Graphic {NAME:T4, type:TEXT, X:426, Y:310}
Graphic {NAME:T5, type:TEXT, X:425, Y:188}

RESTROOM {name:RestRoomSleepingAgent,x:423,y:118}

START 200

/* ---- PRE RUN: Create agents ---- */
PROCEDURE PRE_RUN
    TIMEOUT aliveAgent.loop, 0
    TIMEOUT sleepingAgent.loop, 0
    TERMINATE_VE
ENDPROCEDURE

/* ---- NORMAL ENTITY GENERATOR ---- */
GENERATE 15,0 {NAME:G1, X:153, Y:411}

    ASSIGN myValue, D$N * 10
    MOVE {name:T4, text:"Entity D$N starts with myValue = P$myValue"}

    ADVANCE 10 {to:POS_1}

    ; Deferred SIGNAL call
    SIGNAL aliveAgent.process, X$nAliveAgent, "Hello"

    ; Immediate SIGNALNOW call
    SIGNALNOW aliveAgent.add, X$nAliveAgent, P$myValue, 5
    MOVE {name:T_resultado1, text:"Alive agent result = P$add"}

    SIGNALNOW sleepingAgent.add, X$nSleepingAgent, P$myValue, 5
    MOVE {name:T_resultado2, text:"Sleeping agent result = P$add"}

ENDGENERATE 4

/* ---- ALIVE AGENT ---- */
PROCEDURE aliveAgent.loop
    SAVEVALUE nAliveAgent, D$N
    ASSIGN processing, 0
    WHILE (1==1)
        MOVE {name:T3, text:"Alive agent [P$(nAgent)] active at t = AC1$"}
        ADVANCE 2
    ENDWHILE
    TERMINATE_VE
ENDPROCEDURE

/* ---- SLEEPING AGENT ---- */
PROCEDURE sleepingAgent.loop
    SAVEVALUE nSleepingAgent, D$N
    ASSIGN processing, 0
    MOVE {name:T5, text:"Sleeping agent [P$(nAgent)] active"}
    REST RestRoomSleepingAgent
    TERMINATE_VE
ENDPROCEDURE

/* ---- PROCEDURES INVOKED BY ENTITIES ---- */

PROCEDURE aliveAgent.process
    IF (P$processing==1)
        RETURN
    ENDIF
    ASSIGN processing, 1
    MOVE {name:T2, text:"SIGNAL → agent STARTS processing message: P$PARAM_A"}
    ADVANCE 80
    MOVE {name:T2, text:"SIGNAL → agent FINISHES processing message: P$PARAM_A"}
    ASSIGN processing, 0
    RETURN
ENDPROCEDURE

PROCEDURE aliveAgent.add
    ; PARAM_A = base value
    ; PARAM_B = increment
    RETURN_RESTORE P$PARAM_A + P$PARAM_B
ENDPROCEDURE

PROCEDURE sleepingAgent.add
    ; PARAM_A = base value
    ; PARAM_B = increment
    RETURN_RESTORE P$PARAM_A + P$PARAM_B
ENDPROCEDURE