Proper fix for recent bug in C backend. Fix Thread.daemon flag

This commit is contained in:
Alexey Andreev 2019-03-27 18:29:02 +03:00
parent 0dc170dad2
commit d2a7e31eca
2 changed files with 19 additions and 13 deletions

View File

@ -41,10 +41,6 @@ public class TThread extends TObject implements TRunnable {
private boolean alive = true; private boolean alive = true;
TRunnable target; TRunnable target;
static {
mainThread.setDaemon(true);
}
public TThread() { public TThread() {
this(null, null); this(null, null);
} }
@ -65,7 +61,11 @@ public class TThread extends TObject implements TRunnable {
public void start() { public void start() {
if (PlatformDetector.isLowLevel()) { if (PlatformDetector.isLowLevel()) {
EventQueue.offer(() -> Fiber.start(this::runThread)); boolean daemon = this.daemon;
if (!daemon) {
Fiber.userThreadCount++;
}
EventQueue.offer(() -> Fiber.start(this::runThread, daemon));
} else { } else {
Platform.startThread(this::runThread); Platform.startThread(this::runThread);
} }
@ -207,8 +207,12 @@ public class TThread extends TObject implements TRunnable {
TThread current = currentThread(); TThread current = currentThread();
SleepHandler handler = new SleepHandler(current, callback); SleepHandler handler = new SleepHandler(current, callback);
if (PlatformDetector.isLowLevel()) { if (PlatformDetector.isLowLevel()) {
handler.scheduleId = EventQueue.offer(handler, System.currentTimeMillis() + millis); if (current.interruptedFlag) {
current.interruptHandler = handler; handler.interrupted();
} else {
handler.scheduleId = EventQueue.offer(handler, System.currentTimeMillis() + millis);
current.interruptHandler = handler;
}
} else { } else {
int intMillis = millis < Integer.MAX_VALUE ? (int) millis : Integer.MAX_VALUE; int intMillis = millis < Integer.MAX_VALUE ? (int) millis : Integer.MAX_VALUE;
handler.scheduleId = Platform.schedule(handler, intMillis); handler.scheduleId = Platform.schedule(handler, intMillis);

View File

@ -25,7 +25,7 @@ public class Fiber {
public static final int STATE_RUNNING = 0; public static final int STATE_RUNNING = 0;
public static final int STATE_SUSPENDING = 1; public static final int STATE_SUSPENDING = 1;
public static final int STATE_RESUMING = 2; public static final int STATE_RESUMING = 2;
private static int daemonCount = 1; public static int userThreadCount = 1;
private int[] intValues; private int[] intValues;
private int intTop; private int intTop;
@ -41,11 +41,13 @@ public class Fiber {
private FiberRunner runner; private FiberRunner runner;
private Object result; private Object result;
private Throwable exception; private Throwable exception;
private boolean daemon;
private static Fiber current; private static Fiber current;
private Fiber(FiberRunner runner) { private Fiber(FiberRunner runner, boolean daemon) {
this.runner = runner; this.runner = runner;
this.daemon = daemon;
} }
public void push(int value) { public void push(int value) {
@ -207,12 +209,12 @@ public class Fiber {
static native void setCurrentThread(Thread thread); static native void setCurrentThread(Thread thread);
public static void start(FiberRunner runner) { public static void start(FiberRunner runner, boolean daemon) {
new Fiber(runner).start(); new Fiber(runner, daemon).start();
} }
static void startMain() { static void startMain() {
start(Fiber::runMain); start(Fiber::runMain, false);
} }
static native void runMain(); static native void runMain();
@ -222,7 +224,7 @@ public class Fiber {
current = this; current = this;
runner.run(); runner.run();
current = former; current = former;
if (!isSuspending() && Thread.currentThread().isDaemon() && --daemonCount == 0) { if (!isSuspending() && !daemon && --userThreadCount == 0) {
EventQueue.stop(); EventQueue.stop();
} }
} }