Skip to content

Commit

Permalink
[Runtime]Fix incorrect use of ::GetCurrentThread to get thread hand…
Browse files Browse the repository at this point in the history
…le on Windows.
  • Loading branch information
JX-Master committed Sep 22, 2024
1 parent 85217b8 commit 8159ce2
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Modules/Luna/Runtime/Source/Platform/Windows/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ namespace Luna
SpinLock g_allocated_tls_mtx;
struct Thread
{
HANDLE m_handle;
thread_callback_func_t* m_func;
void* m_params;
};
HANDLE m_main_thread_handle = NULL;
DWORD m_current_thread_handle_tls = 0;
}
}
DWORD WINAPI WinThreadEntry(LPVOID cookie)
{
using namespace Luna;
using namespace Luna::OS;
Thread* ctx = (Thread*)cookie;
::TlsSetValue(m_current_thread_handle_tls, ctx->m_handle);
ctx->m_func(ctx->m_params);
// Clean up all tls.
g_allocated_tls_mtx.lock();
Expand All @@ -57,9 +61,17 @@ namespace Luna
void thread_init()
{
g_allocated_tls.construct();
m_current_thread_handle_tls = ::TlsAlloc();
luassert_always(m_current_thread_handle_tls != TLS_OUT_OF_INDEXES);
m_main_thread_handle = ::OpenThread(THREAD_ALL_ACCESS, FALSE, ::GetCurrentThreadId());
luassert_always(m_main_thread_handle);
::TlsSetValue(m_current_thread_handle_tls, m_main_thread_handle);
}
void thread_close()
{
::CloseHandle(m_main_thread_handle);
m_main_thread_handle = NULL;
::TlsFree(m_current_thread_handle_tls);
g_allocated_tls.destruct();
}
opaque_t new_thread(thread_callback_func_t* callback, void* params, const c8* name, usize stack_size)
Expand All @@ -76,6 +88,7 @@ namespace Luna
Luna::memdelete(t);
lupanic_msg_always("CreateThread failed.");
}
t->m_handle = h;
if (name)
{
wchar_t* buf = utf8_to_wchar_buffered(name);
Expand Down Expand Up @@ -138,7 +151,7 @@ namespace Luna
}
opaque_t get_current_thread_handle()
{
return opaque_t(::GetCurrentThread());
return opaque_t(::TlsGetValue(m_current_thread_handle_tls));
}
void sleep(u32 time_milliseconds)
{
Expand Down

0 comments on commit 8159ce2

Please sign in to comment.