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