From 5a47e29914d5326c39611eedb1e0a7ccd935977b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Thu, 21 Oct 2021 14:45:31 +0200 Subject: [PATCH] wasm: prevent thread cross-talk when logging Emscriptens implementation of fprintf does not provide mutal exclusion when called from multiple threads, for the emsdk versions Qt 5.15, Qt 6.2, and current dev is using. Pick-to: 5.15 6.2 Change-Id: Ied92730b735b11e4e5e85442de48fc25cbad0611 Reviewed-by: Lorn Potter --- src/corelib/global/qlogging.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp index 6e4da27d5e..7c37d5b432 100644 --- a/src/corelib/global/qlogging.cpp +++ b/src/corelib/global/qlogging.cpp @@ -1748,6 +1748,13 @@ static void stderr_message_handler(QtMsgType type, const QMessageLogContext &con if (formattedMessage.isNull()) return; +#ifdef Q_OS_WASM + // Prevent thread cross-talk, which causes Emscripten to log + // non-valid UTF-8. FIXME: remove once we upgrade to emsdk > 2.0.30 + static QBasicMutex m; + auto locker = qt_unique_lock(m); +#endif + fprintf(stderr, "%s\n", formattedMessage.toLocal8Bit().constData()); fflush(stderr); }