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
michael@0 | 1 | /* -*- Mode: C++; tab-width: 12; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef mozilla_SyncRunnable_h |
michael@0 | 7 | #define mozilla_SyncRunnable_h |
michael@0 | 8 | |
michael@0 | 9 | #include "nsThreadUtils.h" |
michael@0 | 10 | #include "mozilla/Monitor.h" |
michael@0 | 11 | |
michael@0 | 12 | namespace mozilla { |
michael@0 | 13 | |
michael@0 | 14 | /** |
michael@0 | 15 | * This class will wrap a nsIRunnable and dispatch it to the main thread |
michael@0 | 16 | * synchronously. This is different from nsIEventTarget.DISPATCH_SYNC: |
michael@0 | 17 | * this class does not spin the event loop waiting for the event to be |
michael@0 | 18 | * dispatched. This means that you don't risk reentrance from pending |
michael@0 | 19 | * messages, but you must be sure that the target thread does not ever block |
michael@0 | 20 | * on this thread, or else you will deadlock. |
michael@0 | 21 | * |
michael@0 | 22 | * Typical usage: |
michael@0 | 23 | * nsRefPtr<SyncRunnable> sr = new SyncRunnable(new myrunnable...()); |
michael@0 | 24 | * sr->DispatchToThread(t); |
michael@0 | 25 | * |
michael@0 | 26 | * We also provide a convenience wrapper: |
michael@0 | 27 | * SyncRunnable::DispatchToThread(new myrunnable...()); |
michael@0 | 28 | * |
michael@0 | 29 | */ |
michael@0 | 30 | class SyncRunnable : public nsRunnable |
michael@0 | 31 | { |
michael@0 | 32 | public: |
michael@0 | 33 | SyncRunnable(nsIRunnable* r) |
michael@0 | 34 | : mRunnable(r) |
michael@0 | 35 | , mMonitor("SyncRunnable") |
michael@0 | 36 | , mDone(false) |
michael@0 | 37 | { } |
michael@0 | 38 | |
michael@0 | 39 | void DispatchToThread(nsIEventTarget* thread, |
michael@0 | 40 | bool forceDispatch = false) |
michael@0 | 41 | { |
michael@0 | 42 | nsresult rv; |
michael@0 | 43 | bool on; |
michael@0 | 44 | |
michael@0 | 45 | if (!forceDispatch) { |
michael@0 | 46 | rv = thread->IsOnCurrentThread(&on); |
michael@0 | 47 | MOZ_ASSERT(NS_SUCCEEDED(rv)); |
michael@0 | 48 | if (NS_SUCCEEDED(rv) && on) { |
michael@0 | 49 | mRunnable->Run(); |
michael@0 | 50 | return; |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | rv = thread->Dispatch(this, NS_DISPATCH_NORMAL); |
michael@0 | 55 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 56 | mozilla::MonitorAutoLock lock(mMonitor); |
michael@0 | 57 | while (!mDone) { |
michael@0 | 58 | lock.Wait(); |
michael@0 | 59 | } |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | static void DispatchToThread(nsIEventTarget* thread, |
michael@0 | 64 | nsIRunnable* r, |
michael@0 | 65 | bool forceDispatch = false) |
michael@0 | 66 | { |
michael@0 | 67 | nsRefPtr<SyncRunnable> s(new SyncRunnable(r)); |
michael@0 | 68 | s->DispatchToThread(thread, forceDispatch); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | protected: |
michael@0 | 72 | NS_IMETHODIMP Run() |
michael@0 | 73 | { |
michael@0 | 74 | mRunnable->Run(); |
michael@0 | 75 | |
michael@0 | 76 | mozilla::MonitorAutoLock lock(mMonitor); |
michael@0 | 77 | MOZ_ASSERT(!mDone); |
michael@0 | 78 | |
michael@0 | 79 | mDone = true; |
michael@0 | 80 | mMonitor.Notify(); |
michael@0 | 81 | |
michael@0 | 82 | return NS_OK; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | private: |
michael@0 | 86 | nsCOMPtr<nsIRunnable> mRunnable; |
michael@0 | 87 | mozilla::Monitor mMonitor; |
michael@0 | 88 | bool mDone; |
michael@0 | 89 | }; |
michael@0 | 90 | |
michael@0 | 91 | } // namespace mozilla |
michael@0 | 92 | |
michael@0 | 93 | #endif // mozilla_SyncRunnable_h |