Skip to content

_Py_ThreadId() fails when compiled using GCC on Windows #124609

Closed
@tonyroberts

Description

@tonyroberts

Bug report

Bug description:

It should be possible to build a C extension module using GCC on Windows, but when defining Py_GIL_DISABLED any call to _Py_ThreadId fails.

The if/defs in _Py_ThreadId look for _MSC_VER to be defined to determine whether it's a Windows build or not, but that specifies the VS version and not whether the build is for Windows or not.

Instead it would be better to use _WIN32 (see https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170)

Something along these lines...

#if defined(_WIN32)
#include <intrin.h>
#endif

static inline uintptr_t
_Py_ThreadId(void)
{
    uintptr_t tid;
#if defined(_WIN32) && defined(_M_X64)
    tid = __readgsqword(48);
#elif defined(_WIN32) && defined(_M_IX86)
    tid = __readfsdword(24);
#elif defined(_WIN32) && defined(_M_ARM64)
    tid = __getReg(18);
...

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions