bug 490072: upgrade XPCOM unit tests to new thread sync API. drop nsAutoLock test. r=bsmedberg

This commit is contained in:
Chris Jones 2009-07-22 22:55:02 -05:00
parent 7888c9702c
commit e1f8c82efc
7 changed files with 53 additions and 136 deletions

View File

@ -102,7 +102,6 @@ CPP_UNIT_TESTS = \
ifndef MOZ_ENABLE_LIBXUL
CPP_UNIT_TESTS += \
TestArray.cpp \
TestAutoLock.cpp \
TestCRT.cpp \
TestDeque.cpp \
TestEncoding.cpp \

View File

@ -1,91 +0,0 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
Some tests for nsAutoLock.
*/
#include "nsAutoLock.h"
#include "prthread.h"
PRLock* gLock;
int gCount;
static void run(void* arg)
{
for (int i = 0; i < 1000000; ++i) {
nsAutoLock guard(gLock);
++gCount;
PR_ASSERT(gCount == 1);
--gCount;
}
}
int main(int argc, char** argv)
{
gLock = PR_NewLock();
gCount = 0;
// This shouldn't compile
//nsAutoLock* l1 = new nsAutoLock(theLock);
//delete l1;
// Create a block-scoped lock. This should compile.
{
nsAutoLock l2(gLock);
}
// Fork a thread to access the shared variable in a tight loop
PRThread* t1 =
PR_CreateThread(PR_SYSTEM_THREAD,
run,
nsnull,
PR_PRIORITY_NORMAL,
PR_GLOBAL_THREAD,
PR_JOINABLE_THREAD,
0);
// ...and now do the same thing ourselves
run(nsnull);
// Wait for the background thread to finish, if necessary.
PR_JoinThread(t1);
return 0;
}

View File

@ -45,8 +45,9 @@
#include "prinrval.h"
#include "nsCRT.h"
#include "nsIPipe.h" // new implementation
#include "nsAutoLock.h"
#include "mozilla/Monitor.h"
using namespace mozilla;
/** NS_NewPipe2 reimplemented, because it's not exported by XPCOM */
nsresult TP_NewPipe2(nsIAsyncInputStream** input,
@ -263,17 +264,17 @@ public:
}
nsShortReader(nsIInputStream* in) : mIn(in), mReceived(0) {
mMon = nsAutoMonitor::NewMonitor("nsShortReader");
mMon = new Monitor("nsShortReader");
}
void Received(PRUint32 count) {
nsAutoMonitor mon(mMon);
MonitorAutoEnter mon(*mMon);
mReceived += count;
mon.Notify();
}
PRUint32 WaitForReceipt(const PRUint32 aWriteCount) {
nsAutoMonitor mon(mMon);
MonitorAutoEnter mon(*mMon);
PRUint32 result = mReceived;
while (result < aWriteCount) {
@ -289,8 +290,8 @@ public:
protected:
nsCOMPtr<nsIInputStream> mIn;
PRUint32 mReceived;
PRMonitor* mMon;
PRUint32 mReceived;
Monitor* mMon;
};
NS_IMPL_THREADSAFE_ISUPPORTS1(nsShortReader, nsIRunnable)

View File

@ -43,7 +43,6 @@
#include "nsIThread.h"
#include "nsIThreadPool.h"
#include "nsAutoLock.h"
#include "nsAutoPtr.h"
#include "nsCOMPtr.h"
#include "nsComponentManagerUtils.h"
@ -52,6 +51,9 @@
#include "nsXPCOMCIDInternal.h"
#include "prlog.h"
#include "mozilla/Mutex.h"
using namespace mozilla;
typedef nsresult(*TestFuncPtr)();
#define TEST_NAME "TestProxies"
@ -213,7 +215,7 @@ private:
class IncrementingRunnable : public SimpleRunnable
{
public:
IncrementingRunnable(PRUint32* aCounter, PRLock* aLock = nsnull)
IncrementingRunnable(PRUint32* aCounter, Mutex* aLock = nsnull)
: SimpleRunnable("IncrementingRunnable"), mCounter(aCounter), mLock(aLock)
{ }
@ -223,19 +225,19 @@ public:
NS_ENSURE_SUCCESS(rv, rv);
if (mLock)
PR_Lock(mLock);
mLock->Lock();
(*mCounter)++;
if (mLock)
PR_Unlock(mLock);
mLock->Unlock();
return NS_OK;
}
private:
PRUint32* mCounter;
PRLock* mLock;
Mutex* mLock;
};
class NonThreadsafeRunnable : public nsIRunnable
@ -703,7 +705,7 @@ TestAsyncProxy()
// Now test async proxies to another thread.
PRLock* counterLock = nsAutoLock::NewLock("counterLock");
Mutex* counterLock = new Mutex("counterLock");
NS_ENSURE_TRUE(counterLock, NS_ERROR_OUT_OF_MEMORY);
counter = 0;
@ -724,11 +726,11 @@ TestAsyncProxy()
rv = NS_ProcessPendingEvents(gMainThread, PR_SecondsToInterval(1));
NS_ENSURE_SUCCESS(rv, rv);
nsAutoLock lock(counterLock);
MutexAutoLock lock(*counterLock);
safeCounter = counter;
}
nsAutoLock::DestroyLock(counterLock);
delete counterLock;
// Now test async proxies to another thread that create sync proxies to this
// thread.

View File

@ -41,12 +41,14 @@
#include "nsIThread.h"
#include "nsIComponentRegistrar.h"
#include "nsAutoLock.h"
#include "nsAutoPtr.h"
#include "nsThreadUtils.h"
#include "nsXPCOMCIDInternal.h"
#include "prmon.h"
#include "mozilla/Monitor.h"
using namespace mozilla;
#ifdef DEBUG
#define TEST_ASSERTION(_test, _msg) \
NS_ASSERTION(_test, _msg);
@ -85,7 +87,7 @@ NS_DEFINE_CID(kFactoryCID2, FACTORY_CID2);
PRInt32 gComponent1Count = 0;
PRInt32 gComponent2Count = 0;
PRMonitor* gMonitor = nsnull;
Monitor* gMonitor = nsnull;
PRBool gCreateInstanceCalled = PR_FALSE;
PRBool gMainThreadWaiting = PR_FALSE;
@ -93,22 +95,22 @@ PRBool gMainThreadWaiting = PR_FALSE;
class AutoCreateAndDestroyMonitor
{
public:
AutoCreateAndDestroyMonitor(PRMonitor** aMonitorPtr)
AutoCreateAndDestroyMonitor(Monitor** aMonitorPtr)
: mMonitorPtr(aMonitorPtr) {
*aMonitorPtr =
nsAutoMonitor::NewMonitor("TestRacingServiceManager::AutoMon");
new Monitor("TestRacingServiceManager::AutoMon");
TEST_ASSERTION(*aMonitorPtr, "Out of memory!");
}
~AutoCreateAndDestroyMonitor() {
if (*mMonitorPtr) {
nsAutoMonitor::DestroyMonitor(*mMonitorPtr);
delete *mMonitorPtr;
*mMonitorPtr = nsnull;
}
}
private:
PRMonitor** mMonitorPtr;
Monitor** mMonitorPtr;
};
class Factory : public nsIFactory
@ -181,7 +183,7 @@ Factory::CreateInstance(nsISupports* aDelegate,
TEST_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
{
nsAutoMonitor mon(gMonitor);
MonitorAutoEnter mon(*gMonitor);
gCreateInstanceCalled = PR_TRUE;
mon.Notify();
@ -222,7 +224,7 @@ NS_IMETHODIMP
Runnable::Run()
{
{
nsAutoMonitor mon(gMonitor);
MonitorAutoEnter mon(*gMonitor);
while (!gMainThreadWaiting) {
mon.Wait();
@ -273,7 +275,7 @@ int main(int argc, char** argv)
NS_ENSURE_SUCCESS(rv, 1);
{
nsAutoMonitor mon(gMonitor);
MonitorAutoEnter mon(*gMonitor);
gMainThreadWaiting = PR_TRUE;
mon.Notify();
@ -295,7 +297,7 @@ int main(int argc, char** argv)
NS_ENSURE_SUCCESS(rv, 1);
{
nsAutoMonitor mon(gMonitor);
MonitorAutoEnter mon(*gMonitor);
gMainThreadWaiting = PR_TRUE;
mon.Notify();

View File

@ -41,7 +41,6 @@
#include "nsIThread.h"
#include "nsIThreadPool.h"
#include "nsAutoLock.h"
#include "nsThreadUtils.h"
#include "nsXPCOMCIDInternal.h"
#include "pratom.h"
@ -49,6 +48,9 @@
#include "prmon.h"
#include "prthread.h"
#include "mozilla/Monitor.h"
using namespace mozilla;
#define NUMBER_OF_THREADS 4
// One hour... because test boxes can be slow!
@ -57,7 +59,7 @@
static nsIThread** gCreatedThreadList = nsnull;
static nsIThread** gShutDownThreadList = nsnull;
static PRMonitor* gMonitor = nsnull;
static Monitor* gMonitor = nsnull;
static PRBool gAllRunnablesPosted = PR_FALSE;
static PRBool gAllThreadsCreated = PR_FALSE;
@ -90,7 +92,7 @@ Listener::OnThreadCreated()
nsCOMPtr<nsIThread> current(do_GetCurrentThread());
TEST_ASSERTION(current, "Couldn't get current thread!");
nsAutoMonitor mon(gMonitor);
MonitorAutoEnter mon(*gMonitor);
while (!gAllRunnablesPosted) {
mon.Wait();
@ -120,7 +122,7 @@ Listener::OnThreadShuttingDown()
nsCOMPtr<nsIThread> current(do_GetCurrentThread());
TEST_ASSERTION(current, "Couldn't get current thread!");
nsAutoMonitor mon(gMonitor);
MonitorAutoEnter mon(*gMonitor);
for (PRUint32 i = 0; i < NUMBER_OF_THREADS; i++) {
nsIThread* thread = gShutDownThreadList[i];
@ -143,21 +145,21 @@ Listener::OnThreadShuttingDown()
class AutoCreateAndDestroyMonitor
{
public:
AutoCreateAndDestroyMonitor(PRMonitor** aMonitorPtr)
AutoCreateAndDestroyMonitor(Monitor** aMonitorPtr)
: mMonitorPtr(aMonitorPtr) {
*aMonitorPtr = nsAutoMonitor::NewMonitor("TestThreadPoolListener::AutoMon");
*aMonitorPtr = new Monitor("TestThreadPoolListener::AutoMon");
TEST_ASSERTION(*aMonitorPtr, "Out of memory!");
}
~AutoCreateAndDestroyMonitor() {
if (*mMonitorPtr) {
nsAutoMonitor::DestroyMonitor(*mMonitorPtr);
delete *mMonitorPtr;
*mMonitorPtr = nsnull;
}
}
private:
PRMonitor** mMonitorPtr;
Monitor** mMonitorPtr;
};
int main(int argc, char** argv)
@ -203,7 +205,7 @@ int main(int argc, char** argv)
NS_ENSURE_SUCCESS(rv, 1);
{
nsAutoMonitor mon(gMonitor);
MonitorAutoEnter mon(*gMonitor);
for (PRUint32 i = 0; i < NUMBER_OF_THREADS; i++) {
nsCOMPtr<nsIRunnable> runnable = new nsRunnable();
@ -218,7 +220,7 @@ int main(int argc, char** argv)
}
{
nsAutoMonitor mon(gMonitor);
MonitorAutoEnter mon(*gMonitor);
while (!gAllThreadsCreated) {
mon.Wait();
}
@ -228,7 +230,7 @@ int main(int argc, char** argv)
NS_ENSURE_SUCCESS(rv, 1);
{
nsAutoMonitor mon(gMonitor);
MonitorAutoEnter mon(*gMonitor);
while (!gAllThreadsShutDown) {
mon.Wait();
}

View File

@ -40,7 +40,6 @@
#include "nsIThread.h"
#include "nsITimer.h"
#include "nsAutoLock.h"
#include "nsCOMPtr.h"
#include "nsComponentManagerUtils.h"
#include "nsServiceManagerUtils.h"
@ -48,6 +47,9 @@
#include "prinrval.h"
#include "prmon.h"
#include "mozilla/Monitor.h"
using namespace mozilla;
typedef nsresult(*TestFuncPtr)();
class AutoTestThread
@ -81,22 +83,22 @@ class AutoCreateAndDestroyMonitor
{
public:
AutoCreateAndDestroyMonitor() {
mMonitor = nsAutoMonitor::NewMonitor("TestTimers::AutoMon");
mMonitor = new Monitor("TestTimers::AutoMon");
NS_ASSERTION(mMonitor, "Out of memory!");
}
~AutoCreateAndDestroyMonitor() {
if (mMonitor) {
nsAutoMonitor::DestroyMonitor(mMonitor);
delete mMonitor;
}
}
operator PRMonitor*() {
operator Monitor* () {
return mMonitor;
}
private:
PRMonitor* mMonitor;
Monitor* mMonitor;
};
class TimerCallback : public nsITimerCallback
@ -104,13 +106,13 @@ class TimerCallback : public nsITimerCallback
public:
NS_DECL_ISUPPORTS
TimerCallback(nsIThread** aThreadPtr, PRMonitor* aMonitor)
TimerCallback(nsIThread** aThreadPtr, Monitor* aMonitor)
: mThreadPtr(aThreadPtr), mMonitor(aMonitor) { }
NS_IMETHOD Notify(nsITimer* aTimer) {
nsCOMPtr<nsIThread> current(do_GetCurrentThread());
nsAutoMonitor mon(mMonitor);
MonitorAutoEnter mon(*mMonitor);
NS_ASSERTION(!*mThreadPtr, "Timer called back more than once!");
*mThreadPtr = current;
@ -121,7 +123,7 @@ public:
}
private:
nsIThread** mThreadPtr;
PRMonitor* mMonitor;
Monitor* mMonitor;
};
NS_IMPL_THREADSAFE_ISUPPORTS1(TimerCallback, nsITimerCallback)
@ -154,7 +156,7 @@ TestTargetedTimers()
nsITimer::TYPE_ONE_SHOT);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoMonitor mon(newMon);
MonitorAutoEnter mon(*newMon);
while (!notifiedThread) {
mon.Wait();
}