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
;
13 using mozilla::MutexAutoLock
;
16 class TracerRunnable
: public nsRunnable
{
19 mTracerLock
= new Mutex("TracerRunnable");
20 mTracerCondVar
= new CondVar(*mTracerLock
, "TracerRunnable");
21 mMainThread
= do_GetMainThread();
25 delete mTracerCondVar
;
27 mTracerLock
= nullptr;
28 mTracerCondVar
= nullptr;
31 virtual nsresult
Run() {
32 MutexAutoLock
lock(*mTracerLock
);
34 mTracerCondVar
->Notify();
39 if (!mTracerLock
|| !mTracerCondVar
) {
43 MutexAutoLock
lock(*mTracerLock
);
45 mMainThread
->Dispatch(this, NS_DISPATCH_NORMAL
);
47 mTracerCondVar
->Wait();
53 MutexAutoLock
lock(*mTracerLock
);
55 mTracerCondVar
->Notify();
60 CondVar
* mTracerCondVar
;
62 nsCOMPtr
<nsIThread
> mMainThread
;
65 StaticRefPtr
<TracerRunnable
> sTracerRunnable
;
67 bool InitWidgetTracing()
69 if (!sTracerRunnable
) {
70 sTracerRunnable
= new TracerRunnable();
75 void CleanUpWidgetTracing()
77 sTracerRunnable
= nullptr;
80 bool FireAndWaitForTracerEvent()
82 if (sTracerRunnable
) {
83 return sTracerRunnable
->Fire();
89 void SignalTracerThread()
91 if (sTracerRunnable
) {
92 return sTracerRunnable
->Signal();
95 } // namespace mozilla