hashlink icon indicating copy to clipboard operation
hashlink copied to clipboard

HL thread hangs when using trace() and semaphores

Open 47rooks opened this issue 5 months ago • 1 comments

Haxe 4.3.3 HL 1.14

I have the following small test program:

package;

import sys.thread.Lock;
import sys.thread.Semaphore;
import sys.thread.Thread;

var produced = new Semaphore(0);
var consumed = new Semaphore(1);
var buffer = 0;
final NUM_ITERATIONS = 1000;

class Tracer {
	public function new() {}

	public function run():Void {
		var i = 0;
		while (i++ < NUM_ITERATIONS) {
			produced.acquire();
			trace('this is iteration ${i} and buffer contains ${buffer}');
			consumed.release();
		}
	}
}

class Producer {
	public function new() {}

	public function run():Void {
		var i = 0;
		while (i++ < NUM_ITERATIONS) {
			consumed.acquire();
			buffer = i;
			produced.release();
		}
	}
}

class ThreadTraceHangHL {
	public static function main() {
		var t = new Tracer();
		var p = new Producer();

		var l = new Lock();
		var tThread = Thread.create(() -> {
			trace('Tracer.run starting');
			t.run();
			trace('Tracer.run ending');

			// Notify Main.main() that I am done
			l.release();
		});

		var pThread = Thread.create(() -> {
			trace('Producer.run starting');
			p.run();
			trace('Producer.run ending');

			// Notify Main.main() that I am done
			l.release();
		});

		l.wait();
		l.wait();
	}
}

This I build with:

-hl hl\main.hl
-cp src
-main ThreadTraceHangHL

--next 

--cmd "D:\Program Files\HashLink1.14\hl.exe" hl\main.hl

When it runs it hangs after 126 iterations. If I reduce the amount printed it will run more iterations but it ultimately wedges. If I comment the trace() call or the semaphore operations it completes. If instead I compile it with hxcpp and run it it runs to completion, with the trace and semaphore ops in place.

Here's the cpp build file:

-cpp cpp
-main ThreadTraceHangHL
--class-path src

--next 

--cmd .\cpp\ThreadTraceHangHL.exe

47rooks avatar Mar 19 '24 03:03 47rooks

The issue is probably the lack of hl_blocking calls in hl_semaphore_acquire (and also hl_condition_acquire and hl_condition_wait).

Apprentice-Alchemist avatar Mar 19 '24 09:03 Apprentice-Alchemist

Yes they should block. Can you submit a PR ?

Le mar. 19 mars 2024 à 02:31, Zeta @.***> a écrit :

The issue is probably the lack of hl_blocking calls in hl_semaphore_acquire (and also hl_condition_acquire and hl_condition_wait ).

— Reply to this email directly, view it on GitHub https://github.com/HaxeFoundation/hashlink/issues/663#issuecomment-2006497749, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHZXQHDICHFCHBAWZSX5NLYZAAVVAVCNFSM6AAAAABE4YLI76VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMBWGQ4TONZUHE . You are receiving this because you are subscribed to this thread.Message ID: @.***>

ncannasse avatar Mar 19 '24 12:03 ncannasse