Skip to content

Commit

Permalink
[PExplicit] Clean up control flow relating to pending state transitio…
Browse files Browse the repository at this point in the history
…n (exit/new-state-entry)

Renames blockedEntryState to blockedNewStateEntry
  • Loading branch information
aman-goel committed Apr 16, 2024
1 parent e14816e commit cb4b0ce
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public abstract class PMachine implements Serializable, Comparable<PMachine> {

private PContinuation blockedBy = null;
@Getter
private State blockedExitState;
private State blockedStateExit;
@Getter
private State blockedEntryState;
private State blockedNewStateEntry;
@Getter
private PValue<?> blockedEntryPayload;
private PValue<?> blockedNewStateEntryPayload;

/**
* TODO
Expand Down Expand Up @@ -321,16 +321,16 @@ void runEvent(PMessage message) {
// make sure event is handled (or is halt event)
if (currBlockedBy.getCaseEvents().contains(event.toString())) {
currBlockedBy.getHandleFun().apply(this, message);

// post process
currBlockedBy.runAfter(this);
} else if (event.isHaltMachineEvent()) {
this.halt();
} else {
Assert.fromModel(false,
String.format("Unexpected event %s received in a receive for machine %s in state %s",
event, this, this.currentState));
}

// post process
currBlockedBy.runAfter(this);
} else {
currentState.handleEvent(message, this);
}
Expand Down Expand Up @@ -378,9 +378,9 @@ public void raiseEvent(PEvent event) {
*/
public void processStateTransition(State newState, PValue<?> payload) {
if (isBlocked()) {
blockedExitState = currentState;
blockedEntryState = newState;
blockedEntryPayload = payload;
blockedStateExit = currentState;
blockedNewStateEntry = newState;
blockedNewStateEntryPayload = payload;
return;
}

Expand All @@ -389,8 +389,8 @@ public void processStateTransition(State newState, PValue<?> payload) {
exitCurrentState();

if (isBlocked()) {
blockedEntryState = newState;
blockedEntryPayload = payload;
blockedNewStateEntry = newState;
blockedNewStateEntryPayload = payload;
return;
}
}
Expand All @@ -405,7 +405,7 @@ public void exitCurrentState() {
return;
}

blockedExitState = null;
blockedStateExit = null;

PExplicitLogger.logStateExit(this);
currentState.exit(this);
Expand All @@ -417,8 +417,8 @@ public void enterNewState(State newState, PValue<?> payload) {
return;
}

blockedEntryState = null;
blockedEntryPayload = null;
blockedNewStateEntry = null;
blockedNewStateEntryPayload = null;

// change current state to new state
currentState = newState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,53 @@ public boolean isDeferred(PEvent event) {
return !event.isHaltMachineEvent() && !caseEvents.contains(event.toString());
}

/**
* Run after a machine executes this continuation
* First, if machine is unblocked, run any pending state exit function
* Second, if still unblocked, run any pending new state entry function
* Third, if still unblocked, clear all continuation variables
*
* @param machine Machine that just executed this continuation
*/
public void runAfter(PMachine machine) {
// process any pending exit state
// process pending state exit function first
if (!machine.isBlocked()) {
State blockedExitState = machine.getBlockedExitState();
State blockedExitState = machine.getBlockedStateExit();
if (blockedExitState != null) {
assert (machine.getCurrentState() == blockedExitState);
machine.exitCurrentState();
}
} else {
// blocked on a different continuation, do nothing
return;
}

// process any pending entry state
// at this point, there should be no pending state exit function
assert (machine.getBlockedStateExit() == null);

// process any pending new state entry function
if (!machine.isBlocked()) {
State blockedEntryState = machine.getBlockedEntryState();
State blockedEntryState = machine.getBlockedNewStateEntry();
if (blockedEntryState != null) {
machine.enterNewState(blockedEntryState, machine.getBlockedEntryPayload());
machine.enterNewState(blockedEntryState, machine.getBlockedNewStateEntryPayload());
}
} else {
// blocked on a different continuation, do nothing
return;
}

// cleanup continuations if unblocked completely
// at this point, there should be no pending exit or new state entry functions
assert (machine.getBlockedStateExit() == null);
assert (machine.getBlockedNewStateEntry() == null);

// cleanup continuation variables if unblocked completely
if (!machine.isBlocked()) {
for (PContinuation c : machine.getContinuationMap().values()) {
c.getClearFun().run();
}
} else {
// blocked on a different continuation, do nothing
return;
}
}

Expand Down

0 comments on commit cb4b0ce

Please sign in to comment.