widget/gtk/WidgetTraceEvent.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/gtk/WidgetTraceEvent.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "mozilla/WidgetTraceEvent.h"
     1.9 +
    1.10 +#include <glib.h>
    1.11 +#include <mozilla/CondVar.h>
    1.12 +#include <mozilla/Mutex.h>
    1.13 +#include <stdio.h>
    1.14 +
    1.15 +using mozilla::CondVar;
    1.16 +using mozilla::Mutex;
    1.17 +using mozilla::MutexAutoLock;
    1.18 +
    1.19 +namespace {
    1.20 +
    1.21 +Mutex* sMutex = nullptr;
    1.22 +CondVar* sCondVar = nullptr;
    1.23 +bool sTracerProcessed = false;
    1.24 +
    1.25 +// This function is called from the main (UI) thread.
    1.26 +gboolean TracerCallback(gpointer data)
    1.27 +{
    1.28 +  mozilla::SignalTracerThread();
    1.29 +  return FALSE;
    1.30 +}
    1.31 +
    1.32 +} // namespace
    1.33 +
    1.34 +namespace mozilla {
    1.35 +
    1.36 +bool InitWidgetTracing()
    1.37 +{
    1.38 +  sMutex = new Mutex("Event tracer thread mutex");
    1.39 +  sCondVar = new CondVar(*sMutex, "Event tracer thread condvar");
    1.40 +  return sMutex && sCondVar;
    1.41 +}
    1.42 +
    1.43 +void CleanUpWidgetTracing()
    1.44 +{
    1.45 +  delete sMutex;
    1.46 +  delete sCondVar;
    1.47 +  sMutex = nullptr;
    1.48 +  sCondVar = nullptr;
    1.49 +}
    1.50 +
    1.51 +// This function is called from the background tracer thread.
    1.52 +bool FireAndWaitForTracerEvent()
    1.53 +{
    1.54 +  NS_ABORT_IF_FALSE(sMutex && sCondVar, "Tracing not initialized!");
    1.55 +
    1.56 +  // Send a default-priority idle event through the
    1.57 +  // event loop, and wait for it to finish.
    1.58 +  MutexAutoLock lock(*sMutex);
    1.59 +  NS_ABORT_IF_FALSE(!sTracerProcessed, "Tracer synchronization state is wrong");
    1.60 +  g_idle_add_full(G_PRIORITY_DEFAULT,
    1.61 +                  TracerCallback,
    1.62 +                  nullptr,
    1.63 +                  nullptr);
    1.64 +  while (!sTracerProcessed)
    1.65 +    sCondVar->Wait();
    1.66 +  sTracerProcessed = false;
    1.67 +  return true;
    1.68 +}
    1.69 +
    1.70 +void SignalTracerThread()
    1.71 +{
    1.72 +  if (!sMutex || !sCondVar)
    1.73 +    return;
    1.74 +  MutexAutoLock lock(*sMutex);
    1.75 +  if (!sTracerProcessed) {
    1.76 +    sTracerProcessed = true;
    1.77 +    sCondVar->Notify();
    1.78 +  }
    1.79 +}
    1.80 +
    1.81 +}  // namespace mozilla

mercurial