Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Runtime]Fix incorrect use of ::GetCurrentThread to get thread handle on Windows. #79

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading