Bug 1873042 - Part 3: Optimise substring(0, 1) pattern. r=jandem
[gecko.git] / widget / gtk / WidgetTraceEvent.cpp
blob161e5302cc721b4a8dfa729b802b784d8ac33444
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) {
24 mozilla::SignalTracerThread();
25 return FALSE;
28 } // namespace
30 namespace mozilla {
32 bool InitWidgetTracing() {
33 sMutex = new Mutex("Event tracer thread mutex");
34 sCondVar = new CondVar(*sMutex, "Event tracer thread condvar");
35 return true;
38 void CleanUpWidgetTracing() {
39 delete sMutex;
40 delete sCondVar;
41 sMutex = nullptr;
42 sCondVar = nullptr;
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;
56 return true;
59 void SignalTracerThread() {
60 if (!sMutex || !sCondVar) return;
61 MutexAutoLock lock(*sMutex);
62 if (!sTracerProcessed) {
63 sTracerProcessed = true;
64 sCondVar->Notify();
68 } // namespace mozilla