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/. */
|
1999-04-06 01:03:22 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsThreadUtils.h"
|
1999-04-06 01:03:22 +04:00
|
|
|
#include <stdio.h>
|
2000-11-30 08:26:15 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "nspr.h"
|
2000-03-06 00:26:01 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2005-11-08 21:17:49 +03:00
|
|
|
#include "nsXPCOM.h"
|
2015-09-15 04:24:43 +03:00
|
|
|
#include "mozilla/Monitor.h"
|
2015-03-09 18:10:32 +03:00
|
|
|
#include "gtest/gtest.h"
|
1999-04-06 01:03:22 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsRunner final : public nsIRunnable {
|
2015-03-09 18:10:32 +03:00
|
|
|
~nsRunner() {}
|
1999-04-06 01:03:22 +04:00
|
|
|
public:
|
2013-07-19 06:31:26 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
1999-04-06 01:03:22 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2000-03-06 00:26:01 +03:00
|
|
|
nsCOMPtr<nsIThread> thread;
|
2006-05-10 21:30:15 +04:00
|
|
|
nsresult rv = NS_GetCurrentThread(getter_AddRefs(thread));
|
2015-03-09 18:10:32 +03:00
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
2001-03-21 08:32:22 +03:00
|
|
|
printf("running %d on thread %p\n", mNum, (void *)thread.get());
|
1999-04-06 01:03:22 +04:00
|
|
|
|
|
|
|
// if we don't do something slow, we'll never see the other
|
|
|
|
// worker threads run
|
2000-04-21 09:12:57 +04:00
|
|
|
PR_Sleep(PR_MillisecondsToInterval(100));
|
1999-04-06 01:03:22 +04:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-03-09 18:10:32 +03:00
|
|
|
explicit nsRunner(int num) : mNum(num) {
|
1999-04-06 01:03:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int mNum;
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsRunner, nsIRunnable)
|
1999-04-06 01:03:22 +04:00
|
|
|
|
2015-03-09 18:10:32 +03:00
|
|
|
TEST(Threads, Main)
|
1999-04-06 01:03:22 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event = new nsRunner(0);
|
2015-03-09 18:10:32 +03:00
|
|
|
EXPECT_TRUE(event);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2000-03-06 00:26:01 +03:00
|
|
|
nsCOMPtr<nsIThread> runner;
|
2006-05-10 21:30:15 +04:00
|
|
|
rv = NS_NewThread(getter_AddRefs(runner), event);
|
2015-03-09 18:10:32 +03:00
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
1999-04-06 01:03:22 +04:00
|
|
|
|
2000-03-06 00:26:01 +03:00
|
|
|
nsCOMPtr<nsIThread> thread;
|
2006-05-10 21:30:15 +04:00
|
|
|
rv = NS_GetCurrentThread(getter_AddRefs(thread));
|
2015-03-09 18:10:32 +03:00
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
1999-04-06 01:03:22 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
rv = runner->Shutdown(); // wait for the runner to die before quitting
|
2015-03-09 18:10:32 +03:00
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
1999-04-06 01:03:22 +04:00
|
|
|
|
2000-04-21 09:12:57 +04:00
|
|
|
PR_Sleep(PR_MillisecondsToInterval(100)); // hopefully the runner will quit here
|
1999-04-06 01:03:22 +04:00
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsStressRunner final : public nsIRunnable {
|
2000-11-30 08:26:15 +03:00
|
|
|
public:
|
2013-07-19 06:31:26 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2000-11-30 08:26:15 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2015-03-09 18:10:32 +03:00
|
|
|
EXPECT_FALSE(mWasRun);
|
2011-10-17 18:59:28 +04:00
|
|
|
mWasRun = true;
|
2000-11-30 08:26:15 +03:00
|
|
|
PR_Sleep(1);
|
|
|
|
if (!PR_AtomicDecrement(&gNum)) {
|
|
|
|
printf(" last thread was %d\n", mNum);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-09 18:10:32 +03:00
|
|
|
explicit nsStressRunner(int num) : mNum(num), mWasRun(false) {
|
2000-11-30 08:26:15 +03:00
|
|
|
PR_AtomicIncrement(&gNum);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t GetGlobalCount() {return gNum;}
|
2004-01-15 09:14:18 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
~nsStressRunner() {
|
2015-03-09 18:10:32 +03:00
|
|
|
EXPECT_TRUE(mWasRun);
|
2000-11-30 08:26:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t gNum;
|
|
|
|
int32_t mNum;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mWasRun;
|
2000-11-30 08:26:15 +03:00
|
|
|
};
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t nsStressRunner::gNum = 0;
|
2000-11-30 08:26:15 +03:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsStressRunner, nsIRunnable)
|
2000-11-30 08:26:15 +03:00
|
|
|
|
2015-03-09 18:10:32 +03:00
|
|
|
TEST(Threads, Stress)
|
2000-11-30 08:26:15 +03:00
|
|
|
{
|
2015-03-09 18:10:32 +03:00
|
|
|
const int loops = 1000;
|
|
|
|
const int threads = 50;
|
2000-11-30 08:26:15 +03:00
|
|
|
|
|
|
|
for (int i = 0; i < loops; i++) {
|
|
|
|
printf("Loop %d of %d\n", i+1, loops);
|
|
|
|
|
|
|
|
int k;
|
|
|
|
nsIThread** array = new nsIThread*[threads];
|
|
|
|
|
2015-03-09 18:10:32 +03:00
|
|
|
EXPECT_EQ(nsStressRunner::GetGlobalCount(), 0);
|
|
|
|
|
2000-11-30 08:26:15 +03:00
|
|
|
for (k = 0; k < threads; k++) {
|
|
|
|
nsCOMPtr<nsIThread> t;
|
2006-05-10 21:30:15 +04:00
|
|
|
nsresult rv = NS_NewThread(getter_AddRefs(t), new nsStressRunner(k));
|
2015-03-09 18:10:32 +03:00
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
2000-11-30 08:26:15 +03:00
|
|
|
NS_ADDREF(array[k] = t);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (k = threads-1; k >= 0; k--) {
|
2006-05-10 21:30:15 +04:00
|
|
|
array[k]->Shutdown();
|
2000-11-30 08:26:15 +03:00
|
|
|
NS_RELEASE(array[k]);
|
|
|
|
}
|
|
|
|
delete [] array;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 04:24:43 +03:00
|
|
|
mozilla::Monitor* gAsyncShutdownReadyMonitor;
|
|
|
|
mozilla::Monitor* gBeginAsyncShutdownMonitor;
|
|
|
|
|
|
|
|
class AsyncShutdownPreparer : public nsIRunnable {
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
EXPECT_FALSE(mWasRun);
|
|
|
|
mWasRun = true;
|
|
|
|
|
|
|
|
mozilla::MonitorAutoLock lock(*gAsyncShutdownReadyMonitor);
|
|
|
|
lock.Notify();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit AsyncShutdownPreparer() : mWasRun(false) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~AsyncShutdownPreparer() {
|
|
|
|
EXPECT_TRUE(mWasRun);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool mWasRun;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(AsyncShutdownPreparer, nsIRunnable)
|
|
|
|
|
|
|
|
class AsyncShutdownWaiter : public nsIRunnable {
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
EXPECT_FALSE(mWasRun);
|
|
|
|
mWasRun = true;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> t;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
{
|
|
|
|
mozilla::MonitorAutoLock lock(*gBeginAsyncShutdownMonitor);
|
|
|
|
|
|
|
|
rv = NS_NewThread(getter_AddRefs(t), new AsyncShutdownPreparer());
|
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
lock.Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = t->AsyncShutdown();
|
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit AsyncShutdownWaiter() : mWasRun(false) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~AsyncShutdownWaiter() {
|
|
|
|
EXPECT_TRUE(mWasRun);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool mWasRun;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(AsyncShutdownWaiter, nsIRunnable)
|
|
|
|
|
|
|
|
class SameThreadSentinel : public nsIRunnable {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
mozilla::MonitorAutoLock lock(*gBeginAsyncShutdownMonitor);
|
|
|
|
lock.Notify();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~SameThreadSentinel() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(SameThreadSentinel, nsIRunnable)
|
|
|
|
|
|
|
|
TEST(Threads, AsyncShutdown)
|
|
|
|
{
|
|
|
|
gAsyncShutdownReadyMonitor = new mozilla::Monitor("gAsyncShutdownReady");
|
|
|
|
gBeginAsyncShutdownMonitor = new mozilla::Monitor("gBeginAsyncShutdown");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> t;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
{
|
|
|
|
mozilla::MonitorAutoLock lock(*gAsyncShutdownReadyMonitor);
|
|
|
|
|
|
|
|
rv = NS_NewThread(getter_AddRefs(t), new AsyncShutdownWaiter());
|
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
lock.Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DispatchToCurrentThread(new SameThreadSentinel());
|
|
|
|
rv = t->Shutdown();
|
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
delete gAsyncShutdownReadyMonitor;
|
|
|
|
delete gBeginAsyncShutdownMonitor;
|
|
|
|
}
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
static void threadProc(void *arg)
|
2000-11-30 08:26:15 +03:00
|
|
|
{
|
|
|
|
// printf(" running thread %d\n", (int) arg);
|
|
|
|
PR_Sleep(1);
|
2015-03-09 18:10:32 +03:00
|
|
|
EXPECT_EQ(PR_JOINABLE_THREAD, PR_GetThreadState(PR_GetCurrentThread()));
|
2000-11-30 08:26:15 +03:00
|
|
|
}
|
|
|
|
|
2015-03-09 18:10:32 +03:00
|
|
|
TEST(Threads, StressNSPR)
|
2000-11-30 08:26:15 +03:00
|
|
|
{
|
2015-03-09 18:10:32 +03:00
|
|
|
const int loops = 1000;
|
|
|
|
const int threads = 50;
|
2000-11-30 08:26:15 +03:00
|
|
|
|
|
|
|
for (int i = 0; i < loops; i++) {
|
|
|
|
printf("Loop %d of %d\n", i+1, loops);
|
|
|
|
|
2015-03-09 18:10:32 +03:00
|
|
|
intptr_t k;
|
2000-11-30 08:26:15 +03:00
|
|
|
PRThread** array = new PRThread*[threads];
|
|
|
|
|
|
|
|
for (k = 0; k < threads; k++) {
|
|
|
|
array[k] = PR_CreateThread(PR_USER_THREAD,
|
|
|
|
threadProc, (void*) k,
|
|
|
|
PR_PRIORITY_NORMAL,
|
|
|
|
PR_GLOBAL_THREAD,
|
|
|
|
PR_JOINABLE_THREAD,
|
|
|
|
0);
|
2015-03-09 18:10:32 +03:00
|
|
|
EXPECT_TRUE(array[k]);
|
|
|
|
}
|
2000-11-30 08:26:15 +03:00
|
|
|
|
|
|
|
for (k = 0; k < threads; k++) {
|
2015-03-09 18:10:32 +03:00
|
|
|
EXPECT_EQ(PR_JOINABLE_THREAD, PR_GetThreadState(array[k]));
|
|
|
|
}
|
2000-11-30 08:26:15 +03:00
|
|
|
|
|
|
|
for (k = threads-1; k >= 0; k--) {
|
|
|
|
PR_JoinThread(array[k]);
|
|
|
|
}
|
|
|
|
delete [] array;
|
|
|
|
}
|
1999-04-06 01:03:22 +04:00
|
|
|
}
|