2008-07-24 17:20:33 +00:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00: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/. */
|
2008-07-24 17:20:33 +00:00
|
|
|
|
|
|
|
#include "TestHarness.h"
|
|
|
|
|
|
|
|
#include "nsIThread.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "prinrval.h"
|
|
|
|
#include "prmon.h"
|
2012-06-20 03:41:56 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2008-07-24 17:20:33 +00:00
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2009-07-23 03:55:02 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2008-07-24 17:20:33 +00:00
|
|
|
typedef nsresult(*TestFuncPtr)();
|
|
|
|
|
|
|
|
class AutoTestThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoTestThread() {
|
|
|
|
nsCOMPtr<nsIThread> newThread;
|
|
|
|
nsresult rv = NS_NewThread(getter_AddRefs(newThread));
|
2010-02-22 17:41:21 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2008-07-24 17:20:33 +00:00
|
|
|
|
|
|
|
newThread.swap(mThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoTestThread() {
|
|
|
|
mThread->Shutdown();
|
|
|
|
}
|
|
|
|
|
2008-08-11 15:05:58 +00:00
|
|
|
operator nsIThread*() const {
|
2008-07-24 17:20:33 +00:00
|
|
|
return mThread;
|
|
|
|
}
|
|
|
|
|
2008-08-11 15:05:58 +00:00
|
|
|
nsIThread* operator->() const {
|
2008-07-24 17:20:33 +00:00
|
|
|
return mThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
|
|
|
};
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
class AutoCreateAndDestroyReentrantMonitor
|
2008-07-24 17:20:33 +00:00
|
|
|
{
|
|
|
|
public:
|
2011-04-29 19:21:57 +00:00
|
|
|
AutoCreateAndDestroyReentrantMonitor() {
|
|
|
|
mReentrantMonitor = new ReentrantMonitor("TestTimers::AutoMon");
|
|
|
|
NS_ASSERTION(mReentrantMonitor, "Out of memory!");
|
2008-07-24 17:20:33 +00:00
|
|
|
}
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
~AutoCreateAndDestroyReentrantMonitor() {
|
2011-05-17 14:01:36 +00:00
|
|
|
delete mReentrantMonitor;
|
2008-07-24 17:20:33 +00:00
|
|
|
}
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
operator ReentrantMonitor* () {
|
|
|
|
return mReentrantMonitor;
|
2008-07-24 17:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitor* mReentrantMonitor;
|
2008-07-24 17:20:33 +00:00
|
|
|
};
|
|
|
|
|
2012-06-20 03:41:56 +00:00
|
|
|
class TimerCallback MOZ_FINAL : public nsITimerCallback
|
2008-07-24 17:20:33 +00:00
|
|
|
{
|
|
|
|
public:
|
2013-07-19 02:31:26 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2008-07-24 17:20:33 +00:00
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
TimerCallback(nsIThread** aThreadPtr, ReentrantMonitor* aReentrantMonitor)
|
|
|
|
: mThreadPtr(aThreadPtr), mReentrantMonitor(aReentrantMonitor) { }
|
2008-07-24 17:20:33 +00:00
|
|
|
|
|
|
|
NS_IMETHOD Notify(nsITimer* aTimer) {
|
|
|
|
nsCOMPtr<nsIThread> current(do_GetCurrentThread());
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(*mReentrantMonitor);
|
2008-07-24 17:20:33 +00:00
|
|
|
|
|
|
|
NS_ASSERTION(!*mThreadPtr, "Timer called back more than once!");
|
|
|
|
*mThreadPtr = current;
|
|
|
|
|
|
|
|
mon.Notify();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsIThread** mThreadPtr;
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitor* mReentrantMonitor;
|
2008-07-24 17:20:33 +00:00
|
|
|
};
|
|
|
|
|
2013-07-19 02:31:26 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(TimerCallback, nsITimerCallback)
|
2008-07-24 17:20:33 +00:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
TestTargetedTimers()
|
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
AutoCreateAndDestroyReentrantMonitor newMon;
|
2008-07-24 17:20:33 +00:00
|
|
|
NS_ENSURE_TRUE(newMon, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
AutoTestThread testThread;
|
|
|
|
NS_ENSURE_TRUE(testThread, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsIEventTarget* target = static_cast<nsIEventTarget*>(testThread);
|
|
|
|
|
|
|
|
rv = timer->SetTarget(target);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
nsIThread* notifiedThread = nullptr;
|
2008-07-24 17:20:33 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsITimerCallback> callback =
|
|
|
|
new TimerCallback(¬ifiedThread, newMon);
|
|
|
|
NS_ENSURE_TRUE(callback, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
rv = timer->InitWithCallback(callback, PR_MillisecondsToInterval(2000),
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(*newMon);
|
2008-07-24 17:20:33 +00:00
|
|
|
while (!notifiedThread) {
|
|
|
|
mon.Wait();
|
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(notifiedThread == testThread, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
ScopedXPCOM xpcom("TestTimers");
|
|
|
|
NS_ENSURE_FALSE(xpcom.failed(), 1);
|
|
|
|
|
|
|
|
static TestFuncPtr testsToRun[] = {
|
|
|
|
TestTargetedTimers
|
|
|
|
};
|
2012-08-22 15:56:38 +00:00
|
|
|
static uint32_t testCount = sizeof(testsToRun) / sizeof(testsToRun[0]);
|
2008-07-24 17:20:33 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < testCount; i++) {
|
2008-07-24 17:20:33 +00:00
|
|
|
nsresult rv = testsToRun[i]();
|
|
|
|
NS_ENSURE_SUCCESS(rv, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|