瀏覽代碼

document native threads and add Thread.isNative.

ncannasse 1 天之前
父節點
當前提交
c8209dc38b
共有 1 個文件被更改,包括 19 次插入3 次删除
  1. 19 3
      std/sys/thread/Thread.hx

+ 19 - 3
std/sys/thread/Thread.hx

@@ -36,9 +36,10 @@ class Thread {
 	var messages : Deque<Dynamic>;
 	var messages : Deque<Dynamic>;
 
 
 	/**
 	/**
-		Event loop of this thread.
+		The events loop of this thread.
+		If this is a native thread, the events will be null.
 	**/
 	**/
-	public var events(default,null) : haxe.EventLoop;
+	public var events(default,null) : Null<haxe.EventLoop>;
 
 
 	/**
 	/**
 		Tells if we needs to wait for the thread to terminate before we stop the main loop (default:true).
 		Tells if we needs to wait for the thread to terminate before we stop the main loop (default:true).
@@ -50,6 +51,12 @@ class Thread {
 	**/
 	**/
 	public var name(default,set) : Null<String>;
 	public var name(default,set) : Null<String>;
 
 
+	/**
+		Tells if a thread is a native thread that is not managed by Haxe.
+		See `Thread.current` for details.
+	**/
+	public var isNative(default,null) : Bool;
+
 	function new(impl) {
 	function new(impl) {
 		this.impl = impl;
 		this.impl = impl;
 		if( impl != null ) this.name = ThreadImpl.getName(impl);
 		if( impl != null ) this.name = ThreadImpl.getName(impl);
@@ -70,6 +77,13 @@ class Thread {
 		messages.add(msg);
 		messages.add(msg);
 	}
 	}
 
 
+	public function disposeNative() {
+		if( !isNative ) return;
+		mutex.acquire();
+		threads.remove(this);
+		mutex.release();
+	}
+
 	public static function readMessage( blocking : Bool ) : Null<Dynamic> {
 	public static function readMessage( blocking : Bool ) : Null<Dynamic> {
 		var t = current();
 		var t = current();
 		if( t.messages == null ) {
 		if( t.messages == null ) {
@@ -82,6 +96,8 @@ class Thread {
 
 
 	/**
 	/**
 		Returns the current thread.
 		Returns the current thread.
+		If you are calling this function from a native thread that is not the main thread and was not created by `Thread.create`, this will return you
+		a native thread with a `null` EvenLoop and `isNative` set to true. You need to call `disposeNative()` on such value on thread termination.
 	**/
 	**/
 	public static function current():Thread {
 	public static function current():Thread {
 		var impl = ThreadImpl.current();
 		var impl = ThreadImpl.current();
@@ -94,7 +110,7 @@ class Thread {
 				return t;
 				return t;
 			}
 			}
 		var t = new Thread(impl);
 		var t = new Thread(impl);
-		// keep t.events = null because this is an unkown thread (not main and not created with create())
+		t.isNative = true;
 		threads.push(t);
 		threads.push(t);
 		mutex.release();
 		mutex.release();
 		return t;
 		return t;