Bumping manifests a=b2g-bump
[gecko.git] / widget / gtk / WidgetTraceEvent.cpp
blob4bc7f510b7a556533f582466213ebcf3e257020a
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"
7 #include <glib.h>
8 #include <mozilla/CondVar.h>
9 #include <mozilla/Mutex.h>
10 #include <stdio.h>
12 using mozilla::CondVar;
13 using mozilla::Mutex;
14 using mozilla::MutexAutoLock;
16 namespace {
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)
25 mozilla::SignalTracerThread();
26 return FALSE;
29 } // namespace
31 namespace mozilla {
33 bool InitWidgetTracing()
35 sMutex = new Mutex("Event tracer thread mutex");
36 sCondVar = new CondVar(*sMutex, "Event tracer thread condvar");
37 return sMutex && sCondVar;
40 void CleanUpWidgetTracing()
42 delete sMutex;
43 delete sCondVar;
44 sMutex = nullptr;
45 sCondVar = nullptr;
48 // This function is called from the background tracer thread.
49 bool FireAndWaitForTracerEvent()
51 NS_ABORT_IF_FALSE(sMutex && sCondVar, "Tracing not initialized!");
53 // Send a default-priority idle event through the
54 // event loop, and wait for it to finish.
55 MutexAutoLock lock(*sMutex);
56 NS_ABORT_IF_FALSE(!sTracerProcessed, "Tracer synchronization state is wrong");
57 g_idle_add_full(G_PRIORITY_DEFAULT,
58 TracerCallback,
59 nullptr,
60 nullptr);
61 while (!sTracerProcessed)
62 sCondVar->Wait();
63 sTracerProcessed = false;
64 return true;
67 void SignalTracerThread()
69 if (!sMutex || !sCondVar)
70 return;
71 MutexAutoLock lock(*sMutex);
72 if (!sTracerProcessed) {
73 sTracerProcessed = true;
74 sCondVar->Notify();
78 } // namespace mozilla