1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "mozilla/WidgetTraceEvent.h"
8 #include <mozilla/CondVar.h>
9 #include <mozilla/Mutex.h>
12 using mozilla::CondVar
;
14 using mozilla::MutexAutoLock
;
18 Mutex
* sMutex
= nullptr;
19 CondVar
* sCondVar
= nullptr;
20 bool sTracerProcessed
= false;
22 // This function is called from the main (UI) thread.
23 gboolean
TracerCallback(gpointer data
) {
24 mozilla::SignalTracerThread();
32 bool InitWidgetTracing() {
33 sMutex
= new Mutex("Event tracer thread mutex");
34 sCondVar
= new CondVar(*sMutex
, "Event tracer thread condvar");
38 void CleanUpWidgetTracing() {
45 // This function is called from the background tracer thread.
46 bool FireAndWaitForTracerEvent() {
47 MOZ_ASSERT(sMutex
&& sCondVar
, "Tracing not initialized!");
49 // Send a default-priority idle event through the
50 // event loop, and wait for it to finish.
51 MutexAutoLock
lock(*sMutex
);
52 MOZ_ASSERT(!sTracerProcessed
, "Tracer synchronization state is wrong");
53 g_idle_add_full(G_PRIORITY_DEFAULT
, TracerCallback
, nullptr, nullptr);
54 while (!sTracerProcessed
) sCondVar
->Wait();
55 sTracerProcessed
= false;
59 void SignalTracerThread() {
60 if (!sMutex
|| !sCondVar
) return;
61 MutexAutoLock
lock(*sMutex
);
62 if (!sTracerProcessed
) {
63 sTracerProcessed
= true;
68 } // namespace mozilla