Bug 1639632 - convert xpcom/ to inherit from mozilla::Runnable where possible; r=KrisWright

A little less boilerplate is nice.

Differential Revision: https://phabricator.services.mozilla.com/D76178
This commit is contained in:
Nathan Froyd 2020-05-20 20:37:58 +00:00
parent 64e6f96111
commit 1a536ec126
10 changed files with 39 additions and 75 deletions

View File

@ -305,7 +305,7 @@ NS_IMPL_ISUPPORTS(LowEventsReporter, nsIMemoryReporter)
* other observers will synchronously free some memory that we'll be able to
* purge here.
*/
class nsJemallocFreeDirtyPagesRunnable final : public nsIRunnable {
class nsJemallocFreeDirtyPagesRunnable final : public Runnable {
~nsJemallocFreeDirtyPagesRunnable() = default;
#if defined(XP_WIN)
@ -313,11 +313,10 @@ class nsJemallocFreeDirtyPagesRunnable final : public nsIRunnable {
#endif
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIRUNNABLE
};
NS_IMPL_ISUPPORTS(nsJemallocFreeDirtyPagesRunnable, nsIRunnable)
nsJemallocFreeDirtyPagesRunnable() : Runnable("nsJemallocFreeDirtyPagesRunnable") {}
};
NS_IMETHODIMP
nsJemallocFreeDirtyPagesRunnable::Run() {

View File

@ -140,10 +140,8 @@ TEST(Atoms, Table)
EXPECT_EQ(NS_GetNumberOfAtoms(), count + 1);
}
class nsAtomRunner final : public nsIRunnable {
class nsAtomRunner final : public Runnable {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_IMETHOD Run() final {
for (int i = 0; i < 10000; i++) {
RefPtr<nsAtom> atom = NS_Atomize(u"A Testing Atom");
@ -151,12 +149,12 @@ class nsAtomRunner final : public nsIRunnable {
return NS_OK;
}
nsAtomRunner() : Runnable("nsAtomRunner") {}
private:
~nsAtomRunner() = default;
};
NS_IMPL_ISUPPORTS(nsAtomRunner, nsIRunnable)
TEST(Atoms, ConcurrentAccessing)
{
static const size_t kThreadCount = 4;

View File

@ -13,10 +13,8 @@
using namespace mozilla;
class nsThreadSafeAutoRefCntRunner final : public nsIRunnable {
class nsThreadSafeAutoRefCntRunner final : public Runnable {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_IMETHOD Run() final {
for (int i = 0; i < 10000; i++) {
if (++sRefCnt == 1) {
@ -33,12 +31,12 @@ class nsThreadSafeAutoRefCntRunner final : public nsIRunnable {
static Atomic<uint32_t, Relaxed> sIncToOne;
static Atomic<uint32_t, Relaxed> sDecToZero;
nsThreadSafeAutoRefCntRunner() : Runnable("nsThreadSafeAutoRefCntRunner") {}
private:
~nsThreadSafeAutoRefCntRunner() = default;
};
NS_IMPL_ISUPPORTS(nsThreadSafeAutoRefCntRunner, nsIRunnable)
ThreadSafeAutoRefCnt nsThreadSafeAutoRefCntRunner::sRefCnt;
Atomic<uint32_t, Relaxed> nsThreadSafeAutoRefCntRunner::sIncToOne(0);
Atomic<uint32_t, Relaxed> nsThreadSafeAutoRefCntRunner::sDecToZero(0);

View File

@ -49,10 +49,8 @@ static nsresult WriteAll(nsIOutputStream* os, const char* buf, uint32_t bufLen,
return NS_OK;
}
class nsReceiver final : public nsIRunnable {
class nsReceiver final : public Runnable {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_IMETHOD Run() override {
nsresult rv;
char buf[101];
@ -81,7 +79,7 @@ class nsReceiver final : public nsIRunnable {
return rv;
}
explicit nsReceiver(nsIInputStream* in) : mIn(in), mCount(0) {}
explicit nsReceiver(nsIInputStream* in) : Runnable("nsReceiver"), mIn(in), mCount(0) {}
uint32_t GetBytesRead() { return mCount; }
@ -93,7 +91,6 @@ class nsReceiver final : public nsIRunnable {
uint32_t mCount;
};
NS_IMPL_ISUPPORTS(nsReceiver, nsIRunnable)
static nsresult TestPipe(nsIInputStream* in, nsIOutputStream* out) {
RefPtr<nsReceiver> receiver = new nsReceiver(in);
@ -136,10 +133,8 @@ static nsresult TestPipe(nsIInputStream* in, nsIOutputStream* out) {
////////////////////////////////////////////////////////////////////////////////
class nsShortReader final : public nsIRunnable {
class nsShortReader final : public Runnable {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_IMETHOD Run() override {
nsresult rv;
char buf[101];
@ -171,7 +166,7 @@ class nsShortReader final : public nsIRunnable {
return rv;
}
explicit nsShortReader(nsIInputStream* in) : mIn(in), mReceived(0) {
explicit nsShortReader(nsIInputStream* in) : Runnable("nsShortReader"), mIn(in), mReceived(0) {
mMon = new ReentrantMonitor("nsShortReader");
}
@ -205,8 +200,6 @@ class nsShortReader final : public nsIRunnable {
ReentrantMonitor* mMon;
};
NS_IMPL_ISUPPORTS(nsShortReader, nsIRunnable)
static nsresult TestShortWrites(nsIInputStream* in, nsIOutputStream* out) {
RefPtr<nsShortReader> receiver = new nsShortReader(in);
nsresult rv;
@ -249,10 +242,8 @@ static nsresult TestShortWrites(nsIInputStream* in, nsIOutputStream* out) {
////////////////////////////////////////////////////////////////////////////////
class nsPump final : public nsIRunnable {
class nsPump final : public Runnable {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_IMETHOD Run() override {
nsresult rv;
uint32_t count;
@ -277,7 +268,7 @@ class nsPump final : public nsIRunnable {
}
nsPump(nsIInputStream* in, nsIOutputStream* out)
: mIn(in), mOut(out), mCount(0) {}
: Runnable("nsPump"), mIn(in), mOut(out), mCount(0) {}
private:
~nsPump() = default;
@ -288,8 +279,6 @@ class nsPump final : public nsIRunnable {
uint32_t mCount;
};
NS_IMPL_ISUPPORTS(nsPump, nsIRunnable)
TEST(Pipes, ChainedPipes)
{
nsresult rv;

View File

@ -19,11 +19,10 @@
using namespace mozilla;
class Task final : public nsIRunnable {
class Task final : public Runnable {
public:
NS_DECL_THREADSAFE_ISUPPORTS
Task(int i, Atomic<int>& aCounter) : mIndex(i), mCounter(aCounter) {}
Task(int i, Atomic<int>& aCounter)
: Runnable("TestThreadPool::Task"), mIndex(i), mCounter(aCounter) {}
NS_IMETHOD Run() override {
printf("###(%d) running from thread: %p\n", mIndex,
@ -42,7 +41,6 @@ class Task final : public nsIRunnable {
int mIndex;
Atomic<int>& mCounter;
};
NS_IMPL_ISUPPORTS(Task, nsIRunnable)
TEST(ThreadPool, Main)
{

View File

@ -13,12 +13,10 @@
#include "mozilla/Monitor.h"
#include "gtest/gtest.h"
class nsRunner final : public nsIRunnable {
class nsRunner final : public Runnable {
~nsRunner() = default;
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_IMETHOD Run() override {
nsCOMPtr<nsIThread> thread;
nsresult rv = NS_GetCurrentThread(getter_AddRefs(thread));
@ -32,14 +30,12 @@ class nsRunner final : public nsIRunnable {
return rv;
}
explicit nsRunner(int num) : mNum(num) {}
explicit nsRunner(int num) : Runnable("nsRunner"), mNum(num) {}
protected:
int mNum;
};
NS_IMPL_ISUPPORTS(nsRunner, nsIRunnable)
TEST(Threads, Main)
{
nsresult rv;
@ -62,10 +58,8 @@ TEST(Threads, Main)
PR_MillisecondsToInterval(100)); // hopefully the runner will quit here
}
class nsStressRunner final : public nsIRunnable {
class nsStressRunner final : public Runnable {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_IMETHOD Run() override {
EXPECT_FALSE(mWasRun);
mWasRun = true;
@ -76,7 +70,7 @@ class nsStressRunner final : public nsIRunnable {
return NS_OK;
}
explicit nsStressRunner(int num) : mNum(num), mWasRun(false) {
explicit nsStressRunner(int num) : Runnable("nsStressRunner"), mNum(num), mWasRun(false) {
PR_AtomicIncrement(&gNum);
}
@ -93,8 +87,6 @@ class nsStressRunner final : public nsIRunnable {
int32_t nsStressRunner::gNum = 0;
NS_IMPL_ISUPPORTS(nsStressRunner, nsIRunnable)
TEST(Threads, Stress)
{
#if defined(XP_WIN) && defined(MOZ_ASAN) // Easily hits OOM
@ -132,9 +124,8 @@ TEST(Threads, Stress)
mozilla::Monitor* gAsyncShutdownReadyMonitor;
mozilla::Monitor* gBeginAsyncShutdownMonitor;
class AsyncShutdownPreparer : public nsIRunnable {
class AsyncShutdownPreparer : public Runnable {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_IMETHOD Run() override {
EXPECT_FALSE(mWasRun);
@ -146,7 +137,7 @@ class AsyncShutdownPreparer : public nsIRunnable {
return NS_OK;
}
explicit AsyncShutdownPreparer() : mWasRun(false) {}
explicit AsyncShutdownPreparer() : Runnable("AsyncShutdownPreparer"), mWasRun(false) {}
private:
virtual ~AsyncShutdownPreparer() { EXPECT_TRUE(mWasRun); }
@ -155,12 +146,8 @@ class AsyncShutdownPreparer : public nsIRunnable {
bool mWasRun;
};
NS_IMPL_ISUPPORTS(AsyncShutdownPreparer, nsIRunnable)
class AsyncShutdownWaiter : public nsIRunnable {
class AsyncShutdownWaiter : public Runnable {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_IMETHOD Run() override {
EXPECT_FALSE(mWasRun);
mWasRun = true;
@ -184,7 +171,7 @@ class AsyncShutdownWaiter : public nsIRunnable {
return NS_OK;
}
explicit AsyncShutdownWaiter() : mWasRun(false) {}
explicit AsyncShutdownWaiter() : Runnable("AsyncShutdownWaiter"), mWasRun(false) {}
private:
virtual ~AsyncShutdownWaiter() { EXPECT_TRUE(mWasRun); }
@ -193,24 +180,20 @@ class AsyncShutdownWaiter : public nsIRunnable {
bool mWasRun;
};
NS_IMPL_ISUPPORTS(AsyncShutdownWaiter, nsIRunnable)
class SameThreadSentinel : public nsIRunnable {
class SameThreadSentinel : public Runnable {
public:
NS_DECL_ISUPPORTS
NS_IMETHOD Run() override {
mozilla::MonitorAutoLock lock(*gBeginAsyncShutdownMonitor);
lock.Notify();
return NS_OK;
}
SameThreadSentinel() : Runnable("SameThreadSentinel") {}
private:
virtual ~SameThreadSentinel() = default;
};
NS_IMPL_ISUPPORTS(SameThreadSentinel, nsIRunnable)
TEST(Threads, AsyncShutdown)
{
gAsyncShutdownReadyMonitor = new mozilla::Monitor("gAsyncShutdownReady");

View File

@ -26,10 +26,11 @@ using namespace mozilla;
using namespace mozilla::tasktracer;
#endif
NS_IMPL_ISUPPORTS(TimerThread, nsIRunnable, nsIObserver)
NS_IMPL_ISUPPORTS_INHERITED(TimerThread, Runnable, nsIObserver)
TimerThread::TimerThread()
: mInitialized(false),
: Runnable("TimerThread"),
mInitialized(false),
mMonitor("TimerThread.mMonitor"),
mShutdown(false),
mWaiting(false),

View File

@ -27,7 +27,7 @@ namespace mozilla {
class TimeStamp;
} // namespace mozilla
class TimerThread final : public nsIRunnable, public nsIObserver {
class TimerThread final : public mozilla::Runnable, public nsIObserver {
public:
typedef mozilla::Monitor Monitor;
typedef mozilla::TimeStamp TimeStamp;
@ -36,7 +36,7 @@ class TimerThread final : public nsIRunnable, public nsIObserver {
TimerThread();
nsresult InitLocks();
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIRUNNABLE
NS_DECL_NSIOBSERVER

View File

@ -37,13 +37,11 @@ static MOZ_THREAD_LOCAL(nsThreadPool*) gCurrentThreadPool;
#define DEFAULT_IDLE_THREAD_LIMIT 1
#define DEFAULT_IDLE_THREAD_TIMEOUT PR_SecondsToInterval(60)
NS_IMPL_ADDREF(nsThreadPool)
NS_IMPL_RELEASE(nsThreadPool)
NS_IMPL_QUERY_INTERFACE(nsThreadPool, nsIThreadPool, nsIEventTarget,
nsIRunnable)
NS_IMPL_ISUPPORTS_INHERITED(nsThreadPool, Runnable, nsIThreadPool, nsIEventTarget)
nsThreadPool::nsThreadPool()
: mMutex("[nsThreadPool.mMutex]"),
: Runnable("nsThreadPool"),
mMutex("[nsThreadPool.mMutex]"),
mEventsAvailable(mMutex, "[nsThreadPool.mEventsAvailable]"),
mThreadLimit(DEFAULT_THREAD_LIMIT),
mIdleThreadLimit(DEFAULT_IDLE_THREAD_LIMIT),

View File

@ -20,9 +20,9 @@
#include "mozilla/Mutex.h"
#include "mozilla/Monitor.h"
class nsThreadPool final : public nsIThreadPool, public nsIRunnable {
class nsThreadPool final : public mozilla::Runnable, public nsIThreadPool {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIEVENTTARGET_FULL
NS_DECL_NSITHREADPOOL
NS_DECL_NSIRUNNABLE