|
@@ -24,41 +24,84 @@ package sys.thread;
|
|
|
|
|
|
import java.Lib;
|
|
import java.Lib;
|
|
import java.lang.Runnable;
|
|
import java.lang.Runnable;
|
|
|
|
+import java.util.WeakHashMap;
|
|
|
|
+import java.util.Collections;
|
|
|
|
+import java.lang.Thread as JavaThread;
|
|
|
|
|
|
-abstract Thread(HaxeThread) from HaxeThread to java.lang.Thread {
|
|
|
|
|
|
+abstract Thread(HaxeThread) from HaxeThread {
|
|
inline function new(t:HaxeThread) {
|
|
inline function new(t:HaxeThread) {
|
|
this = t;
|
|
this = t;
|
|
}
|
|
}
|
|
|
|
|
|
- public static function create(callb:Void->Void):Thread {
|
|
|
|
- var ret = new HaxeThread((cast callb : Runnable));
|
|
|
|
- ret.setDaemon(true);
|
|
|
|
- ret.start();
|
|
|
|
- return new Thread(ret);
|
|
|
|
|
|
+ public static inline function create(callb:()->Void):Thread {
|
|
|
|
+ return HaxeThread.create(callb);
|
|
}
|
|
}
|
|
|
|
|
|
- public static function current():Thread {
|
|
|
|
- return new Thread(Std.downcast(java.lang.Thread.currentThread(), HaxeThread));
|
|
|
|
|
|
+ public static inline function current():Thread {
|
|
|
|
+ return HaxeThread.get(JavaThread.currentThread());
|
|
}
|
|
}
|
|
|
|
|
|
- public static function readMessage(block:Bool):Dynamic {
|
|
|
|
- return current().getHandle().messages.pop(block);
|
|
|
|
|
|
+ public static inline function readMessage(block:Bool):Dynamic {
|
|
|
|
+ return current().getHandle().readMessage(block);
|
|
}
|
|
}
|
|
|
|
|
|
public inline function sendMessage(msg:Dynamic):Void {
|
|
public inline function sendMessage(msg:Dynamic):Void {
|
|
this.sendMessage(msg);
|
|
this.sendMessage(msg);
|
|
}
|
|
}
|
|
|
|
|
|
- private inline function getHandle():HaxeThread {
|
|
|
|
|
|
+ inline function getHandle():HaxeThread {
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-class HaxeThread extends java.lang.Thread {
|
|
|
|
|
|
+private class HaxeThread {
|
|
|
|
+ static final nativeThreads = Collections.synchronizedMap(new WeakHashMap<JavaThread,HaxeThread>());
|
|
|
|
+ static final mainJavaThread = JavaThread.currentThread();
|
|
|
|
+ static final mainHaxeThread = new HaxeThread();
|
|
|
|
|
|
public final messages = new Deque<Dynamic>();
|
|
public final messages = new Deque<Dynamic>();
|
|
|
|
|
|
|
|
+ public static function create(callb:()->Void):HaxeThread {
|
|
|
|
+ var hx = new HaxeThread();
|
|
|
|
+ var thread = new NativeHaxeThread(hx, callb);
|
|
|
|
+ thread.setDaemon(true);
|
|
|
|
+ thread.start();
|
|
|
|
+ return hx;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static function get(javaThread:JavaThread):HaxeThread {
|
|
|
|
+ if(javaThread == mainJavaThread) {
|
|
|
|
+ return mainHaxeThread;
|
|
|
|
+ } else if(javaThread is NativeHaxeThread) {
|
|
|
|
+ return (cast javaThread:NativeHaxeThread).haxeThread;
|
|
|
|
+ } else {
|
|
|
|
+ switch nativeThreads.get(javaThread) {
|
|
|
|
+ case null:
|
|
|
|
+ var hx = new HaxeThread();
|
|
|
|
+ nativeThreads.put(javaThread, hx);
|
|
|
|
+ return hx;
|
|
|
|
+ case hx:
|
|
|
|
+ return hx;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function new() {}
|
|
|
|
+
|
|
public inline function sendMessage(msg:Dynamic):Void {
|
|
public inline function sendMessage(msg:Dynamic):Void {
|
|
messages.add(msg);
|
|
messages.add(msg);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public inline function readMessage(block:Bool):Dynamic {
|
|
|
|
+ return messages.pop(block);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+private class NativeHaxeThread extends java.lang.Thread {
|
|
|
|
+ public final haxeThread:HaxeThread;
|
|
|
|
+
|
|
|
|
+ public function new(haxeThread:HaxeThread, callb:()->Void) {
|
|
|
|
+ super((cast callb:Runnable));
|
|
|
|
+ this.haxeThread = haxeThread;
|
|
|
|
+ }
|
|
}
|
|
}
|