Ignore:
Timestamp:
Dec 15, 2021, 11:53:57 PM (4 years ago)
Author:
bird
Message:

kash: shthread_set_name for naming the async CloseHandle thread.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kash/shthread.c

    r3477 r3505  
    108108}
    109109
     110
     111/**
     112 * Sets the name of the current thread if supported by the OS.
     113 */
     114void shthread_set_name(const char *name)
     115{
     116#if K_OS == K_OS_WINDOWS
     117    typedef BOOL (WINAPI * PFNSETTHREADDESCRIPTION)(HANDLE, WCHAR *);
     118    static KBOOL volatile                   s_initialized             = K_FALSE;
     119    static PFNSETTHREADDESCRIPTION volatile s_pfnSetThreadDescription = NULL;
     120    PFNSETTHREADDESCRIPTION                 pfnSetThreadDescription   = s_pfnSetThreadDescription;
     121    WCHAR                                   wszName[32];
     122    size_t                                  i;
     123
     124    /* Get the function pointer, return if not available. */
     125    if (pfnSetThreadDescription)
     126    { }
     127    else if (s_initialized)
     128        return;
     129    else
     130    {
     131        pfnSetThreadDescription = (PFNSETTHREADDESCRIPTION)GetProcAddress(GetModuleHandleW(L"KERNEL32.DLL"),
     132                                                                          "SetThreadDescription");
     133        s_pfnSetThreadDescription = pfnSetThreadDescription;
     134        s_initialized = K_TRUE;
     135        if (!pfnSetThreadDescription)
     136            return;
     137    }
     138
     139    /* Convert the name to UTF-16 and call the API. */
     140    i = strlen(name);
     141    kHlpAssertStmt(i < K_ELEMENTS(wszName), i = K_ELEMENTS(wszName));
     142    wszName[i] = '\0';
     143    while (i-- > 0)
     144        wszName[i] = name[i];
     145
     146    pfnSetThreadDescription(GetCurrentThread(), wszName);
     147#else
     148    K_NOREF(name);
     149#endif
     150}
     151
Note: See TracChangeset for help on using the changeset viewer.