From: Timo Rothenpieler via ffmpeg-devel <ffmpeg-devel@ffmpeg.org> To: ffmpeg-devel@ffmpeg.org Cc: Timo Rothenpieler <code@ffmpeg.org> Subject: [FFmpeg-devel] [PATCH] avfilter/vsrc_gfxcapture: switch to std::thread (PR #20520) Date: Sun, 14 Sep 2025 20:25:59 -0000 Message-ID: <175788155997.25.17669914793093092952@463a07221176> (raw) PR #20520 opened by Timo Rothenpieler (BtbN) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20520 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20520.patch Combining C++ with our w32pthreads does not work out. The C++ standard lib headers might include pthread.h, which will then define clashing types with our w32pthread header. To solve this, switch to using C++ native std::thread. >From faba1501d5dd1234f760f6501c25ab64866cfe48 Mon Sep 17 00:00:00 2001 From: Timo Rothenpieler <timo@rothenpieler.org> Date: Sun, 14 Sep 2025 21:38:02 +0200 Subject: [PATCH] avfilter/vsrc_gfxcapture: switch to std::thread Combining C++ with our w32pthreads does not work out. The C++ standard lib headers might include pthread.h, which will then define clashing types with our w32pthread header. To solve this, switch to using C++ native std::thread. --- libavfilter/vsrc_gfxcapture_winrt.cpp | 53 +++++++++++++++------------ 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/libavfilter/vsrc_gfxcapture_winrt.cpp b/libavfilter/vsrc_gfxcapture_winrt.cpp index 49181f1c0e..6477e56918 100644 --- a/libavfilter/vsrc_gfxcapture_winrt.cpp +++ b/libavfilter/vsrc_gfxcapture_winrt.cpp @@ -37,7 +37,6 @@ extern "C" { #include "libavutil/mem.h" #include "libavutil/opt.h" #include "libavutil/time.h" -#include "libavutil/thread.h" #include "libavutil/pixdesc.h" #include "libavutil/hwcontext.h" #include "libavutil/hwcontext_d3d11va.h" @@ -51,10 +50,12 @@ extern "C" { #include <atomic> #include <cinttypes> #include <condition_variable> +#include <cwchar> #include <memory> #include <mutex> #include <regex> #include <string> +#include <thread> #include <type_traits> using namespace ABI::Windows::System; @@ -99,6 +100,9 @@ typedef struct GfxCaptureFunctions { hmodule_ptr_t user32_handle; DPI_AWARENESS_CONTEXT (WINAPI *SetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext); + hmodule_ptr_t kernel32_handle; + HRESULT (WINAPI *SetThreadDescription)(HANDLE hThread, PCWSTR lpThreadDescription); + hmodule_ptr_t d3dcompiler_handle; HRESULT (WINAPI *D3DCompile)(LPCVOID pSrcData, SIZE_T SrcDataSize, LPCSTR pSourceName, const D3D10_SHADER_MACRO *pDefines, ID3DInclude *pInclude, LPCSTR pEntrypoint, LPCSTR pTarget, UINT Flags1, UINT Flags2, ID3DBlob **ppCode, ID3DBlob **ppErrorMsgs); @@ -139,13 +143,13 @@ struct GfxCaptureContextCpp { std::unique_ptr<GfxCaptureContextWgc> wgc; std::unique_ptr<GfxCaptureContextD3D> d3d; - pthread_t wgc_thread; - bool wgc_thread_created { false }; + std::thread wgc_thread; DWORD wgc_thread_id { 0 }; std::mutex wgc_thread_init_mutex; std::condition_variable wgc_thread_init_cond; volatile int wgc_thread_init_res { INT_MAX }; std::recursive_mutex wgc_thread_uninit_mutex; + volatile int wgc_thread_res { 0 }; HWND capture_hwnd { nullptr }; HMONITOR capture_hmonitor { nullptr }; @@ -577,17 +581,16 @@ static int wgc_thread_worker(AVFilterContext *avctx) return msg.wParam; } -static void *wgc_thread_entry(void *arg) noexcept +static void wgc_thread_entry(AVFilterContext *avctx) noexcept { - AVFilterContext *avctx = static_cast<AVFilterContext*>(arg); GfxCaptureContext *cctx = CCTX(avctx->priv); GfxCaptureContextCpp *ctx = cctx->ctx; { - static const char name_prefix[] = "wgc_winrt@0x"; - char thread_name[sizeof(name_prefix) + sizeof(void*) * 2]; - snprintf(thread_name, sizeof(thread_name), "%s%" PRIxPTR, name_prefix, (uintptr_t)avctx); - ff_thread_setname(thread_name); + static const wchar_t name_prefix[] = L"wgc_winrt@0x"; + wchar_t thread_name[FF_ARRAY_ELEMS(name_prefix) + sizeof(void*) * 2] = { 0 }; + swprintf(thread_name, FF_ARRAY_ELEMS(thread_name), L"%s%" PRIxPTR, name_prefix, (uintptr_t)avctx); + ctx->fn.SetThreadDescription(GetCurrentThread(), thread_name); std::lock_guard init_lock(ctx->wgc_thread_init_mutex); ctx->wgc_thread_id = GetCurrentThreadId(); @@ -605,8 +608,10 @@ static void *wgc_thread_entry(void *arg) noexcept } ctx->wgc_thread_init_cond.notify_all(); - if (ctx->wgc_thread_init_res < 0) - return (void*)(intptr_t)AVERROR(ENOSYS); + if (ctx->wgc_thread_init_res < 0) { + ctx->wgc_thread_res = ctx->wgc_thread_init_res; + return; + } } int ret; @@ -626,7 +631,7 @@ static void *wgc_thread_entry(void *arg) noexcept std::lock_guard uninit_lock(ctx->wgc_thread_uninit_mutex); wgc_thread_uninit(avctx); - return (void*)(intptr_t)ret; + ctx->wgc_thread_res = ret; } /*********************************** @@ -639,16 +644,14 @@ static int stop_wgc_thread(AVFilterContext *avctx) GfxCaptureContextCpp *ctx = cctx->ctx; int ret = 0; - if (ctx->wgc_thread_created) { + if (ctx->wgc_thread.joinable()) { if (ctx->wgc_thread_id && !PostThreadMessage(ctx->wgc_thread_id, WM_WGC_THREAD_SHUTDOWN, 0, 0)) av_log(avctx, AV_LOG_ERROR, "Failed to post shutdown message to WGC thread\n"); - void *wgc_res = nullptr; - pthread_join(ctx->wgc_thread, &wgc_res); - ret = (int)(intptr_t)wgc_res; + ctx->wgc_thread.join(); + ret = ctx->wgc_thread_res; ctx->wgc_thread_id = 0; - ctx->wgc_thread_created = false; } return ret; @@ -659,7 +662,7 @@ static int start_wgc_thread(AVFilterContext *avctx) GfxCaptureContext *cctx = CCTX(avctx->priv); GfxCaptureContextCpp *ctx = cctx->ctx; - if (ctx->wgc_thread_created || ctx->wgc_thread_id) { + if (ctx->wgc_thread.joinable() || ctx->wgc_thread_id) { av_log(avctx, AV_LOG_ERROR, "Double-creation of WGC thread\n"); return AVERROR_BUG; } @@ -667,14 +670,13 @@ static int start_wgc_thread(AVFilterContext *avctx) std::unique_lock wgc_lock(ctx->wgc_thread_init_mutex); ctx->wgc_thread_init_res = INT_MAX; - int ret = pthread_create(&ctx->wgc_thread, nullptr, wgc_thread_entry, avctx); - if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "Failed to create WGC thread\n"); - return ret; + try { + ctx->wgc_thread = std::thread(wgc_thread_entry, avctx); + } catch (const std::system_error &e) { + av_log(avctx, AV_LOG_ERROR, "Failed to create WGC thread: %s\n", e.what()); + return AVERROR_EXTERNAL; } - ctx->wgc_thread_created = true; - if (!ctx->wgc_thread_init_cond.wait_for(wgc_lock, std::chrono::seconds(1), [&]() { return ctx->wgc_thread_init_res != INT_MAX; })) { @@ -997,6 +999,7 @@ static av_cold int load_functions(AVFilterContext *avctx) LOAD_DLL(ctx->fn.d3d11_handle, "d3d11.dll"); LOAD_DLL(ctx->fn.coremsg_handle, "coremessaging.dll"); LOAD_DLL(ctx->fn.user32_handle, "user32.dll"); + LOAD_DLL(ctx->fn.kernel32_handle, "kernel32.dll"); LOAD_DLL(ctx->fn.d3dcompiler_handle, "d3dcompiler_47.dll"); LOAD_FUNC(ctx->fn.combase_handle, RoInitialize); @@ -1012,6 +1015,8 @@ static av_cold int load_functions(AVFilterContext *avctx) LOAD_FUNC(ctx->fn.user32_handle, SetThreadDpiAwarenessContext); + LOAD_FUNC(ctx->fn.kernel32_handle, SetThreadDescription); + LOAD_FUNC(ctx->fn.d3dcompiler_handle, D3DCompile); #undef LOAD_FUNC -- 2.49.1 _______________________________________________ ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org
reply other threads:[~2025-09-14 20:26 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=175788155997.25.17669914793093092952@463a07221176 \ --to=ffmpeg-devel@ffmpeg.org \ --cc=code@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
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