RichieSams/FiberTaskingLib

GetCurrentThread() uses constant pseudo handle.

Closed this issue · 2 comments

Claim: Main thread handle (on Windows) will always point to the current thread, instead of the main one.

Please note that ::GetCurrentThread() returns constant pseudo handle (value: DWORD - 1):
https://github.com/RichieSams/FiberTaskingLib/blob/master/include/ftl/thread_abstraction.h#L133

This is wrongly stored as a handle for the main thread:
https://github.com/RichieSams/FiberTaskingLib/blob/master/source/task_scheduler.cpp#L275

::GetCurrentThread() details:
https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-getcurrentthread

To correct this you have to create the handle with ::DuplicateHandle, and then close it with ::CloseHandle
Something like this:
::DuplicateHandle(::GetCurrentProcess(), ::GetCurrentThread(), ::GetCurrentProcess(), &handle, 0, FALSE, DUPLICATE_SAME_ACCESS);

Looks like a good find! Apparently, the main thread handle is never actually used by ftl, so the bug never manifested.

Indeed, it's not used.
Perhaps setting it to INVALID_HANDLE_VALUE would be better here as a dummy value, till ftl will need it.

INVALID_HANDLE_VALUE is defined as (from handleapi.h):
#define INVALID_HANDLE_VALUE ((HANDLE)(LONG_PTR)-1)