Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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