Permalink
Browse files

Simplify pstate: use exit_reason for signalling exits.

Also, rename SENDING_EXIT -> EXITING.
  • Loading branch information...
1 parent 4b7b21e commit 5737ecbe3c8478fbed3585d5eb8b468490162fc3 @esstrifork esstrifork committed Jan 29, 2014
Showing with 25 additions and 18 deletions.
  1. +5 −9 src/main/java/erjang/EProc.java
  2. +20 −9 src/main/java/erjang/ETask.java
@@ -28,8 +28,6 @@
import java.util.logging.Logger;
import kilim.Pausable;
-import erjang.m.erlang.ErlFun;
-import erjang.m.erlang.ErlProc;
import erjang.m.java.JavaObject;
/**
@@ -521,7 +519,7 @@ protected void do_proc_termination(EObject result) throws Pausable {
protected void process_incoming_exit(EHandle from, EObject reason, boolean is_erlang_exit2) throws Pausable
{
- if (pstate == STATE.EXIT_SIG || pstate == STATE.SENDING_EXIT || pstate == STATE.DONE) {
+ if (pstate == STATE.EXITING || pstate == STATE.DONE) {
if (log.isLoggable(Level.FINE)) {
log.fine("Ignoring incoming exit reason="+reason+", as we're already exiting reason="+exit_reason);
}
@@ -533,9 +531,9 @@ protected void process_incoming_exit(EHandle from, EObject reason, boolean is_er
}
if (is_erlang_exit2) {
+ EObject exit_reason_to_set;
if (reason == am_kill) {
- exit_reason = am_killed;
-
+ exit_reason_to_set = am_killed;
} else if (trap_exit == ERT.TRUE) {
// we're trapping exits, so we in stead send an {'EXIT', from,
// reason} to self
@@ -546,10 +544,10 @@ protected void process_incoming_exit(EHandle from, EObject reason, boolean is_er
return;
} else {
- this.exit_reason = reason;
+ exit_reason_to_set = reason;
}
- this.pstate = STATE.EXIT_SIG;
+ this.exit_reason = exit_reason_to_set;
this.killer = new Throwable();
this.resume();
return;
@@ -570,15 +568,13 @@ protected void process_incoming_exit(EHandle from, EObject reason, boolean is_er
} else if (reason == am_kill) {
this.exit_reason = am_killed;
- this.pstate = STATE.EXIT_SIG;
this.killer = new Throwable();
this.resume();
} else if (reason != am_normal) {
// System.err.println("kill signal: " +reason + " from "+from);
// try to kill this thread
this.exit_reason = reason;
- this.pstate = STATE.EXIT_SIG;
this.killer = new Throwable();
this.resume();
}
@@ -53,11 +53,22 @@
/*==================== Typedefs ==============================*/
public enum STATE {
- INIT, RUNNING, EXIT_SIG, SENDING_EXIT, DONE
+ INIT, RUNNING, EXITING, DONE
};
/*==================== Process state ====================================*/
+ /** Process lifecycle:
+ *
+ * INIT -----> RUNNING --------------------> DONE
+ * \ \ /
+ * v v /
+ * INIT w/ RUNNING w/ /
+ * exit_reason exit_reason /
+ * \ \ /
+ * -------------------> EXITING --
+ *
+ */
protected volatile STATE pstate = STATE.INIT;
private AtomicReference<PersistentHashSet<EHandle>>
@@ -75,8 +86,11 @@
/** Reduction counter. */
private int reds;
- /** null when task is alive or terminated normally; non-null when task is terminated/terminating abnormally. */
- protected EObject exit_reason;
+ /** Exit signal and reason -
+ * null when task is alive or terminated normally; non-null when task is terminated/terminating abnormally.
+ * Killer and exit_reason are always set together.
+ * */
+ protected volatile EObject exit_reason;
protected Throwable killer;
private Mailbox<EObject> print_trace;
@@ -264,9 +278,7 @@ public void checkKill() {
* trapping)
*/
public final void check_exit() {
- if (this.pstate == STATE.EXIT_SIG) {
- do_check_exit();
- }
+ do_check_exit();
if (print_trace != null) {
Mailbox<EObject> mb = print_trace;
@@ -285,7 +297,7 @@ public final void check_exit() {
/** because the above is called a bazillion times, we split this out to make it easier to inline */
private void do_check_exit() throws ErlangExitSignal {
if (exit_reason != null) {
- this.pstate = STATE.SENDING_EXIT;
+ this.pstate = STATE.EXITING;
EObject reason = exit_reason;
//System.err.println("---- [" + killer + "]");
ErlangExitSignal e = new ErlangExitSignal(reason, killer);
@@ -401,8 +413,7 @@ public final void send_exit(EHandle from, EObject reason, boolean is_erlang_exit
// we have already received one exit signal, ignore
// subsequent ones...
- case EXIT_SIG:
- case SENDING_EXIT:
+ case EXITING:
// TODO: warn that this process is not yet dead. why?
return;

0 comments on commit 5737ecb

Please sign in to comment.