Repository: libsdl-org/SDL
File: src/video/windows/SDL_windowsrawinput.c (C)

Code before:
    RAWINPUTDEVICE devices[2];
    HWND window;
    UINT count = 0;

    window = CreateWindowEx(0, TEXT("Message"), NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
    if (!window) {
        return 0;
    }

    SDL_zeroa(devices);

    if (data->flags & ENABLE_RAW_MOUSE_INPUT) {
        devices[count].usUsagePage = USB_USAGEPAGE_GENERIC_DESKTOP;
        devices[count].usUsage = USB_USAGE_GENERIC_MOUSE;
        devices[count].dwFlags = 0;
        devices[count].hwndTarget = window;
        ++count;
    }

    if (data->flags & ENABLE_RAW_KEYBOARD_INPUT) {
        devices[count].usUsagePage = USB_USAGEPAGE_GENERIC_DESKTOP;
        devices[count].usUsage = USB_USAGE_GENERIC_KEYBOARD;
        devices[count].dwFlags = 0;
        if (SDL_GetHintBoolean(SDL_HINT_WINDOWS_RAW_KEYBOARD_NOHOTKEYS, false)) {
            devices[count].dwFlags |= RIDEV_NOHOTKEYS;
        }
        devices[count].hwndTarget = window;
        ++count;
    }

    if (!RegisterRawInputDevices(devices, count, sizeof(devices[0]))) {
        DestroyWindow(window);
        return 0;
    }

    // Make sure we get events as soon as possible
    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);

    // Tell the parent we're ready to go!
    SetEvent(data->ready_event);

    while (!data->done) {
        Uint64 idle_begin = SDL_GetTicksNS();
        DWORD result = MsgWaitForMultipleObjects(1, &data->done_event, FALSE, INFINITE, QS_RAWINPUT);
        Uint64 idle_end = SDL_GetTicksNS();
        if (result != (WAIT_OBJECT_0 + 1)) {
            break;
        }

        // Clear the queue status so MsgWaitForMultipleObjects() will wait again
        (void)GetQueueStatus(QS_RAWINPUT);

Review comment (suggestion):
I think this is tread-unsafe. Instead of querying the hint here, you should store it as `RawInputThreadData.flags` before starting the thread and simply read it here.

Code after:
    RawInputThreadData *data = (RawInputThreadData *)param;
    RAWINPUTDEVICE devices[2];
    HWND window;
    UINT count = 0;

    window = CreateWindowEx(0, TEXT("Message"), NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
    if (!window) {
        return 0;
    }

    SDL_zeroa(devices);

    if (data->flags & ENABLE_RAW_MOUSE_INPUT) {
        devices[count].usUsagePage = USB_USAGEPAGE_GENERIC_DESKTOP;
        devices[count].usUsage = USB_USAGE_GENERIC_MOUSE;
        devices[count].dwFlags = 0;
        devices[count].hwndTarget = window;
        ++count;
    }

    if (data->flags & ENABLE_RAW_KEYBOARD_INPUT) {
        devices[count].usUsagePage = USB_USAGEPAGE_GENERIC_DESKTOP;
        devices[count].usUsage = USB_USAGE_GENERIC_KEYBOARD;
        devices[count].dwFlags = 0;
        if (data->flags & RAW_KEYBOARD_FLAG_NOHOTKEYS) {
            devices[count].dwFlags |= RIDEV_NOHOTKEYS;
        }
        devices[count].hwndTarget = window;
        ++count;
    }

    if (!RegisterRawInputDevices(devices, count, sizeof(devices[0]))) {
        DestroyWindow(window);
        return 0;
    }

    // Make sure we get events as soon as possible
    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);

    // Tell the parent we're ready to go!
    SetEvent(data->ready_event);

    while (!data->done) {
        Uint64 idle_begin = SDL_GetTicksNS();
        DWORD result = MsgWaitForMultipleObjects(1, &data->done_event, FALSE, INFINITE, QS_RAWINPUT);
        Uint64 idle_end = SDL_GetTicksNS();
        if (result != (WAIT_OBJECT_0 + 1)) {
            break;
        }

        // Clear the queue status so MsgWaitForMultipleObjects() will wait again