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/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 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 8e26826ac..49338fec4 100644 --- a/src/hx/Thread.cpp +++ b/src/hx/Thread.cpp @@ -1,5 +1,7 @@ #include +#if (HXCPP_API_LEVEL<500) + #include #include #include @@ -154,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); } @@ -404,6 +402,8 @@ int __hxcpp_GetCurrentThreadNumber() return hx::thread::Thread_obj::id(); } +#endif + // --- Atomic --- bool _hx_atomic_exchange_if(::cpp::Pointer inPtr, int test, int newVal ) @@ -419,6 +419,4 @@ int _hx_atomic_inc(::cpp::Pointer inPtr ) int _hx_atomic_dec(::cpp::Pointer inPtr ) { return _hx_atomic_sub(inPtr, 1); -} - - +} \ No newline at end of file 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(); diff --git a/src/hx/gc/Immix.cpp b/src/hx/gc/Immix.cpp index 74672a54a..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 @@ -4516,10 +4517,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 +4537,9 @@ class GlobalAllocator sThreadSleeping[inId] = false; - HxCreateDetachedThread(SThreadLoop, info); + std::thread thread(SThreadLoop, info); + + thread.detach(); #endif } 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 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 cd9790db5..2b396bcdd 100644 --- a/toolchain/haxe-target.xml +++ b/toolchain/haxe-target.xml @@ -8,6 +8,7 @@ + @@ -213,28 +214,7 @@ - - - - - - -
- - -
-
- -
-
- -
-
- -
-
- -
+