Переглянути джерело

[hl] added thread name support

Nicolas Cannasse 2 роки тому
батько
коміт
b2f931bb3c
1 змінених файлів з 28 додано та 16 видалено
  1. 28 16
      std/hl/_std/sys/thread/Thread.hx

+ 28 - 16
std/hl/_std/sys/thread/Thread.hx

@@ -51,6 +51,17 @@ abstract Thread(ThreadImpl) from ThreadImpl {
 		return HaxeThread.current();
 		return HaxeThread.current();
 	}
 	}
 
 
+
+	public function setName( name : String ) {
+		#if (hl_ver >= version("1.13.0"))
+		set_name(@:privateAccess this.handle, @:privateAccess name.toUtf8());
+		#end
+	}
+
+	#if (hl_ver >= version("1.13.0"))
+	@:hlNative("?std", "thread_set_name") static function set_name( t : ThreadHandle, name : hl.Bytes ) {}
+	#end
+
 	function get_events():EventLoop {
 	function get_events():EventLoop {
 		if(this.events == null)
 		if(this.events == null)
 			throw new NoEventLoopException();
 			throw new NoEventLoopException();
@@ -66,25 +77,24 @@ abstract Thread(ThreadImpl) from ThreadImpl {
 private typedef ThreadHandle = hl.Abstract<"hl_thread">;
 private typedef ThreadHandle = hl.Abstract<"hl_thread">;
 
 
 private class HaxeThread {
 private class HaxeThread {
-	static var mainThreadHandle:ThreadHandle;
+
 	static var mainThread:HaxeThread;
 	static var mainThread:HaxeThread;
-	static var threads:Array<{thread:HaxeThread, handle:ThreadHandle}>;
+	static var threads:Array<HaxeThread>;
 	static var threadsMutex:Mutex;
 	static var threadsMutex:Mutex;
+	static var UID = 0;
 
 
 	static function __init__() {
 	static function __init__() {
-		mainThreadHandle = currentHandle();
 		threadsMutex = new Mutex();
 		threadsMutex = new Mutex();
 		threads = [];
 		threads = [];
-		mainThread = new HaxeThread();
+		mainThread = new HaxeThread(currentHandle());
 		mainThread.events = new EventLoop();
 		mainThread.events = new EventLoop();
 	}
 	}
 
 
+	var id = UID++;
 	public var events(default,null):Null<EventLoop>;
 	public var events(default,null):Null<EventLoop>;
+	var handle : ThreadHandle;
 	final messages = new Deque();
 	final messages = new Deque();
 
 
-	static var ids = 0;
-	var id = ids++;
-
 	@:hlNative("std", "thread_create")
 	@:hlNative("std", "thread_create")
 	static function createHandle(callb:Void->Void):ThreadHandle {
 	static function createHandle(callb:Void->Void):ThreadHandle {
 		return null;
 		return null;
@@ -97,32 +107,32 @@ private class HaxeThread {
 
 
 	static public function current():HaxeThread {
 	static public function current():HaxeThread {
 		var handle = currentHandle();
 		var handle = currentHandle();
-		if(handle == mainThreadHandle) {
+		if(handle == mainThread.handle) {
 			return mainThread;
 			return mainThread;
 		}
 		}
 		threadsMutex.acquire();
 		threadsMutex.acquire();
 		var thread = null;
 		var thread = null;
 		for(item in threads) {
 		for(item in threads) {
 			if(item.handle == handle) {
 			if(item.handle == handle) {
-				thread = item.thread;
+				thread = item;
 				break;
 				break;
 			}
 			}
 		}
 		}
 		if(thread == null) {
 		if(thread == null) {
-			thread = new HaxeThread();
-			threads.push({thread:thread, handle:handle});
+			thread = new HaxeThread(handle);
+			threads.push(thread);
 		}
 		}
 		threadsMutex.release();
 		threadsMutex.release();
 		return thread;
 		return thread;
 	}
 	}
 
 
 	public static function create(callb:()->Void, withEventLoop:Bool):Thread {
 	public static function create(callb:()->Void, withEventLoop:Bool):Thread {
-		var item = {handle:null, thread:new HaxeThread()};
+		var item = new HaxeThread(null);
 		threadsMutex.acquire();
 		threadsMutex.acquire();
 		threads.push(item);
 		threads.push(item);
 		threadsMutex.release();
 		threadsMutex.release();
 		if(withEventLoop)
 		if(withEventLoop)
-			item.thread.events = new EventLoop();
+			item.events = new EventLoop();
 		item.handle = createHandle(() -> {
 		item.handle = createHandle(() -> {
 			if(item.handle == null) {
 			if(item.handle == null) {
 				item.handle = currentHandle();
 				item.handle = currentHandle();
@@ -130,14 +140,14 @@ private class HaxeThread {
 			try {
 			try {
 				callb();
 				callb();
 				if(withEventLoop)
 				if(withEventLoop)
-					item.thread.events.loop();
+					item.events.loop();
 			} catch(e) {
 			} catch(e) {
 				dropThread(item);
 				dropThread(item);
 				throw e;
 				throw e;
 			}
 			}
 			dropThread(item);
 			dropThread(item);
 		});
 		});
-		return item.thread;
+		return item;
 	}
 	}
 
 
 	public static function runWithEventLoop(job:()->Void):Void {
 	public static function runWithEventLoop(job:()->Void):Void {
@@ -172,7 +182,9 @@ private class HaxeThread {
 		return messages.pop(block);
 		return messages.pop(block);
 	}
 	}
 
 
-	public function new() {}
+	public function new(h) {
+		handle = h;
+	}
 
 
 	public function sendMessage(msg:Dynamic) {
 	public function sendMessage(msg:Dynamic) {
 		messages.add(msg);
 		messages.add(msg);