瀏覽代碼

[hl] drop data of terminated threads (fixes #9875)

Aleksandr Kuzmenko 5 年之前
父節點
當前提交
60a62d6ed6
共有 2 個文件被更改,包括 27 次插入3 次删除
  1. 1 0
      extra/CHANGES.txt
  2. 26 3
      std/hl/_std/sys/thread/Thread.hx

+ 1 - 0
extra/CHANGES.txt

@@ -15,6 +15,7 @@
 	cs,java : fixed generation of `@:generic` classes with anonymous functions (#9799)
 	jvm : fixed sending/reading messages with `sys.thread.Threads` for threads created outside of Haxe (#9863)
 	flash : fixed loading swc libraries containing `Vector` without a type parameter (#9805)
+	hl : fixed messages being send to wrong threads with `sendMessage`/`readMessage` in `sys.thread.Thread` (#9875)
 
 2020-07-22 4.1.3
 

+ 26 - 3
std/hl/_std/sys/thread/Thread.hx

@@ -22,9 +22,9 @@
 
 package sys.thread;
 
-typedef ThreadHandle = hl.Abstract<"hl_thread">;
+private typedef ThreadHandle = hl.Abstract<"hl_thread">;
 
-abstract Thread(ThreadHandle) {
+abstract Thread(ThreadHandle) from ThreadHandle to ThreadHandle {
 	public function sendMessage(msg:Dynamic) {
 		getQueue(this).add(msg);
 	}
@@ -56,8 +56,31 @@ abstract Thread(ThreadHandle) {
 		return q;
 	}
 
+	static public function create(callb:()->Void):Thread {
+		return createHandle(() -> {
+			try {
+				callb();
+			} catch(e) {
+				dropThread(current());
+				throw e;
+			}
+			dropThread(current());
+		});
+	}
+
+	static inline function dropThread(handle:ThreadHandle) {
+		queue_mutex.acquire();
+		for (i => tq in threads_queues) {
+			if (tq.t == handle) {
+				threads_queues.splice(i, 1);
+				break;
+			}
+		}
+		queue_mutex.release();
+	}
+
 	@:hlNative("std", "thread_create")
-	public static function create(callb:Void->Void):Thread {
+	static function createHandle(callb:()->Void):ThreadHandle {
 		return null;
 	}