gecko/widget/gtk/WidgetTraceEvent.cpp
Nicholas Nethercote 6ee3666899 Bug 1127201 (attempt 2, part 1) - Replace most NS_ABORT_IF_FALSE calls with MOZ_ASSERT. r=Waldo.
--HG--
extra : rebase_source : 488e401ff87e31a2074c4108c4df0572d9536667
2015-02-09 14:34:50 -08:00

79 lines
1.7 KiB
C++

/* 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 sMutex && sCondVar;
}
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