Skip to content

Commit 4821418

Browse files
authored
[cpp] Typed Thread and TLS (#12885)
* Typed thread and tls * Add scratch class extern
1 parent a1e5612 commit 4821418

3 files changed

Lines changed: 38 additions & 19 deletions

File tree

std/cpp/Scratch.hx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cpp;
2+
3+
@:include("hx/thread/Scratch.hpp")
4+
@:semantics(value)
5+
@:cpp.ValueType({ namespace : [ "hx", "thread" ], flags : [ StackOnly ] })
6+
private extern class Scratch {
7+
final view : cpp.marshal.View<cpp.UInt8>;
8+
9+
static function alloc(bytes:Int):Scratch;
10+
}

std/cpp/_std/sys/thread/ThreadImpl.hx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,32 @@
2222

2323
package sys.thread;
2424

25-
@:callable
26-
@:coreType
27-
private abstract NativeThreadHandle {}
28-
29-
private typedef ThreadHandle = NativeThreadHandle;
25+
@:include("hx/thread/Thread.hpp")
26+
@:cpp.ManagedType({ namespace : [ "hx", "thread" ], type : "Thread", flags : [ StandardNaming ] })
27+
private extern class NativeThread {
28+
static function create(job:()->Void):NativeThread;
29+
static function current():NativeThread;
30+
31+
function getName():String;
32+
function setName(name:String):Void;
33+
}
3034

31-
abstract ThreadImpl(ThreadHandle) {
35+
abstract ThreadImpl(NativeThread) {
3236

3337
public static #if !scriptable inline #end function current():ThreadImpl {
34-
return untyped __global__.__hxcpp_thread_current();
38+
return cast NativeThread.current();
3539
}
3640

3741
public static #if !scriptable inline #end function create(job:Void->Void):ThreadImpl {
38-
return untyped __global__.__hxcpp_thread_create(job);
42+
return cast NativeThread.create(job);
3943
}
4044

4145
public static function setName( t : ThreadImpl, name : String ) {
46+
(cast t : NativeThread).setName(name);
4247
}
4348

4449
public static function getName( t : ThreadImpl ) {
45-
return null;
50+
return (cast t : NativeThread).getName();
4651
}
4752

4853
}

std/cpp/_std/sys/thread/Tls.hx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,32 @@
2222

2323
package sys.thread;
2424

25+
@:include("hx/thread/ThreadLocal.hpp")
26+
@:cpp.ManagedType({ namespace : [ "hx", "thread" ], flags : [ StandardNaming ] })
27+
private extern class ThreadLocal {
28+
function new():Void;
29+
30+
function get():Dynamic;
31+
function set(obj:Dynamic):Void;
32+
}
33+
2534
@:coreApi
2635
class Tls<T> {
27-
static var sFreeSlot:Int;
28-
29-
var mTLSID:Int;
36+
final tls:ThreadLocal;
3037

3138
public var value(get, set):Null<T>;
3239

3340
public function new() {
34-
mTLSID = sFreeSlot++;
41+
tls = new ThreadLocal();
3542
}
3643

3744
function get_value():Null<T> {
38-
return untyped __global__.__hxcpp_tls_get(mTLSID);
45+
return tls.get();
3946
}
4047

4148
function set_value(v:Null<T>):Null<T> {
42-
untyped __global__.__hxcpp_tls_set(mTLSID, v);
43-
return v;
44-
}
49+
tls.set(v);
4550

46-
static function __init__ ():Void {
47-
sFreeSlot = 0;
51+
return v;
4852
}
4953
}

0 commit comments

Comments
 (0)