diff options
| author | Moss Heim <moss.heim@qt.io> | 2025-09-16 11:34:33 +0200 |
|---|---|---|
| committer | Moss Heim <moss.heim@qt.io> | 2025-09-29 14:21:47 +0200 |
| commit | 9bd5625923733a96134fe2588233e4711736b35e (patch) | |
| tree | 20ff0066fea8dbc2f538e07f34a94098c1b790bd | |
| parent | 851cfd30f53aaf8f9198468f1812360016c0cf73 (diff) | |
Ensure function-scope static variables are only initialized once
Previously some static variables were initialized by checking the value
after the variable was declared, without any guarding mutex. This could
result in multiple threads performing the initialization simultaneously.
In some cases the initialization was idempotent and innocuous, but the
pattern should be avoided, when side effects are introduced there will
be difficult-to-reproduce data races.
Pick-to: 6.10 6.9
Change-Id: I536738dca102cfd1a2a59dd8156084e07ad1922b
Reviewed-by: Peter Varga <pvarga@inf.u-szeged.hu>
| -rw-r--r-- | src/core/ozone/ozone_platform_qt.cpp | 9 | ||||
| -rw-r--r-- | src/core/ozone/ozone_util_qt.cpp | 49 |
2 files changed, 29 insertions, 29 deletions
diff --git a/src/core/ozone/ozone_platform_qt.cpp b/src/core/ozone/ozone_platform_qt.cpp index f4a169a22..3fa7f638e 100644 --- a/src/core/ozone/ozone_platform_qt.cpp +++ b/src/core/ozone/ozone_platform_qt.cpp @@ -47,6 +47,8 @@ extern void *GetQtXDisplay(); #endif +#include <mutex> + namespace ui { namespace { @@ -112,12 +114,11 @@ OzonePlatformQt::~OzonePlatformQt() {} const ui::OzonePlatform::PlatformProperties &OzonePlatformQt::GetPlatformProperties() { static base::NoDestructor<ui::OzonePlatform::PlatformProperties> properties; - static bool initialized = false; - if (!initialized) { + static std::once_flag flag; + std::call_once(flag, [this]() { DCHECK(m_supportsNativePixmaps); properties->fetch_buffer_formats_for_gmb_on_gpu = m_supportsNativePixmaps.value(); - initialized = true; - } + }); return *properties; } diff --git a/src/core/ozone/ozone_util_qt.cpp b/src/core/ozone/ozone_util_qt.cpp index 0e9f871a0..76db63999 100644 --- a/src/core/ozone/ozone_util_qt.cpp +++ b/src/core/ozone/ozone_util_qt.cpp @@ -27,8 +27,6 @@ void *getXDisplay() QOpenGLContext *getQOpenGLContext() { - static QOpenGLContext *tmpGLContext = nullptr; - #if QT_CONFIG(opengl) if (auto *shareContext = QOpenGLContext::globalShareContext()) return shareContext; @@ -36,44 +34,45 @@ QOpenGLContext *getQOpenGLContext() if (auto *currentContext = QOpenGLContext::currentContext()) return currentContext; - if (!tmpGLContext) { - tmpGLContext = new QOpenGLContext(); + static QOpenGLContext *tmpGLContext = []() { + auto tmpGLContext = new QOpenGLContext(); tmpGLContext->create(); - QObject::connect(qGuiApp, &QGuiApplication::aboutToQuit, []() { delete tmpGLContext; }); - } -#endif + QObject::connect(qGuiApp, &QGuiApplication::aboutToQuit, [=]() { delete tmpGLContext; }); + return tmpGLContext; + }(); return tmpGLContext; +#else + return nullptr; +#endif } bool usingGLX() { - static std::optional<bool> result; - #if QT_CONFIG(opengl) && QT_CONFIG(xcb_glx_plugin) - if (result.has_value()) - return result.value(); - - QOpenGLContext *context = getQOpenGLContext(); - result = (context->nativeInterface<QNativeInterface::QGLXContext>() != nullptr); + static bool result = []() { + QOpenGLContext *context = getQOpenGLContext(); + return context->nativeInterface<QNativeInterface::QGLXContext>() != nullptr; + }(); + + return result; +#else + return false; #endif - - return result.value_or(false); } bool usingEGL() { - static std::optional<bool> result; - #if QT_CONFIG(opengl) && QT_CONFIG(egl) - if (result.has_value()) - return result.value(); - - QOpenGLContext *context = getQOpenGLContext(); - result = (context->nativeInterface<QNativeInterface::QEGLContext>() != nullptr); + static bool result = []() { + QOpenGLContext *context = getQOpenGLContext(); + return context->nativeInterface<QNativeInterface::QEGLContext>() != nullptr; + }(); + + return result; +#else + return false; #endif - - return result.value_or(false); } } // namespace OzoneUtilQt |
