2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2011-03-03 19:20:02 +03:00
|
|
|
|
|
|
|
#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 {
|
|
|
|
|
2013-10-08 22:47:37 +04:00
|
|
|
Mutex* sMutex = nullptr;
|
|
|
|
CondVar* sCondVar = nullptr;
|
2011-03-03 19:20:02 +03:00
|
|
|
bool sTracerProcessed = false;
|
|
|
|
|
|
|
|
// This function is called from the main (UI) thread.
|
|
|
|
gboolean TracerCallback(gpointer data)
|
|
|
|
{
|
2011-10-18 18:51:36 +04:00
|
|
|
mozilla::SignalTracerThread();
|
2011-03-03 19:20:02 +03:00
|
|
|
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;
|
2013-10-08 22:47:37 +04:00
|
|
|
sMutex = nullptr;
|
|
|
|
sCondVar = nullptr;
|
2011-03-03 19:20:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function is called from the background tracer thread.
|
|
|
|
bool FireAndWaitForTracerEvent()
|
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(sMutex && sCondVar, "Tracing not initialized!");
|
2011-03-03 19:20:02 +03:00
|
|
|
|
|
|
|
// Send a default-priority idle event through the
|
|
|
|
// event loop, and wait for it to finish.
|
|
|
|
MutexAutoLock lock(*sMutex);
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!sTracerProcessed, "Tracer synchronization state is wrong");
|
2011-03-03 19:20:02 +03:00
|
|
|
g_idle_add_full(G_PRIORITY_DEFAULT,
|
|
|
|
TracerCallback,
|
2013-10-08 22:47:37 +04:00
|
|
|
nullptr,
|
|
|
|
nullptr);
|
2011-03-03 19:20:02 +03:00
|
|
|
while (!sTracerProcessed)
|
|
|
|
sCondVar->Wait();
|
|
|
|
sTracerProcessed = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-18 18:51:36 +04:00
|
|
|
void SignalTracerThread()
|
|
|
|
{
|
2012-03-13 00:13:14 +04:00
|
|
|
if (!sMutex || !sCondVar)
|
|
|
|
return;
|
2011-10-18 18:51:36 +04:00
|
|
|
MutexAutoLock lock(*sMutex);
|
2012-12-21 20:11:24 +04:00
|
|
|
if (!sTracerProcessed) {
|
|
|
|
sTracerProcessed = true;
|
|
|
|
sCondVar->Notify();
|
|
|
|
}
|
2011-10-18 18:51:36 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|