widget/gonk/WidgetTraceEvent.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/gonk/WidgetTraceEvent.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,96 @@
     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 +#include "mozilla/StaticPtr.h"
    1.10 +#include "nsThreadUtils.h"
    1.11 +#include <mozilla/CondVar.h>
    1.12 +#include <mozilla/Mutex.h>
    1.13 +
    1.14 +using mozilla::CondVar;
    1.15 +using mozilla::Mutex;
    1.16 +using mozilla::MutexAutoLock;
    1.17 +
    1.18 +namespace mozilla {
    1.19 +  class TracerRunnable : public nsRunnable {
    1.20 +    public:
    1.21 +      TracerRunnable() {
    1.22 +        mTracerLock = new Mutex("TracerRunnable");
    1.23 +        mTracerCondVar = new CondVar(*mTracerLock, "TracerRunnable");
    1.24 +        mMainThread = do_GetMainThread();
    1.25 +      }
    1.26 +
    1.27 +      ~TracerRunnable() {
    1.28 +        delete mTracerCondVar;
    1.29 +        delete mTracerLock;
    1.30 +        mTracerLock = nullptr;
    1.31 +        mTracerCondVar = nullptr;
    1.32 +      }
    1.33 +
    1.34 +      virtual nsresult Run() {
    1.35 +        MutexAutoLock lock(*mTracerLock);
    1.36 +        mHasRun = true;
    1.37 +        mTracerCondVar->Notify();
    1.38 +        return NS_OK;
    1.39 +      }
    1.40 +
    1.41 +      bool Fire() {
    1.42 +        if (!mTracerLock || !mTracerCondVar) {
    1.43 +          return false;
    1.44 +        }
    1.45 +
    1.46 +        MutexAutoLock lock(*mTracerLock);
    1.47 +        mHasRun = false;
    1.48 +        mMainThread->Dispatch(this, NS_DISPATCH_NORMAL);
    1.49 +        while (!mHasRun) {
    1.50 +          mTracerCondVar->Wait();
    1.51 +        }
    1.52 +        return true;
    1.53 +      }
    1.54 +
    1.55 +      void Signal() {
    1.56 +        MutexAutoLock lock(*mTracerLock);
    1.57 +        mHasRun = true;
    1.58 +        mTracerCondVar->Notify();
    1.59 +      }
    1.60 +
    1.61 +    private:
    1.62 +      Mutex* mTracerLock;
    1.63 +      CondVar* mTracerCondVar;
    1.64 +      bool mHasRun;
    1.65 +      nsCOMPtr<nsIThread> mMainThread;
    1.66 +  };
    1.67 +
    1.68 +  StaticRefPtr<TracerRunnable> sTracerRunnable;
    1.69 +
    1.70 +  bool InitWidgetTracing()
    1.71 +  {
    1.72 +    if (!sTracerRunnable) {
    1.73 +      sTracerRunnable = new TracerRunnable();
    1.74 +    }
    1.75 +    return true;
    1.76 +  }
    1.77 +
    1.78 +  void CleanUpWidgetTracing()
    1.79 +  {
    1.80 +    sTracerRunnable = nullptr;
    1.81 +  }
    1.82 +
    1.83 +  bool FireAndWaitForTracerEvent()
    1.84 +  {
    1.85 +    if (sTracerRunnable) {
    1.86 +      return sTracerRunnable->Fire();
    1.87 +    }
    1.88 +
    1.89 +    return false;
    1.90 +  }
    1.91 +
    1.92 +  void SignalTracerThread()
    1.93 +  {
    1.94 +    if (sTracerRunnable) {
    1.95 +      return sTracerRunnable->Signal();
    1.96 +    }
    1.97 +  }
    1.98 +}  // namespace mozilla
    1.99 +

mercurial