widget/gonk/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.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include "mozilla/WidgetTraceEvent.h"
     6 #include "mozilla/StaticPtr.h"
     7 #include "nsThreadUtils.h"
     8 #include <mozilla/CondVar.h>
     9 #include <mozilla/Mutex.h>
    11 using mozilla::CondVar;
    12 using mozilla::Mutex;
    13 using mozilla::MutexAutoLock;
    15 namespace mozilla {
    16   class TracerRunnable : public nsRunnable {
    17     public:
    18       TracerRunnable() {
    19         mTracerLock = new Mutex("TracerRunnable");
    20         mTracerCondVar = new CondVar(*mTracerLock, "TracerRunnable");
    21         mMainThread = do_GetMainThread();
    22       }
    24       ~TracerRunnable() {
    25         delete mTracerCondVar;
    26         delete mTracerLock;
    27         mTracerLock = nullptr;
    28         mTracerCondVar = nullptr;
    29       }
    31       virtual nsresult Run() {
    32         MutexAutoLock lock(*mTracerLock);
    33         mHasRun = true;
    34         mTracerCondVar->Notify();
    35         return NS_OK;
    36       }
    38       bool Fire() {
    39         if (!mTracerLock || !mTracerCondVar) {
    40           return false;
    41         }
    43         MutexAutoLock lock(*mTracerLock);
    44         mHasRun = false;
    45         mMainThread->Dispatch(this, NS_DISPATCH_NORMAL);
    46         while (!mHasRun) {
    47           mTracerCondVar->Wait();
    48         }
    49         return true;
    50       }
    52       void Signal() {
    53         MutexAutoLock lock(*mTracerLock);
    54         mHasRun = true;
    55         mTracerCondVar->Notify();
    56       }
    58     private:
    59       Mutex* mTracerLock;
    60       CondVar* mTracerCondVar;
    61       bool mHasRun;
    62       nsCOMPtr<nsIThread> mMainThread;
    63   };
    65   StaticRefPtr<TracerRunnable> sTracerRunnable;
    67   bool InitWidgetTracing()
    68   {
    69     if (!sTracerRunnable) {
    70       sTracerRunnable = new TracerRunnable();
    71     }
    72     return true;
    73   }
    75   void CleanUpWidgetTracing()
    76   {
    77     sTracerRunnable = nullptr;
    78   }
    80   bool FireAndWaitForTracerEvent()
    81   {
    82     if (sTracerRunnable) {
    83       return sTracerRunnable->Fire();
    84     }
    86     return false;
    87   }
    89   void SignalTracerThread()
    90   {
    91     if (sTracerRunnable) {
    92       return sTracerRunnable->Signal();
    93     }
    94   }
    95 }  // namespace mozilla

mercurial