From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 1A27347CAB for ; Sun, 14 Sep 2025 20:26:13 +0000 (UTC) Authentication-Results: ffbox; dkim=fail (body hash mismatch (got b'bTCveY44RTAepzWvXPfwZtJusdKr8srVoFzi0BvpO1o=', expected b'JvWHfvANYeDOSgmOa5fFWyvT0Zs2UZn8F8oWJYRmfaI=')) header.d=ffmpeg.org header.i=@ffmpeg.org header.a=rsa-sha256 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ffmpeg.org; i=@ffmpeg.org; q=dns/txt; s=mail; t=1757881569; h=mime-version : to : date : message-id : reply-to : subject : list-id : list-archive : list-archive : list-help : list-owner : list-post : list-subscribe : list-unsubscribe : from : cc : content-type : content-transfer-encoding : from; bh=bTCveY44RTAepzWvXPfwZtJusdKr8srVoFzi0BvpO1o=; b=YMY8VgSW+mxV1JtsMPWFSEer9txRjTNGQE0jFfoDLwmvhggZn4//8yN12xrz5jWKROYHP sqL5Wd8y1botWdzu2Or36Zz/KbjN/6l9q/jFHAc6KdnDErSUKfqAtbh/v/1LwZtc6JD7CZL fX+f0TVzUKxBBkHUskYsvgGTPFhX9+gX+KOHuTKv6dVdWmoN3hxaCN4AAn5iN2P4+eLN/DA FT5T2pCDFcI2htJkAwQKXwJgK4NbJpxtYLJ2A53ORvpbSzrLQ2KbPD9qZLnZ0o5AHO1Q1LB C93CD7g3VIAWECriCqVhDyTz40A3Y1WyVa8MqQc1aaMNQguyOCf4bETOTcxg== Received: from [172.19.0.4] (unknown [172.19.0.4]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id 2076568E7D7; Sun, 14 Sep 2025 23:26:09 +0300 (EEST) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=ffmpeg.org; s=arc; t=1757881568; b=FYkTyDzKaisOpzh+vdNu2eIks17TtOYJZs7d1owruikVZzLUEoSkaeIZzsnXsWxSGN95O SwzN+4Q+qlKSbXqR32m1g5Y+C7NuivVsu2MFXW6G1Woa42AszfIkI+mCHI59sV4LShXslJy bdVgnE7zsvLc/8qQQLNaeVktGRh1ehZtoXz1j1Vair46uAQ4oScsg0AOorrzU6PrqNPQqMc pQHShw9whxjJofS/9ceIkejr1TTfNZ8dX+TkeQq17/gwtgWq3TS1ldH+2ig8Q8pOWSXMB/m 125DkJ3Hgse2wK7cxk1TD9Go54ousHs+KfBGJhVwfhm1OfW64IB3NohKC1Qg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=ffmpeg.org; s=arc; t=1757881568; h=from : sender : reply-to : subject : date : message-id : to : cc : mime-version : content-type : content-transfer-encoding : content-id : content-description : resent-date : resent-from : resent-sender : resent-to : resent-cc : resent-message-id : in-reply-to : references : list-id : list-help : list-unsubscribe : list-subscribe : list-post : list-owner : list-archive; bh=tEau4s3bH5T2MFazFK9akm3qsx9DoW54XhNbp+Jyzc0=; b=dOzJ0rg0OHptkLiZMQ/1OIbBUGpbTkKxRnyiLksxil9ygg1jzhiFF+NRh8cCas7O2pCSf PpRDkJ+RYU68sTAWMv+NBjS917QhnvJz7nj2W7rLMYbFI5e+0Wg5Kv5wOgLW1MBLDfNY6W9 1UcM3Bkx3uOv2+JmNFlMdPrWMG9+tlaWiFAGw8T0lKmaa3gu/1i07nYMVQ0ORnP0/wzuAd9 CLiJLdIy4ikS3VAQY91N6girMdWilbIwXbKW14hvrUw4pcD+1vTHU6+AF3u9nssDHCRKGQX Z/fUJo9p4q3nr+YCn3oin4XEV9+wAFBlIa5nxFS4+Q7dLnjYmiTvQVt+xFVw== ARC-Authentication-Results: i=1; ffmpeg.org; dkim=pass header.d=ffmpeg.org header.i=@ffmpeg.org; arc=none; dmarc=none Authentication-Results: ffmpeg.org; dkim=pass header.d=ffmpeg.org header.i=@ffmpeg.org; arc=none (Message is not ARC signed); dmarc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ffmpeg.org; i=@ffmpeg.org; q=dns/txt; s=mail; t=1757881559; h=content-type : mime-version : content-transfer-encoding : from : to : reply-to : subject : date : from; bh=JvWHfvANYeDOSgmOa5fFWyvT0Zs2UZn8F8oWJYRmfaI=; b=ZYP8F2UgwUU+WUdPEjebizyYPN+tP/0wO0IKJB1t3EIcjEuMnLfvDzoyLOFq62mFrgFA3 eQPsJboJCUZvSruJrLwKewiOR2NL71BntLQ06jdYiafTZBf8qqU5tJ8hjiVoV/bkh6ahkBF evikE4sHGrAi/7+rdlaLMR5wOISt2FnPVsDmcfwEpYLIXUObH1BOJKIufAJ8uTwecgrO730 uIwDcbbznOWI7yS3YwPV8OjF0xbWqP/rH33KV1mClOVHlDf7Hgj6QQoQTqqchDuJsh84mNk ZfMzJ7l9ex8Xey+o88vzg8COY4WXsGVTm2EKyY9KNhYdObqyJ2Qwi1At+haA== Received: from ed19c606a818 (code.ffmpeg.org [188.245.149.3]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id D2C4168E7B2 for ; Sun, 14 Sep 2025 23:25:59 +0300 (EEST) MIME-Version: 1.0 To: ffmpeg-devel@ffmpeg.org Date: Sun, 14 Sep 2025 20:25:59 -0000 Message-ID: <175788155997.25.17669914793093092952@463a07221176> Message-ID-Hash: JOE2655BZX2YFVNGRZLLHIFTUTWAOTIC X-Message-ID-Hash: JOE2655BZX2YFVNGRZLLHIFTUTWAOTIC X-MailFrom: code@ffmpeg.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; header-match-ffmpeg-devel.ffmpeg.org-0; header-match-ffmpeg-devel.ffmpeg.org-1; header-match-ffmpeg-devel.ffmpeg.org-2; header-match-ffmpeg-devel.ffmpeg.org-3; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list Reply-To: FFmpeg development discussions and patches Subject: [FFmpeg-devel] [PATCH] avfilter/vsrc_gfxcapture: switch to std::thread (PR #20520) List-Id: FFmpeg development discussions and patches Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Timo Rothenpieler via ffmpeg-devel Cc: Timo Rothenpieler Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Archived-At: List-Archive: List-Post: 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 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 #include #include +#include #include #include #include #include +#include #include 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 wgc; std::unique_ptr 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(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