summaryrefslogtreecommitdiffstats
path: root/widget/gtk/WidgetTraceEvent.cpp
blob: 161e5302cc721b4a8dfa729b802b784d8ac33444 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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 <glib.h>
#include <mozilla/CondVar.h>
#include <mozilla/Mutex.h>
#include <stdio.h>

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