Skip to content

Commit aef979a

Browse files
Aidan63Aidan Lee
andauthored
Typed thread and tls api (#1336)
* Initial typed thread and thread local storage * Move scratch to it's own header and source files * Add emscripten throw back * have old c-style functions use new implementation * Add haxe 4 dynamic fallback * use the thread id c function less * Reduce manual field assignment with new ctors * Bodge it --------- Co-authored-by: Aidan Lee <aidan.lee@evcam.com>
1 parent 96cd8ff commit aef979a

14 files changed

Lines changed: 583 additions & 254 deletions

include/hx/thread/Scratch.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#ifndef HXCPP_H
4+
#include <hxcpp.h>
5+
#endif
6+
7+
HX_DECLARE_CLASS2(hx, thread, Thread)
8+
9+
namespace hx
10+
{
11+
namespace thread
12+
{
13+
class Scratch final
14+
{
15+
using ReleaseFunc = void(*)(cpp::marshal::View<uint8_t>);
16+
17+
int* count;
18+
ReleaseFunc release;
19+
20+
public:
21+
static Scratch alloc(int bytes);
22+
23+
cpp::marshal::View<uint8_t> view;
24+
25+
Scratch(int* _count, cpp::marshal::View<uint8_t> _view, ReleaseFunc _release);
26+
Scratch(const Scratch& other);
27+
28+
~Scratch();
29+
30+
Scratch& operator=(const Scratch& other);
31+
};
32+
}
33+
}

include/hx/thread/Thread.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
3+
#ifndef HXCPP_H
4+
#include <hxcpp.h>
5+
#endif
6+
7+
HX_DECLARE_CLASS2(hx, thread, Thread)
8+
9+
namespace hx
10+
{
11+
namespace thread
12+
{
13+
struct Thread_obj : public hx::Object
14+
{
15+
using CreateFunction =
16+
#if (HXCPP_API_LEVEL >= 500)
17+
Callable<void(void)>;
18+
#else
19+
Dynamic;
20+
#endif
21+
22+
static Thread create(CreateFunction);
23+
static Thread current();
24+
static int id();
25+
26+
virtual String getName() = 0;
27+
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
35+
};
36+
}
37+
}

include/hx/thread/ThreadLocal.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#ifndef HXCPP_H
4+
#include <hxcpp.h>
5+
#endif
6+
7+
HX_DECLARE_CLASS2(hx, thread, ThreadLocal)
8+
9+
namespace hx
10+
{
11+
namespace thread
12+
{
13+
class ThreadLocal_obj : public hx::Object
14+
{
15+
struct Impl;
16+
17+
Impl* impl;
18+
19+
public:
20+
ThreadLocal_obj();
21+
22+
Dynamic get();
23+
void set(Dynamic obj);
24+
};
25+
}
26+
}

src/hx/Debug.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <hx/Thread.h>
88
#include <hx/Telemetry.h>
99
#include <hx/Unordered.h>
10+
#include <hx/thread/Thread.hpp>
1011
#include <hx/OS.h>
1112
#include <mutex>
1213

@@ -244,7 +245,7 @@ StackContext::~StackContext()
244245
void StackContext::onThreadAttach()
245246
{
246247
#ifdef HXCPP_STACK_IDS
247-
mThreadId = __hxcpp_GetCurrentThreadNumber();
248+
mThreadId = hx::thread::Thread_obj::id();
248249

249250
{
250251
std::lock_guard<std::mutex> guard(sStackMapMutex);

src/hx/Debugger.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <hx/Thread.h>
88
#include <hx/OS.h>
99
#include <hx/QuickVec.h>
10+
#include <hx/thread/Thread.hpp>
1011
#include <mutex>
1112

1213
// Newer versions of haxe compiler will set these too (or might be null for haxe 3.0)
@@ -1260,7 +1261,7 @@ void __hxcpp_dbg_setEventNotificationHandler(Dynamic handler)
12601261
if (hx::g_eventNotificationHandler != null()) {
12611262
GCRemoveRoot(&(hx::g_eventNotificationHandler.mPtr));
12621263
}
1263-
hx::g_debugThreadNumber = __hxcpp_GetCurrentThreadNumber();
1264+
hx::g_debugThreadNumber = hx::thread::Thread_obj::id();
12641265
hx::g_eventNotificationHandler = handler;
12651266
GCAddRoot(&(hx::g_eventNotificationHandler.mPtr));
12661267
}
@@ -1275,7 +1276,7 @@ void __hxcpp_dbg_enableCurrentThreadDebugging(bool enable)
12751276

12761277
int __hxcpp_dbg_getCurrentThreadNumber()
12771278
{
1278-
return __hxcpp_GetCurrentThreadNumber();
1279+
return hx::thread::Thread_obj::id();
12791280
}
12801281

12811282

0 commit comments

Comments
 (0)