michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/WidgetTraceEvent.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: using mozilla::CondVar; michael@0: using mozilla::Mutex; michael@0: using mozilla::MutexAutoLock; michael@0: michael@0: namespace { michael@0: michael@0: Mutex* sMutex = nullptr; michael@0: CondVar* sCondVar = nullptr; michael@0: bool sTracerProcessed = false; michael@0: michael@0: // This function is called from the main (UI) thread. michael@0: gboolean TracerCallback(gpointer data) michael@0: { michael@0: mozilla::SignalTracerThread(); michael@0: return FALSE; michael@0: } michael@0: michael@0: } // namespace michael@0: michael@0: namespace mozilla { michael@0: michael@0: bool InitWidgetTracing() michael@0: { michael@0: sMutex = new Mutex("Event tracer thread mutex"); michael@0: sCondVar = new CondVar(*sMutex, "Event tracer thread condvar"); michael@0: return sMutex && sCondVar; michael@0: } michael@0: michael@0: void CleanUpWidgetTracing() michael@0: { michael@0: delete sMutex; michael@0: delete sCondVar; michael@0: sMutex = nullptr; michael@0: sCondVar = nullptr; michael@0: } michael@0: michael@0: // This function is called from the background tracer thread. michael@0: bool FireAndWaitForTracerEvent() michael@0: { michael@0: NS_ABORT_IF_FALSE(sMutex && sCondVar, "Tracing not initialized!"); michael@0: michael@0: // Send a default-priority idle event through the michael@0: // event loop, and wait for it to finish. michael@0: MutexAutoLock lock(*sMutex); michael@0: NS_ABORT_IF_FALSE(!sTracerProcessed, "Tracer synchronization state is wrong"); michael@0: g_idle_add_full(G_PRIORITY_DEFAULT, michael@0: TracerCallback, michael@0: nullptr, michael@0: nullptr); michael@0: while (!sTracerProcessed) michael@0: sCondVar->Wait(); michael@0: sTracerProcessed = false; michael@0: return true; michael@0: } michael@0: michael@0: void SignalTracerThread() michael@0: { michael@0: if (!sMutex || !sCondVar) michael@0: return; michael@0: MutexAutoLock lock(*sMutex); michael@0: if (!sTracerProcessed) { michael@0: sTracerProcessed = true; michael@0: sCondVar->Notify(); michael@0: } michael@0: } michael@0: michael@0: } // namespace mozilla