2014-06-30 19:39:45 +04: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 20:11:00 +04: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 04:30:59 +04:00
|
|
|
#define mozilla_SyncRunnable_h
|
2013-03-26 20:11:00 +04:00
|
|
|
|
|
|
|
#include "nsThreadUtils.h"
|
2016-05-04 11:24:25 +03:00
|
|
|
#include "mozilla/AbstractThread.h"
|
2013-03-26 20:11:00 +04:00
|
|
|
#include "mozilla/Monitor.h"
|
2016-06-29 05:24:54 +03:00
|
|
|
#include "mozilla/Move.h"
|
2013-03-26 20:11:00 +04: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 08:24:48 +03:00
|
|
|
* RefPtr<SyncRunnable> sr = new SyncRunnable(new myrunnable...());
|
2013-03-26 20:11:00 +04:00
|
|
|
* sr->DispatchToThread(t);
|
|
|
|
*
|
|
|
|
* We also provide a convenience wrapper:
|
|
|
|
* SyncRunnable::DispatchToThread(new myrunnable...());
|
|
|
|
*
|
|
|
|
*/
|
2016-04-26 03:23:21 +03:00
|
|
|
class SyncRunnable : public Runnable
|
2013-03-26 20:11:00 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-09-02 17:50:07 +04:00
|
|
|
explicit SyncRunnable(nsIRunnable* aRunnable)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("SyncRunnable")
|
|
|
|
, mRunnable(aRunnable)
|
2013-03-26 20:11:00 +04:00
|
|
|
, mMonitor("SyncRunnable")
|
2014-03-11 20:42:58 +04:00
|
|
|
, mDone(false)
|
2014-05-27 11:15:35 +04:00
|
|
|
{
|
|
|
|
}
|
2013-03-26 20:11:00 +04:00
|
|
|
|
2016-06-29 05:24:54 +03:00
|
|
|
explicit SyncRunnable(already_AddRefed<nsIRunnable> aRunnable)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("SyncRunnable")
|
|
|
|
, mRunnable(Move(aRunnable))
|
2016-06-29 05:24:54 +03:00
|
|
|
, mMonitor("SyncRunnable")
|
|
|
|
, mDone(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void DispatchToThread(nsIEventTarget* aThread, bool aForceDispatch = false)
|
2013-03-26 20:11:00 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
bool on;
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!aForceDispatch) {
|
|
|
|
rv = aThread->IsOnCurrentThread(&on);
|
2013-09-26 04:58:16 +04:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
if (NS_SUCCEEDED(rv) && on) {
|
|
|
|
mRunnable->Run();
|
|
|
|
return;
|
|
|
|
}
|
2013-03-26 20:11:00 +04:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
rv = aThread->Dispatch(this, NS_DISPATCH_NORMAL);
|
2013-03-26 20:11:00 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-03-11 20:42:58 +04:00
|
|
|
mozilla::MonitorAutoLock lock(mMonitor);
|
|
|
|
while (!mDone) {
|
|
|
|
lock.Wait();
|
|
|
|
}
|
2013-03-26 20:11:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 11:24:25 +03: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 11:15:35 +04:00
|
|
|
static void DispatchToThread(nsIEventTarget* aThread,
|
|
|
|
nsIRunnable* aRunnable,
|
|
|
|
bool aForceDispatch = false)
|
2013-03-26 20:11:00 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SyncRunnable> s(new SyncRunnable(aRunnable));
|
2014-05-27 11:15:35 +04:00
|
|
|
s->DispatchToThread(aThread, aForceDispatch);
|
2013-03-26 20:11:00 +04:00
|
|
|
}
|
|
|
|
|
2016-05-04 11:24:25 +03:00
|
|
|
static void DispatchToThread(AbstractThread* aThread,
|
|
|
|
nsIRunnable* aRunnable,
|
|
|
|
bool aForceDispatch = false)
|
|
|
|
{
|
|
|
|
RefPtr<SyncRunnable> s(new SyncRunnable(aRunnable));
|
|
|
|
s->DispatchToThread(aThread, aForceDispatch);
|
|
|
|
}
|
|
|
|
|
2013-03-26 20:11:00 +04:00
|
|
|
protected:
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2013-03-26 20:11:00 +04:00
|
|
|
{
|
|
|
|
mRunnable->Run();
|
2014-03-11 20:42:58 +04:00
|
|
|
|
|
|
|
mozilla::MonitorAutoLock lock(mMonitor);
|
|
|
|
MOZ_ASSERT(!mDone);
|
|
|
|
|
|
|
|
mDone = true;
|
|
|
|
mMonitor.Notify();
|
|
|
|
|
2013-03-26 20:11:00 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIRunnable> mRunnable;
|
|
|
|
mozilla::Monitor mMonitor;
|
2014-03-11 20:42:58 +04:00
|
|
|
bool mDone;
|
2013-03-26 20:11:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_SyncRunnable_h
|