Browse Source

use tls for faster Thread.current(), add toString() and set main thread name.

ncannasse 1 day ago
parent
commit
6234e542a3
1 changed files with 21 additions and 11 deletions
  1. 21 11
      std/sys/thread/Thread.hx

+ 21 - 11
std/sys/thread/Thread.hx

@@ -68,6 +68,10 @@ class Thread {
 		return n;
 		return n;
 	}
 	}
 
 
+	public function toString() {
+		return "Thread#"+(name ?? Std.string(impl));
+	}
+
 	public function sendMessage( msg : Dynamic ) {
 	public function sendMessage( msg : Dynamic ) {
 		if( messages == null ) {
 		if( messages == null ) {
 			mutex.acquire();
 			mutex.acquire();
@@ -79,9 +83,15 @@ class Thread {
 
 
 	public function disposeNative() {
 	public function disposeNative() {
 		if( !isNative ) return;
 		if( !isNative ) return;
+		dispose();
+	}
+
+
+	function dispose() {
 		mutex.acquire();
 		mutex.acquire();
 		threads.remove(this);
 		threads.remove(this);
 		mutex.release();
 		mutex.release();
+		currentTLS.value = null;
 	}
 	}
 
 
 	public static function readMessage( blocking : Bool ) : Null<Dynamic> {
 	public static function readMessage( blocking : Bool ) : Null<Dynamic> {
@@ -94,25 +104,24 @@ class Thread {
 		return t.messages.pop(blocking);
 		return t.messages.pop(blocking);
 	}
 	}
 
 
+	static var currentTLS : Tls<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
 		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.
 		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 t = currentTLS.value;
+		if( t != null )
+			return t;
 		var impl = ThreadImpl.current();
 		var impl = ThreadImpl.current();
-		if( impl == mainThread.impl )
-			return mainThread;
-		mutex.acquire();
-		for( t in threads )
-			if( t.impl == impl ) {
-				mutex.release();
-				return t;
-			}
 		var t = new Thread(impl);
 		var t = new Thread(impl);
 		t.isNative = true;
 		t.isNative = true;
+		mutex.acquire();
 		threads.push(t);
 		threads.push(t);
 		mutex.release();
 		mutex.release();
+		currentTLS.value = t;
 		return t;
 		return t;
 	}
 	}
 
 
@@ -144,9 +153,7 @@ class Thread {
 			} catch( e ) {
 			} catch( e ) {
 				exception = e;
 				exception = e;
 			}
 			}
-			mutex.acquire();
-			threads.remove(t);
-			mutex.release();
+			t.dispose();
 			@:privateAccess main().events.wakeup();
 			@:privateAccess main().events.wakeup();
 			if( exception != null )
 			if( exception != null )
 				t.onAbort(exception);
 				t.onAbort(exception);
@@ -180,7 +187,10 @@ class Thread {
 		mutex = new Mutex();
 		mutex = new Mutex();
 		threads = [];
 		threads = [];
 		mainThread = new Thread(ThreadImpl.current());
 		mainThread = new Thread(ThreadImpl.current());
+		mainThread.name = "Main";
 		mainThread.events = haxe.EventLoop.main;
 		mainThread.events = haxe.EventLoop.main;
+		currentTLS = new Tls();
+		currentTLS.value = mainThread;
 	}
 	}
 
 
 }
 }