Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH v2] w32pthreads: add support for setting thread name
@ 2025-03-04 15:04 Kacper Michajłow
  2025-03-30 22:04 ` Jan Ekström
  0 siblings, 1 reply; 3+ messages in thread
From: Kacper Michajłow @ 2025-03-04 15:04 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Kacper Michajłow

Signed-off-by: Kacper Michajłow <kasper93@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..8d5b4729fa 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 AVERROR(ENOSYS);
+    pSetThreadDescription = (SetThreadDescriptionFn)
+        GetProcAddress(kernel32, "SetThreadDescription");
+    if (!pSetThreadDescription)
+        return AVERROR(ENOSYS);
+#else
+    WINBASEAPI HRESULT WINAPI
+    SetThreadDescription(HANDLE hThread, PCWSTR lpThreadDescription);
+    pSetThreadDescription = &SetThreadDescription;
+#endif
+
+    if (utf8towchar(name, &wname) < 0)
+        return AVERROR(ENOMEM);
+
+    hr = pSetThreadDescription(GetCurrentThread(), wname);
+    av_free(wname);
+    return SUCCEEDED(hr) ? 0 : AVERROR(EINVAL);
+}
+
 #endif /* COMPAT_W32PTHREADS_H */
diff --git a/libavutil/thread.h b/libavutil/thread.h
index 2c00c7cc35..184e2d8c5f 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 = win32_thread_setname(name);
 #else
     ret = AVERROR(ENOSYS);
 #endif
-- 
2.45.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [FFmpeg-devel] [PATCH v2] w32pthreads: add support for setting thread name
  2025-03-04 15:04 [FFmpeg-devel] [PATCH v2] w32pthreads: add support for setting thread name Kacper Michajłow
@ 2025-03-30 22:04 ` Jan Ekström
  2025-03-31  0:50   ` softworkz .
  0 siblings, 1 reply; 3+ messages in thread
From: Jan Ekström @ 2025-03-30 22:04 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

On Tue, Mar 4, 2025 at 5:14 PM Kacper Michajłow <kasper93@gmail.com> wrote:
>
> Signed-off-by: Kacper Michajłow <kasper93@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..8d5b4729fa 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 AVERROR(ENOSYS);
> +    pSetThreadDescription = (SetThreadDescriptionFn)
> +        GetProcAddress(kernel32, "SetThreadDescription");
> +    if (!pSetThreadDescription)
> +        return AVERROR(ENOSYS);
> +#else
> +    WINBASEAPI HRESULT WINAPI
> +    SetThreadDescription(HANDLE hThread, PCWSTR lpThreadDescription);
> +    pSetThreadDescription = &SetThreadDescription;
> +#endif
> +
> +    if (utf8towchar(name, &wname) < 0)
> +        return AVERROR(ENOMEM);
> +
> +    hr = pSetThreadDescription(GetCurrentThread(), wname);
> +    av_free(wname);
> +    return SUCCEEDED(hr) ? 0 : AVERROR(EINVAL);
> +}
> +

I can only comment on the non-UWP side of things, but in general the
code seems fine. I guess this function definition has not been in
mingw-w64 etc for long enough to hope it would always be there and
thus we need to define it (similar to pf_DXGIGetDebugInterface)?

The only question mark that is left is whether this functionality is
actually in kernel32 or kernelbase. When I first saw
https://stackoverflow.com/questions/62243162/how-to-access-setthreaddescription-in-windows-2016-server-version-1607
I more or less thought of it as a possibly fluke or so, but then
apparently mingw-w64 went for kernelbase as well in winpthreads?
https://sourceforge.net/p/mingw-w64/mailman/message/58829419/

What it seems like is that kernel32 works for (at the very least) 20+
versions of win10+, and older stuff such as the still-supported server
2016 requires direct usage of kernelbase?

Jan
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [FFmpeg-devel] [PATCH v2] w32pthreads: add support for setting thread name
  2025-03-30 22:04 ` Jan Ekström
@ 2025-03-31  0:50   ` softworkz .
  0 siblings, 0 replies; 3+ messages in thread
From: softworkz . @ 2025-03-31  0:50 UTC (permalink / raw)
  To: FFmpeg development discussions and patches



> -----Original Message-----
> From: ffmpeg-devel <ffmpeg-devel-bounces@ffmpeg.org> On Behalf Of Jan
> Ekström
> Sent: Montag, 31. März 2025 00:05
> To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
> Subject: Re: [FFmpeg-devel] [PATCH v2] w32pthreads: add support for
> setting thread name
> 
> On Tue, Mar 4, 2025 at 5:14 PM Kacper Michajłow <kasper93@gmail.com>
> wrote:
> >
> > Signed-off-by: Kacper Michajłow <kasper93@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..8d5b4729fa 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 AVERROR(ENOSYS);
> > +    pSetThreadDescription = (SetThreadDescriptionFn)
> > +        GetProcAddress(kernel32, "SetThreadDescription");
> > +    if (!pSetThreadDescription)
> > +        return AVERROR(ENOSYS);
> > +#else
> > +    WINBASEAPI HRESULT WINAPI
> > +    SetThreadDescription(HANDLE hThread, PCWSTR lpThreadDescription);
> > +    pSetThreadDescription = &SetThreadDescription;
> > +#endif
> > +
> > +    if (utf8towchar(name, &wname) < 0)
> > +        return AVERROR(ENOMEM);
> > +
> > +    hr = pSetThreadDescription(GetCurrentThread(), wname);
> > +    av_free(wname);
> > +    return SUCCEEDED(hr) ? 0 : AVERROR(EINVAL);
> > +}
> > +
> 
> I can only comment on the non-UWP side of things, but in general the
> code seems fine. I guess this function definition has not been in
> mingw-w64 etc for long enough to hope it would always be there and
> thus we need to define it (similar to pf_DXGIGetDebugInterface)?
> 
> The only question mark that is left is whether this functionality is
> actually in kernel32 or kernelbase. When I first saw
> https://stackoverflow.com/questions/62243162/how-to-access-
> setthreaddescription-in-windows-2016-server-version-1607
> I more or less thought of it as a possibly fluke or so, but then
> apparently mingw-w64 went for kernelbase as well in winpthreads?
> https://sourceforge.net/p/mingw-w64/mailman/message/58829419/
> 
> What it seems like is that kernel32 works for (at the very least) 20+
> versions of win10+, and older stuff such as the still-supported server
> 2016 requires direct usage of kernelbase?
> 
> Jan
> _______________________________________________

Hi Jan,

the actual implementation is (and has always been) in kernelbase.dll.

It was introduced there with Windows SDK 10.0.14393 (Windows OS 1607).
At some point between SDKs 14393 and 18362 (don't have the in-betweens installed), a forwarding export had been added to kernel32.dll (try: dumpbin /exports c:\windows\system32\kernel32.dll | findstr SetThreadDescription).

So, using kernelbase.dll will cover more Windows versions than with kernel32.dll.

sw



_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-03-31  0:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-04 15:04 [FFmpeg-devel] [PATCH v2] w32pthreads: add support for setting thread name Kacper Michajłow
2025-03-30 22:04 ` Jan Ekström
2025-03-31  0:50   ` softworkz .

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git