VirtualBox

Changeset 86170 in vbox for trunk/src/VBox/Runtime/r3


Ignore:
Timestamp:
Sep 18, 2020 1:38:40 PM (4 years ago)
Author:
vboxsync
Message:

IPRT/win/thread: Use new SetThreadDescription to set thread names.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/win/thread-win.cpp

    r85121 r86170  
    5050
    5151/*********************************************************************************************************************************
    52 *   Defined Constants And Macros                                                                                                 *
     52*   Structures and Typedefs                                                                                                      *
     53*********************************************************************************************************************************/
     54/** SetThreadDescription */
     55typedef HRESULT (WINAPI *PFNSETTHREADDESCRIPTION)(HANDLE hThread, WCHAR *pwszName); /* Since W10 1607 */
     56
     57
     58/*********************************************************************************************************************************
     59*   Global Variables                                                                                                             *
    5360*********************************************************************************************************************************/
    5461/** The TLS index allocated for storing the RTTHREADINT pointer. */
    5562static DWORD g_dwSelfTLS = TLS_OUT_OF_INDEXES;
     63/** Pointer to SetThreadDescription (KERNEL32.DLL) if available. */
     64static PFNSETTHREADDESCRIPTION g_pfnSetThreadDescription = NULL;
    5665
    5766
     
    6170static unsigned __stdcall rtThreadNativeMain(void *pvArgs) RT_NOTHROW_PROTO;
    6271static void rtThreadWinTellDebuggerThreadName(uint32_t idThread, const char *pszName);
     72DECLINLINE(void) rtThreadWinSetThreadName(PRTTHREADINT pThread, DWORD idThread);
    6373
    6474
     
    6878    if (g_dwSelfTLS == TLS_OUT_OF_INDEXES)
    6979        return VERR_NO_TLS_FOR_SELF;
     80
     81    g_pfnSetThreadDescription = (PFNSETTHREADDESCRIPTION)GetProcAddress(g_hModKernel32, "SetThreadDescription");
    7082    return VINF_SUCCESS;
    7183}
     
    110122    if (!TlsSetValue(g_dwSelfTLS, pThread))
    111123        return VERR_FAILED_TO_SET_SELF_TLS;
    112     if (IsDebuggerPresent())
    113         rtThreadWinTellDebuggerThreadName(GetCurrentThreadId(), pThread->szName);
     124    rtThreadWinSetThreadName(pThread, GetCurrentThreadId());
    114125    return VINF_SUCCESS;
    115126}
     
    149160    {
    150161
     162    }
     163}
     164
     165
     166/**
     167 * Sets the thread name as best as we can.
     168 */
     169DECLINLINE(void) rtThreadWinSetThreadName(PRTTHREADINT pThread, DWORD idThread)
     170{
     171    if (IsDebuggerPresent())
     172        rtThreadWinTellDebuggerThreadName(idThread, &pThread->szName[0]);
     173
     174    /* The SetThreadDescription API introduced in windows 10 1607 / server 2016
     175       allows setting the thread name while the debugger isn't attached.  Works
     176       with WinDbgX, VisualStudio 2017 v15.6+, and presumeably some recent windbg
     177       version. */
     178    if (g_pfnSetThreadDescription)
     179    {
     180        /* The name should be ASCII, so we just need to expand 'char' to 'WCHAR'. */
     181        WCHAR wszName[RTTHREAD_NAME_LEN];
     182        for (size_t i = 0; i < RTTHREAD_NAME_LEN; i++)
     183            wszName[i] = pThread->szName[i];
     184
     185        HRESULT hrc = g_pfnSetThreadDescription(GetCurrentThread(), wszName);
     186        Assert(SUCCEEDED(hrc)); RT_NOREF(hrc);
    151187    }
    152188}
     
    250286    if (!TlsSetValue(g_dwSelfTLS, pThread))
    251287        AssertReleaseMsgFailed(("failed to set self TLS. lasterr=%d thread '%s'\n", GetLastError(), pThread->szName));
    252     if (IsDebuggerPresent())
    253         rtThreadWinTellDebuggerThreadName(dwThreadId, &pThread->szName[0]);
     288    rtThreadWinSetThreadName(pThread, dwThreadId);
    254289
    255290    int rc = rtThreadMain(pThread, dwThreadId, &pThread->szName[0]);
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette