@@ -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
125127hx::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
133147Dynamic hx::thread::ThreadImpl_obj::getSlot (const int id)
134148{
0 commit comments