[FFmpeg-devel] [PATCH] w32pthreads: add support for setting thread name

James Almer jamrial at gmail.com
Tue Mar 4 16:50:12 EET 2025


On 3/4/2025 9:52 AM, Kacper Michajłow wrote:
> Signed-off-by: Kacper Michajłow <kasper93 at gmail.com>
> ---
>   compat/w32pthreads.h | 30 ++++++++++++++++++++++++++++++
>   libavutil/thread.h   |  2 ++
>   2 files changed, 32 insertions(+)
> 
> diff --git a/compat/w32pthreads.h b/compat/w32pthreads.h
> index fd6428e29f..83b4819205 100644
> --- a/compat/w32pthreads.h
> +++ b/compat/w32pthreads.h
> @@ -44,6 +44,7 @@
>   #include "libavutil/internal.h"
>   #include "libavutil/mem.h"
>   #include "libavutil/time.h"
> +#include "libavutil/wchar_filename.h"
>   
>   typedef struct pthread_t {
>       void *handle;
> @@ -209,4 +210,33 @@ static inline int pthread_setcancelstate(int state, int *oldstate)
>       return 0;
>   }
>   
> +static inline int win32_thread_setname(const char *name)
> +{
> +    typedef HRESULT (WINAPI *SetThreadDescriptionFn)(HANDLE, PCWSTR);
> +    SetThreadDescriptionFn pSetThreadDescription;
> +    HRESULT hr;
> +    wchar_t *wname;
> +
> +#if !HAVE_UWP
> +    HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll");
> +    if (!kernel32)
> +        return ENOSYS;
> +    pSetThreadDescription = (SetThreadDescriptionFn)
> +        GetProcAddress(kernel32, "SetThreadDescription");
> +    if (!pSetThreadDescription)
> +        return ENOSYS;
> +#else
> +    WINBASEAPI HRESULT WINAPI
> +    SetThreadDescription(HANDLE hThread, PCWSTR lpThreadDescription);
> +    pSetThreadDescription = &SetThreadDescription;
> +#endif
> +
> +    if (utf8towchar(name, &wname) < 0)
> +        return ENOMEM;
> +
> +    hr = pSetThreadDescription(GetCurrentThread(), wname);
> +    av_free(wname);
> +    return SUCCEEDED(hr) ? 0 : EINVAL;
> +}
> +
>   #endif /* COMPAT_W32PTHREADS_H */
> diff --git a/libavutil/thread.h b/libavutil/thread.h
> index 2c00c7cc35..d1a36a6772 100644
> --- a/libavutil/thread.h
> +++ b/libavutil/thread.h
> @@ -229,6 +229,8 @@ static inline int ff_thread_setname(const char *name)
>   #endif
>   #elif HAVE_PTHREAD_SET_NAME_NP
>       pthread_set_name_np(pthread_self(), name);
> +#elif HAVE_W32THREADS
> +    ret = AVERROR(win32_thread_setname(name));

AVERROR(0) is probably not a good idea, so negate the codes in 
win32_thread_setname() directly.

>   #else
>       ret = AVERROR(ENOSYS);
>   #endif

-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature.asc
Type: application/pgp-signature
Size: 495 bytes
Desc: OpenPGP digital signature
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20250304/019c391f/attachment.sig>


More information about the ffmpeg-devel mailing list