Permalink
Browse files

Merge pull request #57 from jetztgradnet/clean-shutdown

Ensure clean shutdown without calling System.exit().
  • Loading branch information...
2 parents 8036428 + b5147c8 commit 2d0aec624d306456170ec63ae6e7a6e7f850e4fa @krestenkrab krestenkrab committed Jul 22, 2011
@@ -1041,6 +1041,14 @@ public static void shutdown() {
init_pid.sendb(tup);
}
}
+
+ /**
+ * Shutdown Kilim schedulers.
+ */
+ /*package*/ static void shutdownSchedulers() {
+ scheduler.shutdown();
+ async_scheduler.shutdown();
+ }
static public InputStream orig_in = System.in;
static public PrintStream orig_out = System.out;
@@ -1069,5 +1077,4 @@ public static void debug(boolean condition, String text) {
debug(text);
}
}
-
}
@@ -134,4 +134,9 @@ public static long read_timer(ERef ref) {
return -1;
}
+
+ public static void shutdown() {
+ send_timer.cancel();
+ send_timer.purge();
+ }
}
@@ -84,7 +84,6 @@ private static String guess_erl_root() {
ERT.log.severe("Cannot find OTPROOT directory\n"
+ "Pass -root <dir>, or set environment variable.");
- System.exit(-1);
return null;
}
@@ -162,7 +161,7 @@ public static void main(String[] args) throws Exception {
if (!(new File(erl_bootstrap_ebindir)).exists() && !erl_bootstrap_ebindir.startsWith(EFile.RESOURCE_PREFIX)) {
ERT.log.severe("No bootstrap classes at: "+erl_bootstrap_ebindir);
- System.exit(1);
+ throw new IllegalArgumentException("No bootstrap classes at: "+erl_bootstrap_ebindir);
}
OTPMain.main(ra.toArray(new String[ra.size()]));
@@ -24,7 +24,6 @@
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
-import java.util.logging.Level;
import java.util.logging.Logger;
import erjang.driver.EDriver;
@@ -83,10 +82,18 @@ public static void start_otp_ring0(ESeq argv) {
EAtom am_start = EAtom.intern("start");
ESeq env = ERT.NIL;
+ // launch first (initial) process, which starts OTP
EProc proc = new EProc(null, am_otp_ring0, am_start, ERT.NIL.cons(argv).cons(env));
ERT.run(proc);
+
+ // wait for this process to terminate
proc.joinb();
+
+ // shutdown schedulers, after the first (initial) process has stopped
+ ERT.shutdownSchedulers();
+ // shutdown timer task
+ ETimerTask.shutdown();
}
protected static ESeq process_args(String[] args) {
@@ -56,7 +56,7 @@ public ERLConsole(String title) {
static final int HINSET = 8;
public static void main(final String[] args) {
- final ERLConsole console = new ERLConsole("Erjang Console");
+ ERLConsole console = new ERLConsole("Erjang Console");
console.getContentPane().setLayout(new BorderLayout());
console.setSize(700, 600);
@@ -77,7 +77,7 @@ public static void main(final String[] args) {
pane.setBorder(BorderFactory.createLineBorder(Color.darkGray));
console.getContentPane().add(pane, BorderLayout.CENTER);
- StatusBar status = new StatusBar();
+ final StatusBar status = new StatusBar();
console.getContentPane().add(status, BorderLayout.SOUTH);
console.validate();
@@ -88,12 +88,20 @@ public void windowClosing(WindowEvent e) {
ERT.shutdown();
}
});
+ console.setVisible(true);
+
+ Timer timer = new Timer(true);
+ timer.schedule(new TimerTask() {
+
+ @Override
+ public void run() {
+ status.updateMemory();
+ }}, 1000, 1000);
erjang.OTPMain.add_driver(tty);
Thread t2 = new Thread() {
public void run() {
- console.setVisible(true);
try {
erjang.Main.main(args);
} catch (Exception e) {
@@ -108,8 +116,12 @@ public void run() {
} catch (InterruptedException ie) {
// ignore
}
-
- System.exit(0);
+ timer.cancel();
+ timer.purge();
+ timer = null;
+ console.setVisible(false);
+ console.dispose();
+ console = null;
}
private Font findFont(String otherwise, int style, int size, String[] families) {
@@ -138,7 +150,7 @@ private Font findFont(String otherwise, int style, int size, String[] families)
@SuppressWarnings("serial")
class StatusBar extends JPanel {
- private JLabel mem_label;
+ private JLabel mem_label;
private JLabel progress;
public StatusBar() {
@@ -168,13 +180,6 @@ public void run() {
mem_label = new JLabel();
add(mem_label, BorderLayout.CENTER);
-
- new Timer(true).schedule(new TimerTask() {
-
- @Override
- public void run() {
- StatusBar.this.updateMemory();
- }}, 1000, 1000);
}
void updateMemory() {
@@ -351,7 +351,6 @@ public void execute() throws Pausable {
if (log.isLoggable(Level.FINER)) {
log.finer("EXITING "+result);
- System.exit(1);
}
} finally {
@@ -271,28 +271,35 @@ public static EObject spawn(EProc proc, EObject mod, EObject fun, EObject args)
@BIF
public static EObject halt(EProc proc) {
- System.exit(0);
- return null;
+ return halt(proc, null);
}
@BIF
public static EObject halt(EProc proc, EObject value) {
- ESmall val = value.testSmall();
- if (val != null) {
- System.exit(val.value);
- return null;
+ int exitCode = 1;
+ String message = null;
+ if (value != null) {
+ ESmall val = value.testSmall();
+ if (val != null) {
+ exitCode = val.value;
+ }
+
+ EString str = value.testString();
+ if (str != null) {
+ message = str.stringValue();
+ }
}
- EString str = value.testString();
- if (str != null) {
- // TODO: create crash file
- log.severe("halting system: " + str.stringValue());
- System.exit(1);
+ // TODO: create crash file
+ if (message != null) {
+ log.severe("halting system: " + message);
}
+ else {
+ log.info("halting system");
+ }
+ ERT.shutdown();
- throw ERT.badarg(value);
-
-
+ return null;
}
@BIF

0 comments on commit 2d0aec6

Please sign in to comment.