Skip to content

Commit 3c5ae67

Browse files
author
Aidan Lee
committed
Reduce manual field assignment with new ctors
1 parent edcdd16 commit 3c5ae67

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

src/hx/thread/ThreadImpl.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ namespace
4343
//
4444
// I'm assuming the array of two elements is some trick to ensure it's on the stack.
4545
// Maybe we could use OS specific functions to get the stack range?
46-
//
47-
tls.set(new hx::Object* { thread.GetPtr() });
46+
47+
auto root = new hx::Object* { thread.GetPtr() };
48+
49+
hx::GCAddRoot(root);
50+
51+
tls.set(root);
4852

4953
auto info = std::array<hx::thread::ThreadImpl_obj*, 2>{ thread.GetPtr(), nullptr };
5054

@@ -66,10 +70,8 @@ hx::thread::Thread hx::thread::Thread_obj::create(CreateFunction job)
6670
#else
6771

6872
auto semaphore = new hx::thread::CountingSemaphore_obj(0);
69-
auto obj = new ThreadImpl_obj(nextThreadnumber++);
70-
auto native = new ThreadImpl_obj::Native(new std::thread(run, obj, job, semaphore));
71-
72-
obj->native = native;
73+
auto obj = new ThreadImpl_obj(nextThreadnumber++, job, semaphore);
74+
//auto native = new ThreadImpl_obj::Native(new std::thread(run, obj, job, semaphore));
7375

7476
hx::GCSetFinalizer(obj, ThreadImpl_obj::finalise);
7577
hx::GCPrepareMultiThreaded();
@@ -96,10 +98,10 @@ hx::thread::Thread hx::thread::Thread_obj::current()
9698
// The C++ std has no way of getting the native handle of the current thread, so we need to use OS specific
9799
// apis to get that so we can get and set thread names.
98100
info = new ThreadImpl_obj(nextThreadnumber++);
99-
info->native = new ThreadImpl_obj::Native();
100101

101102
auto root = new hx::Object* { info };
102103

104+
hx::GCSetFinalizer(info, ThreadImpl_obj::finalise);
103105
hx::GCAddRoot(root);
104106

105107
tls.set(root);
@@ -124,11 +126,23 @@ int hx::thread::Thread_obj::id()
124126

125127
hx::thread::ThreadImpl_obj::ThreadImpl_obj(const int _id)
126128
: id(_id)
127-
, native()
129+
, native(new Native())
128130
, scratch(std::numeric_limits<uint16_t>::max(), std::numeric_limits<uint16_t>::max())
129131
, slots(0, 0) { }
130132

131-
hx::thread::ThreadImpl_obj::Native::Native(std::thread* _thread) : thread(_thread), handle(thread->native_handle()) {}
133+
hx::thread::ThreadImpl_obj::ThreadImpl_obj(const int _id, Thread_obj::CreateFunction _job, CountingSemaphore _semaphore)
134+
: id(_id)
135+
, native(new Native(_job, this, _semaphore))
136+
, scratch(std::numeric_limits<uint16_t>::max(), std::numeric_limits<uint16_t>::max())
137+
, slots(0, 0)
138+
{
139+
}
140+
141+
hx::thread::ThreadImpl_obj::Native::Native(Thread_obj::CreateFunction _job, ThreadImpl _thread, CountingSemaphore _semaphore)
142+
: thread(new std::thread(run, _thread, _job, _semaphore))
143+
, handle(thread->native_handle())
144+
{
145+
}
132146

133147
Dynamic hx::thread::ThreadImpl_obj::getSlot(const int id)
134148
{

src/hx/thread/ThreadImpl.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ namespace hx
2929
std::unique_ptr<std::thread> thread;
3030
std::thread::native_handle_type handle;
3131

32-
Native(std::thread* _thread);
3332
Native();
33+
Native(Thread_obj::CreateFunction _job, ThreadImpl _thread, CountingSemaphore _semaphore);
3434
};
3535

3636
Native* native;
3737
const int id;
3838

3939
ThreadImpl_obj(const int _id);
40+
ThreadImpl_obj(const int _id, Thread_obj::CreateFunction _run, CountingSemaphore _semaphore);
4041

4142
String getName() override;
4243
void setName(const String& name) override;

0 commit comments

Comments
 (0)