From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- widget/gtk/WidgetTraceEvent.cpp | 68 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 widget/gtk/WidgetTraceEvent.cpp (limited to 'widget/gtk/WidgetTraceEvent.cpp') diff --git a/widget/gtk/WidgetTraceEvent.cpp b/widget/gtk/WidgetTraceEvent.cpp new file mode 100644 index 0000000000..161e5302cc --- /dev/null +++ b/widget/gtk/WidgetTraceEvent.cpp @@ -0,0 +1,68 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/WidgetTraceEvent.h" + +#include +#include +#include +#include + +using mozilla::CondVar; +using mozilla::Mutex; +using mozilla::MutexAutoLock; + +namespace { + +Mutex* sMutex = nullptr; +CondVar* sCondVar = nullptr; +bool sTracerProcessed = false; + +// This function is called from the main (UI) thread. +gboolean TracerCallback(gpointer data) { + mozilla::SignalTracerThread(); + return FALSE; +} + +} // namespace + +namespace mozilla { + +bool InitWidgetTracing() { + sMutex = new Mutex("Event tracer thread mutex"); + sCondVar = new CondVar(*sMutex, "Event tracer thread condvar"); + return true; +} + +void CleanUpWidgetTracing() { + delete sMutex; + delete sCondVar; + sMutex = nullptr; + sCondVar = nullptr; +} + +// This function is called from the background tracer thread. +bool FireAndWaitForTracerEvent() { + MOZ_ASSERT(sMutex && sCondVar, "Tracing not initialized!"); + + // Send a default-priority idle event through the + // event loop, and wait for it to finish. + MutexAutoLock lock(*sMutex); + MOZ_ASSERT(!sTracerProcessed, "Tracer synchronization state is wrong"); + g_idle_add_full(G_PRIORITY_DEFAULT, TracerCallback, nullptr, nullptr); + while (!sTracerProcessed) sCondVar->Wait(); + sTracerProcessed = false; + return true; +} + +void SignalTracerThread() { + if (!sMutex || !sCondVar) return; + MutexAutoLock lock(*sMutex); + if (!sTracerProcessed) { + sTracerProcessed = true; + sCondVar->Notify(); + } +} + +} // namespace mozilla -- cgit v1.2.3