widget/gtk/WidgetTraceEvent.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "mozilla/WidgetTraceEvent.h"
michael@0 6
michael@0 7 #include <glib.h>
michael@0 8 #include <mozilla/CondVar.h>
michael@0 9 #include <mozilla/Mutex.h>
michael@0 10 #include <stdio.h>
michael@0 11
michael@0 12 using mozilla::CondVar;
michael@0 13 using mozilla::Mutex;
michael@0 14 using mozilla::MutexAutoLock;
michael@0 15
michael@0 16 namespace {
michael@0 17
michael@0 18 Mutex* sMutex = nullptr;
michael@0 19 CondVar* sCondVar = nullptr;
michael@0 20 bool sTracerProcessed = false;
michael@0 21
michael@0 22 // This function is called from the main (UI) thread.
michael@0 23 gboolean TracerCallback(gpointer data)
michael@0 24 {
michael@0 25 mozilla::SignalTracerThread();
michael@0 26 return FALSE;
michael@0 27 }
michael@0 28
michael@0 29 } // namespace
michael@0 30
michael@0 31 namespace mozilla {
michael@0 32
michael@0 33 bool InitWidgetTracing()
michael@0 34 {
michael@0 35 sMutex = new Mutex("Event tracer thread mutex");
michael@0 36 sCondVar = new CondVar(*sMutex, "Event tracer thread condvar");
michael@0 37 return sMutex && sCondVar;
michael@0 38 }
michael@0 39
michael@0 40 void CleanUpWidgetTracing()
michael@0 41 {
michael@0 42 delete sMutex;
michael@0 43 delete sCondVar;
michael@0 44 sMutex = nullptr;
michael@0 45 sCondVar = nullptr;
michael@0 46 }
michael@0 47
michael@0 48 // This function is called from the background tracer thread.
michael@0 49 bool FireAndWaitForTracerEvent()
michael@0 50 {
michael@0 51 NS_ABORT_IF_FALSE(sMutex && sCondVar, "Tracing not initialized!");
michael@0 52
michael@0 53 // Send a default-priority idle event through the
michael@0 54 // event loop, and wait for it to finish.
michael@0 55 MutexAutoLock lock(*sMutex);
michael@0 56 NS_ABORT_IF_FALSE(!sTracerProcessed, "Tracer synchronization state is wrong");
michael@0 57 g_idle_add_full(G_PRIORITY_DEFAULT,
michael@0 58 TracerCallback,
michael@0 59 nullptr,
michael@0 60 nullptr);
michael@0 61 while (!sTracerProcessed)
michael@0 62 sCondVar->Wait();
michael@0 63 sTracerProcessed = false;
michael@0 64 return true;
michael@0 65 }
michael@0 66
michael@0 67 void SignalTracerThread()
michael@0 68 {
michael@0 69 if (!sMutex || !sCondVar)
michael@0 70 return;
michael@0 71 MutexAutoLock lock(*sMutex);
michael@0 72 if (!sTracerProcessed) {
michael@0 73 sTracerProcessed = true;
michael@0 74 sCondVar->Notify();
michael@0 75 }
michael@0 76 }
michael@0 77
michael@0 78 } // namespace mozilla

mercurial