d3d11: Serialize ensure_element_data() call
authorSeungha Yang <seungha@centricular.com>
Thu, 7 Jul 2022 16:30:13 +0000 (01:30 +0900)
committerSeungha Yang <seungha@centricular.com>
Thu, 7 Jul 2022 16:35:34 +0000 (01:35 +0900)
It can be called any time from random thread. Thus, multiple
device objects can be created for the same physical device
if the call is not serialized, that's not a scenario we expect.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/2727>

subprojects/gst-plugins-bad/gst-libs/gst/d3d11/gstd3d11utils.cpp

index f6f772813ae93337d15208b6b73d529025799a9f..f5fe77e12c49618bd426634087ea7aac2bf423a2 100644 (file)
 
 #include <windows.h>
 #include <versionhelpers.h>
+#include <mutex>
+
+/* *INDENT-OFF* */
+static std::recursive_mutex _context_lock;
+/* *INDENT-ON* */
 
 GST_DEBUG_CATEGORY_STATIC (GST_CAT_CONTEXT);
 #ifndef GST_DISABLE_GST_DEBUG
@@ -380,6 +385,9 @@ gst_d3d11_ensure_element_data (GstElement * element, gint adapter,
     GstD3D11Device ** device)
 {
   guint target_adapter = 0;
+  /* *INDENT-OFF* */
+  std::lock_guard<std::recursive_mutex> lk (_context_lock);
+  /* *INDENT-ON* */
 
   g_return_val_if_fail (element != NULL, FALSE);
   g_return_val_if_fail (device != NULL, FALSE);
@@ -447,6 +455,10 @@ gboolean
 gst_d3d11_ensure_element_data_for_adapter_luid (GstElement * element,
     gint64 adapter_luid, GstD3D11Device ** device)
 {
+  /* *INDENT-OFF* */
+  std::lock_guard<std::recursive_mutex> lk (_context_lock);
+  /* *INDENT-ON* */
+
   g_return_val_if_fail (element != NULL, FALSE);
   g_return_val_if_fail (device != NULL, FALSE);