Skip to content

Commit 70933dc

Browse files
Aidan63Aidan Lee
andauthored
Thread Changes Cleanup (HaxeFoundation#1345)
* Initial ifdef removal of old thread impl * separate file technique * Move various internal parts to use std::thread also change ifdef end to still include atomic * fix cppia thread create global * Add thread include to immix * Add scratch tests * Remove un-needed virtual functions already virtual on the base --------- Co-authored-by: Aidan Lee <aidan.lee@evcam.com>
1 parent aef979a commit 70933dc

11 files changed

Lines changed: 129 additions & 79 deletions

File tree

include/hx/StdLibs.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,14 @@ double __hxcpp_time_stamp();
299299

300300
// --- vm/threading --------------------------------------------------------------------
301301

302-
#if (HXCPP_API_LEVEL>=500)
303-
Dynamic __hxcpp_thread_create(hx::Callable<void()> inFunc);
304-
#else
302+
#if (HXCPP_API_LEVEL<500)
303+
305304
Dynamic __hxcpp_thread_create(Dynamic inFunc);
306-
#endif
305+
307306
Dynamic __hxcpp_thread_current();
308307
void __hxcpp_thread_send(Dynamic inThread, Dynamic inMessage);
309308
Dynamic __hxcpp_thread_read_message(bool inBlocked);
310-
bool __hxcpp_is_current_thread(hx::Object *inThread);
309+
bool __hxcpp_is_current_thread(hx::Object* inThread);
311310

312311
Dynamic __hxcpp_mutex_create();
313312
void __hxcpp_mutex_acquire(Dynamic);
@@ -324,21 +323,23 @@ void __hxcpp_condition_acquire(Dynamic);
324323
bool __hxcpp_condition_try_acquire(Dynamic);
325324
void __hxcpp_condition_release(Dynamic);
326325
void __hxcpp_condition_wait(Dynamic);
327-
bool __hxcpp_condition_timed_wait(Dynamic,double);
326+
bool __hxcpp_condition_timed_wait(Dynamic, double);
328327
void __hxcpp_condition_signal(Dynamic);
329328
void __hxcpp_condition_broadcast(Dynamic);
330329

331330
Dynamic __hxcpp_lock_create();
332-
bool __hxcpp_lock_wait(Dynamic inlock,double inTime);
331+
bool __hxcpp_lock_wait(Dynamic inlock, double inTime);
333332
void __hxcpp_lock_release(Dynamic inlock);
334333

335334
Dynamic __hxcpp_deque_create();
336-
void __hxcpp_deque_add(Dynamic q,Dynamic inVal);
337-
void __hxcpp_deque_push(Dynamic q,Dynamic inVal);
338-
Dynamic __hxcpp_deque_pop(Dynamic q,bool block);
335+
void __hxcpp_deque_add(Dynamic q, Dynamic inVal);
336+
void __hxcpp_deque_push(Dynamic q, Dynamic inVal);
337+
Dynamic __hxcpp_deque_pop(Dynamic q, bool block);
339338

340339
Dynamic __hxcpp_tls_get(int inID);
341-
void __hxcpp_tls_set(int inID,Dynamic inVal);
340+
void __hxcpp_tls_set(int inID, Dynamic inVal);
341+
342+
#endif
342343

343344
bool _hx_atomic_exchange_if(::cpp::Pointer<cpp::AtomicInt> inPtr, int test, int newVal );
344345
int _hx_atomic_inc(::cpp::Pointer<cpp::AtomicInt> inPtr );

include/hx/thread/Thread.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ namespace hx
2525

2626
virtual String getName() = 0;
2727
virtual void setName(const String& name) = 0;
28-
29-
virtual String toString() override = 0;
30-
31-
virtual void __Mark(HX_MARK_PARAMS) override = 0;
32-
#ifdef HXCPP_VISIT_ALLOCS
33-
virtual void __Visit(HX_VISIT_PARAMS) override = 0;
34-
#endif
3528
};
3629
}
3730
}

src/hx/Profiler.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include <hxcpp.h>
22
#include <map>
33
#include <vector>
4-
#include <hx/Thread.h>
54
#include <hx/OS.h>
65
#include <mutex>
6+
#include <thread>
7+
#include <chrono>
78

89

910
#ifdef HX_WINRT
@@ -49,7 +50,8 @@ class Profiler
4950
gThreadRefCount += 1;
5051

5152
if (gThreadRefCount == 1) {
52-
HxCreateDetachedThread(ProfileMainLoop, 0);
53+
std::thread thread(ProfileMainLoop);
54+
thread.detach();
5355
}
5456
}
5557

@@ -214,18 +216,16 @@ class Profiler
214216
int childrenPlusSelf;
215217
};
216218

217-
static THREAD_FUNC_TYPE ProfileMainLoop(void *)
219+
static void ProfileMainLoop()
218220
{
219221
int millis = 1;
220222

221223
while (gThreadRefCount > 0) {
222-
HxSleep(millis);
224+
std::this_thread::sleep_for(std::chrono::milliseconds(millis));
223225

224226
int count = gProfileClock + 1;
225227
gProfileClock = (count < 0) ? 0 : count;
226228
}
227-
228-
THREAD_FUNC_RET
229229
}
230230

231231
String mDumpFile;

src/hx/Telemetry.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#include <hx/Telemetry.h>
99
#include <hx/OS.h>
1010
#include <mutex>
11-
11+
#include <thread>
12+
#include <chrono>
1213

1314
namespace hx
1415
{
@@ -67,7 +68,9 @@ class Telemetry
6768

6869
gThreadRefCount += 1;
6970
if (gThreadRefCount == 1) {
70-
HxCreateDetachedThread(ProfileMainLoop, 0);
71+
std::thread thread(ProfileMainLoop);
72+
73+
thread.detach();
7174
}
7275
}
7376

@@ -284,26 +287,16 @@ class Telemetry
284287
int GetNameIdx(const char *fullName);
285288
int ComputeCallStackId();
286289

287-
static THREAD_FUNC_TYPE ProfileMainLoop(void *)
290+
static void ProfileMainLoop()
288291
{
289292
int millis = 1;
290293

291-
while (gThreadRefCount > 0) {
292-
#ifdef HX_WINDOWS
293-
Sleep(millis);
294-
#else
295-
struct timespec t;
296-
struct timespec tmp;
297-
t.tv_sec = 0;
298-
t.tv_nsec = millis * 1000000;
299-
nanosleep(&t, &tmp);
300-
#endif
294+
while (gThreadRefCount > 0) {
295+
std::this_thread::sleep_for(std::chrono::milliseconds(millis));
301296

302297
int count = gProfileClock + 1;
303298
gProfileClock = (count < 0) ? 0 : count;
304299
}
305-
306-
THREAD_FUNC_RET
307300
}
308301

309302
StackContext *stack;

src/hx/Thread.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <hxcpp.h>
22

3+
#if (HXCPP_API_LEVEL<500)
4+
35
#include <hx/Thread.h>
46
#include <time.h>
57
#include <hx/thread/ConditionVariable.hpp>
@@ -154,11 +156,7 @@ Dynamic __hxcpp_deque_pop(Dynamic q,bool block)
154156

155157
// --- Thread ----------------------------------------------------------
156158

157-
#if (HXCPP_API_LEVEL>=500)
158-
Dynamic __hxcpp_thread_create(hx::Callable<void()> inStart)
159-
#else
160159
Dynamic __hxcpp_thread_create(Dynamic inStart)
161-
#endif
162160
{
163161
return hx::thread::Thread_obj::create(inStart);
164162
}
@@ -404,6 +402,8 @@ int __hxcpp_GetCurrentThreadNumber()
404402
return hx::thread::Thread_obj::id();
405403
}
406404

405+
#endif
406+
407407
// --- Atomic ---
408408

409409
bool _hx_atomic_exchange_if(::cpp::Pointer<cpp::AtomicInt> inPtr, int test, int newVal )
@@ -419,6 +419,4 @@ int _hx_atomic_inc(::cpp::Pointer<cpp::AtomicInt> inPtr )
419419
int _hx_atomic_dec(::cpp::Pointer<cpp::AtomicInt> inPtr )
420420
{
421421
return _hx_atomic_sub(inPtr, 1);
422-
}
423-
424-
422+
}

src/hx/cppia/GlobalBuiltin.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include <hxcpp.h>
22
#include "Cppia.h"
33

4+
#if (HXCPP_API_LEVEL>=500)
5+
#include <hx/thread/Thread.hpp>
6+
#endif
7+
48
namespace hx
59
{
610

@@ -382,17 +386,11 @@ CppiaExpr *createGlobalBuiltin(CppiaExpr *src, String function, Expressions &ioE
382386
{
383387
if (ioExpressions.size()==1)
384388
#if (HXCPP_API_LEVEL>=500)
385-
return new ObjectBuiltin1<Callable<void(void)>, Dynamic, __hxcpp_thread_create>(src, ioExpressions);
389+
return new ObjectBuiltin1<hx::Callable<void(void)>, hx::thread::Thread, hx::thread::Thread_obj::create>(src, ioExpressions);
386390
#else
387-
return new ObjectBuiltin1<Dynamic, Dynamic, __hxcpp_thread_create>(src, ioExpressions);
391+
return new ObjectBuiltin1<Dynamic, Dynamic, __hxcpp_thread_create>(src, ioExpressions);
388392
#endif
389393
}
390-
if (function==HX_CSTRING("__hxcpp_thread_send") )
391-
{
392-
if (ioExpressions.size()==2)
393-
return new VoidBuiltin2<Dynamic,Dynamic,__hxcpp_thread_send>(src,ioExpressions);
394-
}
395-
396394

397395
printf("Unknown function : %s(%d)\n", function.out_str(), (int)ioExpressions.size() );
398396
throw (HX_CSTRING("Unknown global:") + function).utf8_str();

src/hx/gc/Immix.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "GcRegCapture.h"
99
#include <hx/Unordered.h>
1010
#include <mutex>
11+
#include <thread>
1112
#include <condition_variable>
1213

1314
#ifdef EMSCRIPTEN
@@ -4516,10 +4517,9 @@ class GlobalAllocator
45164517
}
45174518
}
45184519

4519-
static THREAD_FUNC_TYPE SThreadLoop( void *inInfo )
4520+
static void SThreadLoop( void *inInfo )
45204521
{
45214522
sGlobalAlloc->ThreadLoop((int)(size_t)inInfo);
4522-
THREAD_FUNC_RET;
45234523
}
45244524

45254525
void CreateWorker(int inId)
@@ -4537,7 +4537,9 @@ class GlobalAllocator
45374537

45384538
sThreadSleeping[inId] = false;
45394539

4540-
HxCreateDetachedThread(SThreadLoop, info);
4540+
std::thread thread(SThreadLoop, info);
4541+
4542+
thread.detach();
45414543
#endif
45424544
}
45434545

test/native/Native.hx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class Native
1818
new tests.TestNativeEnum(),
1919

2020
#if (haxe_ver>=5)
21+
new tests.TestScratch(),
22+
2123
new tests.marshalling.classes.TestLocalValueType(),
2224
new tests.marshalling.classes.TestClassValueType(),
2325
new tests.marshalling.classes.TestInterfaceValueType(),

test/native/tests/TestScratch.hx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package tests;
2+
3+
import cpp.Scratch;
4+
import utest.Test;
5+
import utest.Assert;
6+
7+
using haxe.Int64;
8+
9+
class TestScratch extends Test {
10+
function test_zeroed_small_alloc() {
11+
final size = 16;
12+
final alloc = Scratch.alloc(size);
13+
14+
Assert.equals(size, alloc.view.length.toInt());
15+
16+
for (i in 0...size.toInt()) {
17+
Assert.equals(0, alloc.view[i]);
18+
}
19+
}
20+
21+
function test_zeroed_large_alloc() {
22+
final size = 100_000;
23+
final alloc = Scratch.alloc(size);
24+
25+
Assert.equals(size, alloc.view.length.toInt());
26+
27+
for (i in 0...size.toInt()) {
28+
Assert.equals(0, alloc.view[i]);
29+
}
30+
}
31+
32+
function test_contents_wiped() {
33+
{
34+
final size = 16;
35+
final alloc = Scratch.alloc(size);
36+
37+
Assert.equals(size, alloc.view.length.toInt());
38+
39+
alloc.view.fill(7);
40+
}
41+
42+
{
43+
final size = 16;
44+
final alloc = Scratch.alloc(size);
45+
46+
Assert.equals(size, alloc.view.length.toInt());
47+
48+
for (i in 0...size.toInt()) {
49+
Assert.equals(0, alloc.view[i]);
50+
}
51+
}
52+
}
53+
}

toolchain/haxe-target-threads.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<xml>
2+
<depend name="${HXCPP}/src/hx/thread/ThreadImpl.hpp"/>
3+
4+
<file name="src/hx/thread/RecursiveMutex.cpp"/>
5+
<file name="src/hx/thread/ConditionVariable.cpp"/>
6+
<file name="src/hx/thread/ThreadImpl.cpp"/>
7+
<file name="src/hx/thread/ThreadLocal.cpp"/>
8+
<file name="src/hx/thread/Scratch.cpp"/>
9+
10+
<section if="windows">
11+
<file name="src/hx/thread/CountingSemaphore.win32.cpp"/>
12+
<file name="src/hx/thread/ThreadImpl.win32.cpp"/>
13+
</section>
14+
15+
<section if="apple">
16+
<file name="src/hx/thread/CountingSemaphore.apple.cpp"/>
17+
<file name="src/hx/thread/ThreadImpl.noop.cpp"/>
18+
</section>
19+
20+
<section if="linux||android">
21+
<file name="src/hx/thread/CountingSemaphore.posix.cpp"/>
22+
<file name="src/hx/thread/ThreadImpl.noop.cpp"/>
23+
</section>
24+
25+
<section unless="windows||apple||linux||android">
26+
<file name="src/hx/thread/CountingSemaphore.unsupported.cpp"/>
27+
<file name="src/hx/thread/ThreadImpl.noop.cpp"/>
28+
</section>
29+
30+
</xml>

0 commit comments

Comments
 (0)