2015-04-09 20:25:05 +03: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: */
|
2012-05-21 15:12:37 +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/. */
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsXPCOMCIDInternal.h"
|
|
|
|
#include "nsIThreadPool.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsIRunnable.h"
|
2016-01-05 23:15:59 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2015-01-27 04:49:22 +03:00
|
|
|
#include "mozilla/Atomics.h"
|
2015-05-09 02:32:27 +03:00
|
|
|
#include "mozilla/Monitor.h"
|
2015-01-27 04:49:22 +03:00
|
|
|
#include "gtest/gtest.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2016-01-05 23:15:59 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class Task final : public nsIRunnable
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
public:
|
2013-07-19 06:31:26 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-01-27 04:49:22 +03:00
|
|
|
explicit Task(int i) : mIndex(i) {}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Run() override
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
|
|
|
printf("###(%d) running from thread: %p\n", mIndex, (void *) PR_GetCurrentThread());
|
|
|
|
int r = (int) ((float) rand() * 200 / RAND_MAX);
|
|
|
|
PR_Sleep(PR_MillisecondsToInterval(r));
|
|
|
|
printf("###(%d) exiting from thread: %p\n", mIndex, (void *) PR_GetCurrentThread());
|
2015-01-27 04:49:22 +03:00
|
|
|
++sCount;
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2015-01-27 04:49:22 +03:00
|
|
|
static mozilla::Atomic<int> sCount;
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
private:
|
2015-01-27 04:49:22 +03:00
|
|
|
~Task() {}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
int mIndex;
|
|
|
|
};
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(Task, nsIRunnable)
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2015-01-27 04:49:22 +03:00
|
|
|
mozilla::Atomic<int> Task::sCount;
|
|
|
|
|
|
|
|
TEST(ThreadPool, Main)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
nsCOMPtr<nsIThreadPool> pool = do_CreateInstance(NS_THREADPOOL_CONTRACTID);
|
2015-01-27 04:49:22 +03:00
|
|
|
EXPECT_TRUE(pool);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i) {
|
|
|
|
nsCOMPtr<nsIRunnable> task = new Task(i);
|
2015-01-27 04:49:22 +03:00
|
|
|
EXPECT_TRUE(task);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
pool->Dispatch(task, NS_DISPATCH_NORMAL);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
pool->Shutdown();
|
2015-01-27 04:49:22 +03:00
|
|
|
EXPECT_EQ(Task::sCount, 100);
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
2015-05-09 02:32:27 +03:00
|
|
|
|
|
|
|
TEST(ThreadPool, Parallelism)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIThreadPool> pool = do_CreateInstance(NS_THREADPOOL_CONTRACTID);
|
|
|
|
EXPECT_TRUE(pool);
|
|
|
|
|
|
|
|
// Dispatch and sleep to ensure we have an idle thread
|
2017-06-12 22:34:10 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r0 = new Runnable("TestRunnable");
|
2015-05-09 02:32:27 +03:00
|
|
|
pool->Dispatch(r0, NS_DISPATCH_SYNC);
|
|
|
|
PR_Sleep(PR_SecondsToInterval(2));
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class Runnable1 : public Runnable {
|
2015-05-09 02:32:27 +03:00
|
|
|
public:
|
|
|
|
Runnable1(Monitor& aMonitor, bool& aDone)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("Runnable1")
|
|
|
|
, mMonitor(aMonitor)
|
|
|
|
, mDone(aDone)
|
|
|
|
{
|
|
|
|
}
|
2015-05-09 02:32:27 +03:00
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
if (!mDone) {
|
|
|
|
// Wait for a reasonable timeout since we don't want to block gtests
|
|
|
|
// forever should any regression happen.
|
|
|
|
mon.Wait(PR_SecondsToInterval(300));
|
|
|
|
}
|
|
|
|
EXPECT_TRUE(mDone);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
Monitor& mMonitor;
|
|
|
|
bool& mDone;
|
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class Runnable2 : public Runnable {
|
2015-05-09 02:32:27 +03:00
|
|
|
public:
|
|
|
|
Runnable2(Monitor& aMonitor, bool& aDone)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("Runnable2")
|
|
|
|
, mMonitor(aMonitor)
|
|
|
|
, mDone(aDone)
|
|
|
|
{
|
|
|
|
}
|
2015-05-09 02:32:27 +03:00
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
mDone = true;
|
|
|
|
mon.NotifyAll();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
Monitor& mMonitor;
|
|
|
|
bool& mDone;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Dispatch 2 events in a row. Since we are still within the thread limit,
|
|
|
|
// We should wake up the idle thread and spawn a new thread so these 2 events
|
|
|
|
// can run in parallel. We will time out if r1 and r2 run in sequence for r1
|
|
|
|
// won't finish until r2 finishes.
|
|
|
|
Monitor mon("ThreadPool::Parallelism");
|
|
|
|
bool done = false;
|
|
|
|
nsCOMPtr<nsIRunnable> r1 = new Runnable1(mon, done);
|
|
|
|
nsCOMPtr<nsIRunnable> r2 = new Runnable2(mon, done);
|
|
|
|
pool->Dispatch(r1, NS_DISPATCH_NORMAL);
|
|
|
|
pool->Dispatch(r2, NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
pool->Shutdown();
|
|
|
|
}
|