Bumping manifests a=b2g-bump
[gecko.git] / widget / gonk / WidgetTraceEvent.cpp
blob48e5b47391455d2ca974ee8383ce26de2274ed3c
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();
24 ~TracerRunnable() {
25 delete mTracerCondVar;
26 delete mTracerLock;
27 mTracerLock = nullptr;
28 mTracerCondVar = nullptr;
31 virtual nsresult Run() {
32 MutexAutoLock lock(*mTracerLock);
33 mHasRun = true;
34 mTracerCondVar->Notify();
35 return NS_OK;
38 bool Fire() {
39 if (!mTracerLock || !mTracerCondVar) {
40 return false;
43 MutexAutoLock lock(*mTracerLock);
44 mHasRun = false;
45 mMainThread->Dispatch(this, NS_DISPATCH_NORMAL);
46 while (!mHasRun) {
47 mTracerCondVar->Wait();
49 return true;
52 void Signal() {
53 MutexAutoLock lock(*mTracerLock);
54 mHasRun = true;
55 mTracerCondVar->Notify();
58 private:
59 Mutex* mTracerLock;
60 CondVar* mTracerCondVar;
61 bool mHasRun;
62 nsCOMPtr<nsIThread> mMainThread;
65 StaticRefPtr<TracerRunnable> sTracerRunnable;
67 bool InitWidgetTracing()
69 if (!sTracerRunnable) {
70 sTracerRunnable = new TracerRunnable();
72 return true;
75 void CleanUpWidgetTracing()
77 sTracerRunnable = nullptr;
80 bool FireAndWaitForTracerEvent()
82 if (sTracerRunnable) {
83 return sTracerRunnable->Fire();
86 return false;
89 void SignalTracerThread()
91 if (sTracerRunnable) {
92 return sTracerRunnable->Signal();
95 } // namespace mozilla