From d32bdb48c4bdc0ab38cda40169414724ce08a20c Mon Sep 17 00:00:00 2001 From: Aidan Lee Date: Sun, 24 May 2026 10:44:41 +0100 Subject: [PATCH 1/7] Initial ifdef removal of old thread impl --- include/hx/StdLibs.h | 23 ++++++++++---------- src/hx/Thread.cpp | 4 +++- toolchain/haxe-target.xml | 46 ++++++++++++++++++++++----------------- 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/include/hx/StdLibs.h b/include/hx/StdLibs.h index a62ee4088..a26bbd6ae 100644 --- a/include/hx/StdLibs.h +++ b/include/hx/StdLibs.h @@ -299,15 +299,14 @@ double __hxcpp_time_stamp(); // --- vm/threading -------------------------------------------------------------------- -#if (HXCPP_API_LEVEL>=500) -Dynamic __hxcpp_thread_create(hx::Callable inFunc); -#else +#if (HXCPP_API_LEVEL<500) + Dynamic __hxcpp_thread_create(Dynamic inFunc); -#endif + Dynamic __hxcpp_thread_current(); void __hxcpp_thread_send(Dynamic inThread, Dynamic inMessage); Dynamic __hxcpp_thread_read_message(bool inBlocked); -bool __hxcpp_is_current_thread(hx::Object *inThread); +bool __hxcpp_is_current_thread(hx::Object* inThread); Dynamic __hxcpp_mutex_create(); void __hxcpp_mutex_acquire(Dynamic); @@ -324,21 +323,23 @@ void __hxcpp_condition_acquire(Dynamic); bool __hxcpp_condition_try_acquire(Dynamic); void __hxcpp_condition_release(Dynamic); void __hxcpp_condition_wait(Dynamic); -bool __hxcpp_condition_timed_wait(Dynamic,double); +bool __hxcpp_condition_timed_wait(Dynamic, double); void __hxcpp_condition_signal(Dynamic); void __hxcpp_condition_broadcast(Dynamic); Dynamic __hxcpp_lock_create(); -bool __hxcpp_lock_wait(Dynamic inlock,double inTime); +bool __hxcpp_lock_wait(Dynamic inlock, double inTime); void __hxcpp_lock_release(Dynamic inlock); Dynamic __hxcpp_deque_create(); -void __hxcpp_deque_add(Dynamic q,Dynamic inVal); -void __hxcpp_deque_push(Dynamic q,Dynamic inVal); -Dynamic __hxcpp_deque_pop(Dynamic q,bool block); +void __hxcpp_deque_add(Dynamic q, Dynamic inVal); +void __hxcpp_deque_push(Dynamic q, Dynamic inVal); +Dynamic __hxcpp_deque_pop(Dynamic q, bool block); Dynamic __hxcpp_tls_get(int inID); -void __hxcpp_tls_set(int inID,Dynamic inVal); +void __hxcpp_tls_set(int inID, Dynamic inVal); + +#endif bool _hx_atomic_exchange_if(::cpp::Pointer inPtr, int test, int newVal ); int _hx_atomic_inc(::cpp::Pointer inPtr ); diff --git a/src/hx/Thread.cpp b/src/hx/Thread.cpp index 8e26826ac..99f30f7b7 100644 --- a/src/hx/Thread.cpp +++ b/src/hx/Thread.cpp @@ -1,5 +1,7 @@ #include +#if (HXCPP_API_LEVEL<500) + #include #include #include @@ -421,4 +423,4 @@ int _hx_atomic_dec(::cpp::Pointer inPtr ) return _hx_atomic_sub(inPtr, 1); } - +#endif \ No newline at end of file diff --git a/toolchain/haxe-target.xml b/toolchain/haxe-target.xml index cd9790db5..22dd685c9 100644 --- a/toolchain/haxe-target.xml +++ b/toolchain/haxe-target.xml @@ -213,27 +213,33 @@ - - - - - - -
- - -
-
- -
-
- +
+ + + + + + +
+ + +
+
+ +
+
+ +
+
+ +
+
+ +
-
- -
-
- + +
+
From 2a19a4ed7f64e2c6ad0ec2edcdb30c2c76a9b7ad Mon Sep 17 00:00:00 2001 From: Aidan Lee Date: Sun, 24 May 2026 14:59:19 +0100 Subject: [PATCH 2/7] separate file technique --- toolchain/haxe-target-threads.xml | 30 ++++++++++++++++++++++++++++++ toolchain/haxe-target.xml | 30 ++---------------------------- 2 files changed, 32 insertions(+), 28 deletions(-) create mode 100644 toolchain/haxe-target-threads.xml diff --git a/toolchain/haxe-target-threads.xml b/toolchain/haxe-target-threads.xml new file mode 100644 index 000000000..445d9acf6 --- /dev/null +++ b/toolchain/haxe-target-threads.xml @@ -0,0 +1,30 @@ + + + + + + + + + +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
\ No newline at end of file diff --git a/toolchain/haxe-target.xml b/toolchain/haxe-target.xml index 22dd685c9..2b396bcdd 100644 --- a/toolchain/haxe-target.xml +++ b/toolchain/haxe-target.xml @@ -8,6 +8,7 @@ + @@ -213,34 +214,7 @@ -
- - - - - - -
- - -
-
- -
-
- -
-
- -
-
- -
-
- -
- -
+ From 62eb6354c9308b434ca4a91d40c1f0201aff5c0d Mon Sep 17 00:00:00 2001 From: Aidan Lee Date: Sun, 24 May 2026 15:35:27 +0100 Subject: [PATCH 3/7] Move various internal parts to use std::thread also change ifdef end to still include atomic --- src/hx/Profiler.cpp | 12 ++++++------ src/hx/Telemetry.cpp | 23 ++++++++--------------- src/hx/Thread.cpp | 6 +++--- src/hx/gc/Immix.cpp | 7 ++++--- 4 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/hx/Profiler.cpp b/src/hx/Profiler.cpp index d5cd809d3..b266041bc 100644 --- a/src/hx/Profiler.cpp +++ b/src/hx/Profiler.cpp @@ -1,9 +1,10 @@ #include #include #include -#include #include #include +#include +#include #ifdef HX_WINRT @@ -49,7 +50,8 @@ class Profiler gThreadRefCount += 1; if (gThreadRefCount == 1) { - HxCreateDetachedThread(ProfileMainLoop, 0); + std::thread thread(ProfileMainLoop); + thread.detach(); } } @@ -214,18 +216,16 @@ class Profiler int childrenPlusSelf; }; - static THREAD_FUNC_TYPE ProfileMainLoop(void *) + static void ProfileMainLoop() { int millis = 1; while (gThreadRefCount > 0) { - HxSleep(millis); + std::this_thread::sleep_for(std::chrono::milliseconds(millis)); int count = gProfileClock + 1; gProfileClock = (count < 0) ? 0 : count; } - - THREAD_FUNC_RET } String mDumpFile; diff --git a/src/hx/Telemetry.cpp b/src/hx/Telemetry.cpp index c49035061..3dd017322 100644 --- a/src/hx/Telemetry.cpp +++ b/src/hx/Telemetry.cpp @@ -8,7 +8,8 @@ #include #include #include - +#include +#include namespace hx { @@ -67,7 +68,9 @@ class Telemetry gThreadRefCount += 1; if (gThreadRefCount == 1) { - HxCreateDetachedThread(ProfileMainLoop, 0); + std::thread thread(ProfileMainLoop); + + thread.detach(); } } @@ -284,26 +287,16 @@ class Telemetry int GetNameIdx(const char *fullName); int ComputeCallStackId(); - static THREAD_FUNC_TYPE ProfileMainLoop(void *) + static void ProfileMainLoop() { int millis = 1; - while (gThreadRefCount > 0) { -#ifdef HX_WINDOWS - Sleep(millis); -#else - struct timespec t; - struct timespec tmp; - t.tv_sec = 0; - t.tv_nsec = millis * 1000000; - nanosleep(&t, &tmp); -#endif + while (gThreadRefCount > 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(millis)); int count = gProfileClock + 1; gProfileClock = (count < 0) ? 0 : count; } - - THREAD_FUNC_RET } StackContext *stack; diff --git a/src/hx/Thread.cpp b/src/hx/Thread.cpp index 99f30f7b7..25614e4c0 100644 --- a/src/hx/Thread.cpp +++ b/src/hx/Thread.cpp @@ -406,6 +406,8 @@ int __hxcpp_GetCurrentThreadNumber() return hx::thread::Thread_obj::id(); } +#endif + // --- Atomic --- bool _hx_atomic_exchange_if(::cpp::Pointer inPtr, int test, int newVal ) @@ -421,6 +423,4 @@ int _hx_atomic_inc(::cpp::Pointer inPtr ) int _hx_atomic_dec(::cpp::Pointer inPtr ) { return _hx_atomic_sub(inPtr, 1); -} - -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/hx/gc/Immix.cpp b/src/hx/gc/Immix.cpp index 74672a54a..5a319cdf3 100644 --- a/src/hx/gc/Immix.cpp +++ b/src/hx/gc/Immix.cpp @@ -4516,10 +4516,9 @@ class GlobalAllocator } } - static THREAD_FUNC_TYPE SThreadLoop( void *inInfo ) + static void SThreadLoop( void *inInfo ) { sGlobalAlloc->ThreadLoop((int)(size_t)inInfo); - THREAD_FUNC_RET; } void CreateWorker(int inId) @@ -4537,7 +4536,9 @@ class GlobalAllocator sThreadSleeping[inId] = false; - HxCreateDetachedThread(SThreadLoop, info); + std::thread thread(SThreadLoop, info); + + thread.detach(); #endif } From f587c5d01e96ad62e5cabbdc509018457eaf3cb2 Mon Sep 17 00:00:00 2001 From: Aidan Lee Date: Sun, 24 May 2026 16:17:53 +0100 Subject: [PATCH 4/7] fix cppia thread create global --- src/hx/Thread.cpp | 4 ---- src/hx/cppia/GlobalBuiltin.cpp | 14 ++++++-------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/hx/Thread.cpp b/src/hx/Thread.cpp index 25614e4c0..49338fec4 100644 --- a/src/hx/Thread.cpp +++ b/src/hx/Thread.cpp @@ -156,11 +156,7 @@ Dynamic __hxcpp_deque_pop(Dynamic q,bool block) // --- Thread ---------------------------------------------------------- -#if (HXCPP_API_LEVEL>=500) -Dynamic __hxcpp_thread_create(hx::Callable inStart) -#else Dynamic __hxcpp_thread_create(Dynamic inStart) -#endif { return hx::thread::Thread_obj::create(inStart); } diff --git a/src/hx/cppia/GlobalBuiltin.cpp b/src/hx/cppia/GlobalBuiltin.cpp index 315c56fc3..e4e1ab1eb 100644 --- a/src/hx/cppia/GlobalBuiltin.cpp +++ b/src/hx/cppia/GlobalBuiltin.cpp @@ -1,6 +1,10 @@ #include #include "Cppia.h" +#if (HXCPP_API_LEVEL>=500) +#include +#endif + namespace hx { @@ -382,17 +386,11 @@ CppiaExpr *createGlobalBuiltin(CppiaExpr *src, String function, Expressions &ioE { if (ioExpressions.size()==1) #if (HXCPP_API_LEVEL>=500) - return new ObjectBuiltin1, Dynamic, __hxcpp_thread_create>(src, ioExpressions); + return new ObjectBuiltin1, hx::thread::Thread, hx::thread::Thread_obj::create>(src, ioExpressions); #else - return new ObjectBuiltin1(src, ioExpressions); + return new ObjectBuiltin1(src, ioExpressions); #endif } - if (function==HX_CSTRING("__hxcpp_thread_send") ) - { - if (ioExpressions.size()==2) - return new VoidBuiltin2(src,ioExpressions); - } - printf("Unknown function : %s(%d)\n", function.out_str(), (int)ioExpressions.size() ); throw (HX_CSTRING("Unknown global:") + function).utf8_str(); From e6f4203b228fb226fa15dd154a12e338bf08b536 Mon Sep 17 00:00:00 2001 From: Aidan Lee Date: Sun, 24 May 2026 16:35:28 +0100 Subject: [PATCH 5/7] Add thread include to immix --- src/hx/gc/Immix.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/hx/gc/Immix.cpp b/src/hx/gc/Immix.cpp index 5a319cdf3..0500b8fba 100644 --- a/src/hx/gc/Immix.cpp +++ b/src/hx/gc/Immix.cpp @@ -8,6 +8,7 @@ #include "GcRegCapture.h" #include #include +#include #include #ifdef EMSCRIPTEN From 75ff615ecfe6e85c351d4bafcb29f5217b10fe2d Mon Sep 17 00:00:00 2001 From: Aidan Lee Date: Sun, 24 May 2026 20:19:36 +0100 Subject: [PATCH 6/7] Add scratch tests --- test/native/Native.hx | 2 ++ test/native/tests/TestScratch.hx | 53 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 test/native/tests/TestScratch.hx diff --git a/test/native/Native.hx b/test/native/Native.hx index 5b300a23b..de2b8dfb5 100644 --- a/test/native/Native.hx +++ b/test/native/Native.hx @@ -18,6 +18,8 @@ class Native new tests.TestNativeEnum(), #if (haxe_ver>=5) + new tests.TestScratch(), + new tests.marshalling.classes.TestLocalValueType(), new tests.marshalling.classes.TestClassValueType(), new tests.marshalling.classes.TestInterfaceValueType(), diff --git a/test/native/tests/TestScratch.hx b/test/native/tests/TestScratch.hx new file mode 100644 index 000000000..264a160b4 --- /dev/null +++ b/test/native/tests/TestScratch.hx @@ -0,0 +1,53 @@ +package tests; + +import cpp.Scratch; +import utest.Test; +import utest.Assert; + +using haxe.Int64; + +class TestScratch extends Test { + function test_zeroed_small_alloc() { + final size = 16; + final alloc = Scratch.alloc(size); + + Assert.equals(size, alloc.view.length.toInt()); + + for (i in 0...size.toInt()) { + Assert.equals(0, alloc.view[i]); + } + } + + function test_zeroed_large_alloc() { + final size = 100_000; + final alloc = Scratch.alloc(size); + + Assert.equals(size, alloc.view.length.toInt()); + + for (i in 0...size.toInt()) { + Assert.equals(0, alloc.view[i]); + } + } + + function test_contents_wiped() { + { + final size = 16; + final alloc = Scratch.alloc(size); + + Assert.equals(size, alloc.view.length.toInt()); + + alloc.view.fill(7); + } + + { + final size = 16; + final alloc = Scratch.alloc(size); + + Assert.equals(size, alloc.view.length.toInt()); + + for (i in 0...size.toInt()) { + Assert.equals(0, alloc.view[i]); + } + } + } +} \ No newline at end of file From c1ec08a2f72452cf9f98aa440a1f878c5fbe93d8 Mon Sep 17 00:00:00 2001 From: Aidan Lee Date: Mon, 25 May 2026 10:38:20 +0100 Subject: [PATCH 7/7] Remove un-needed virtual functions already virtual on the base --- include/hx/thread/Thread.hpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/include/hx/thread/Thread.hpp b/include/hx/thread/Thread.hpp index b34fcc941..ee857bd2a 100644 --- a/include/hx/thread/Thread.hpp +++ b/include/hx/thread/Thread.hpp @@ -25,13 +25,6 @@ namespace hx virtual String getName() = 0; virtual void setName(const String& name) = 0; - - virtual String toString() override = 0; - - virtual void __Mark(HX_MARK_PARAMS) override = 0; -#ifdef HXCPP_VISIT_ALLOCS - virtual void __Visit(HX_VISIT_PARAMS) override = 0; -#endif }; } } \ No newline at end of file