2014-06-30 15:39:45 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-03-26 16:11:00 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_SyncRunnable_h
|
2013-12-13 00:30:59 +00:00
|
|
|
#define mozilla_SyncRunnable_h
|
2013-03-26 16:11:00 +00:00
|
|
|
|
|
|
|
#include "nsThreadUtils.h"
|
2016-05-04 08:24:25 +00:00
|
|
|
#include "mozilla/AbstractThread.h"
|
2013-03-26 16:11:00 +00:00
|
|
|
#include "mozilla/Monitor.h"
|
2020-01-20 16:18:20 +00:00
|
|
|
#include <utility>
|
2013-03-26 16:11:00 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class will wrap a nsIRunnable and dispatch it to the main thread
|
|
|
|
* synchronously. This is different from nsIEventTarget.DISPATCH_SYNC:
|
|
|
|
* this class does not spin the event loop waiting for the event to be
|
|
|
|
* dispatched. This means that you don't risk reentrance from pending
|
|
|
|
* messages, but you must be sure that the target thread does not ever block
|
|
|
|
* on this thread, or else you will deadlock.
|
|
|
|
*
|
|
|
|
* Typical usage:
|
2015-10-18 05:24:48 +00:00
|
|
|
* RefPtr<SyncRunnable> sr = new SyncRunnable(new myrunnable...());
|
2013-03-26 16:11:00 +00:00
|
|
|
* sr->DispatchToThread(t);
|
|
|
|
*
|
|
|
|
* We also provide a convenience wrapper:
|
|
|
|
* SyncRunnable::DispatchToThread(new myrunnable...());
|
|
|
|
*
|
|
|
|
*/
|
2016-04-26 00:23:21 +00:00
|
|
|
class SyncRunnable : public Runnable {
|
2013-03-26 16:11:00 +00:00
|
|
|
public:
|
2014-09-02 13:50:07 +00:00
|
|
|
explicit SyncRunnable(nsIRunnable* aRunnable)
|
2017-06-12 19:34:10 +00:00
|
|
|
: Runnable("SyncRunnable"),
|
|
|
|
mRunnable(aRunnable),
|
2013-03-26 16:11:00 +00:00
|
|
|
mMonitor("SyncRunnable"),
|
2014-03-11 16:42:58 +00:00
|
|
|
mDone(false) {}
|
2013-03-26 16:11:00 +00:00
|
|
|
|
2016-06-29 02:24:54 +00:00
|
|
|
explicit SyncRunnable(already_AddRefed<nsIRunnable> aRunnable)
|
2017-06-12 19:34:10 +00:00
|
|
|
: Runnable("SyncRunnable"),
|
2018-05-30 19:15:35 +00:00
|
|
|
mRunnable(std::move(aRunnable)),
|
2016-06-29 02:24:54 +00:00
|
|
|
mMonitor("SyncRunnable"),
|
|
|
|
mDone(false) {}
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
void DispatchToThread(nsIEventTarget* aThread, bool aForceDispatch = false) {
|
2013-03-26 16:11:00 +00:00
|
|
|
nsresult rv;
|
|
|
|
bool on;
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
if (!aForceDispatch) {
|
|
|
|
rv = aThread->IsOnCurrentThread(&on);
|
2013-09-26 00:58:16 +00:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
if (NS_SUCCEEDED(rv) && on) {
|
|
|
|
mRunnable->Run();
|
|
|
|
return;
|
|
|
|
}
|
2013-03-26 16:11:00 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
rv = aThread->Dispatch(this, NS_DISPATCH_NORMAL);
|
2013-03-26 16:11:00 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-03-11 16:42:58 +00:00
|
|
|
mozilla::MonitorAutoLock lock(mMonitor);
|
|
|
|
while (!mDone) {
|
|
|
|
lock.Wait();
|
|
|
|
}
|
2013-03-26 16:11:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 08:24:25 +00:00
|
|
|
void DispatchToThread(AbstractThread* aThread, bool aForceDispatch = false) {
|
|
|
|
if (!aForceDispatch && aThread->IsCurrentThreadIn()) {
|
|
|
|
mRunnable->Run();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check we don't have tail dispatching here. Otherwise we will deadlock
|
|
|
|
// ourself when spinning the loop below.
|
|
|
|
MOZ_ASSERT(!aThread->RequiresTailDispatchFromCurrentThread());
|
|
|
|
|
|
|
|
aThread->Dispatch(RefPtr<nsIRunnable>(this).forget());
|
|
|
|
mozilla::MonitorAutoLock lock(mMonitor);
|
|
|
|
while (!mDone) {
|
|
|
|
lock.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 07:15:35 +00:00
|
|
|
static void DispatchToThread(nsIEventTarget* aThread, nsIRunnable* aRunnable,
|
|
|
|
bool aForceDispatch = false) {
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<SyncRunnable> s(new SyncRunnable(aRunnable));
|
2014-05-27 07:15:35 +00:00
|
|
|
s->DispatchToThread(aThread, aForceDispatch);
|
2013-03-26 16:11:00 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 08:24:25 +00:00
|
|
|
static void DispatchToThread(AbstractThread* aThread, nsIRunnable* aRunnable,
|
|
|
|
bool aForceDispatch = false) {
|
|
|
|
RefPtr<SyncRunnable> s(new SyncRunnable(aRunnable));
|
|
|
|
s->DispatchToThread(aThread, aForceDispatch);
|
|
|
|
}
|
|
|
|
|
2013-03-26 16:11:00 +00:00
|
|
|
protected:
|
2016-08-08 02:18:10 +00:00
|
|
|
NS_IMETHOD Run() override {
|
2013-03-26 16:11:00 +00:00
|
|
|
mRunnable->Run();
|
2014-03-11 16:42:58 +00:00
|
|
|
|
|
|
|
mozilla::MonitorAutoLock lock(mMonitor);
|
|
|
|
MOZ_ASSERT(!mDone);
|
|
|
|
|
|
|
|
mDone = true;
|
|
|
|
mMonitor.Notify();
|
|
|
|
|
2013-03-26 16:11:00 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIRunnable> mRunnable;
|
|
|
|
mozilla::Monitor mMonitor;
|
2014-03-11 16:42:58 +00:00
|
|
|
bool mDone;
|
2013-03-26 16:11:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_SyncRunnable_h
|