Bug 484921. Create TimeDuration/TimeStamp API that never rolls over and provides better typechecking than PR_IntervalTime. r=bsmedberg,cgj

This commit is contained in:
Robert O'Callahan 2009-04-06 11:49:48 +12:00
Родитель 0e8be7d125
Коммит 899449340e
6 изменённых файлов: 424 добавлений и 4 удалений

Просмотреть файл

@ -85,6 +85,7 @@
#include "nsTimerImpl.h"
#include "TimerThread.h"
#include "nsTimeStamp.h"
#include "nsThread.h"
#include "nsProcess.h"
@ -140,6 +141,10 @@ NS_DECL_CLASSINFO(nsStringInputStream)
#include <locale.h>
#include "nsXPCOM.h"
using mozilla::TimeStamp;
// Registry Factory creation function defined in nsRegistry.cpp
// We hook into this function locally to create and register the registry
// Since noone outside xpcom needs to know about this and nsRegistry.cpp
@ -161,7 +166,6 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsProcess)
#define NS_ENVIRONMENT_CLASSNAME "Environment Service"
#include "nsXPCOM.h"
// ds/nsISupportsPrimitives
#define NS_SUPPORTS_ID_CLASSNAME "Supports ID"
#define NS_SUPPORTS_CSTRING_CLASSNAME "Supports String"
@ -542,6 +546,10 @@ NS_InitXPCOM3(nsIServiceManager* *result,
NS_LogInit();
// Set up TimeStamp
rv = TimeStamp::Startup();
NS_ENSURE_SUCCESS(rv, rv);
// Establish the main thread here.
rv = nsThreadManager::get()->Init();
if (NS_FAILED(rv)) return rv;
@ -874,6 +882,8 @@ NS_ShutdownXPCOM(nsIServiceManager* servMgr)
NS_IF_RELEASE(gDebug);
TimeStamp::Shutdown();
NS_LogTerm();
#ifdef GC_LEAK_DETECTOR

Просмотреть файл

@ -53,12 +53,14 @@ REQUIRES = \
$(NULL)
CPPSRCS = \
nsArray.cpp \
nsAtomTable.cpp \
nsAtomService.cpp \
nsByteBuffer.cpp \
nsCheapSets.cpp \
nsCRT.cpp \
nsFixedSizeAllocator.cpp \
nsHashPropertyBag.cpp \
nsHashSets.cpp \
nsHashtable.cpp \
nsINIParserImpl.cpp \
@ -72,11 +74,10 @@ CPPSRCS = \
nsSupportsArray.cpp \
nsSupportsArrayEnumerator.cpp \
nsSupportsPrimitives.cpp \
nsTimelineService.cpp \
nsTimeStamp.cpp \
nsUnicharBuffer.cpp \
nsVariant.cpp \
nsTimelineService.cpp \
nsArray.cpp \
nsHashPropertyBag.cpp \
$(NULL)
EXPORTS = \
@ -100,6 +101,7 @@ EXPORTS = \
nsSupportsArray.h \
nsSupportsPrimitives.h \
nsTime.h \
nsTimeStamp.h \
nsVariant.h \
nsStringEnumerator.h \
nsHashPropertyBag.h \

84
xpcom/ds/nsTimeStamp.cpp Normal file
Просмотреть файл

@ -0,0 +1,84 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* ***** 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 code.
*
* The Initial Developer of the Original Code is the Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Robert O'Callahan <robert@ocallahan.org>
*
* Alternatively, the contents of this file may be used under the terms of
* either 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 ***** */
#include "nsTimeStamp.h"
#include "prlock.h"
namespace mozilla {
static PRLock* gTimeStampLock;
static PRUint32 gRolloverCount;
static PRIntervalTime gLastNow;
nsresult TimeStamp::Startup()
{
gTimeStampLock = PR_NewLock();
gRolloverCount = 1;
gLastNow = 0;
return gTimeStampLock ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
void TimeStamp::Shutdown()
{
if (gTimeStampLock) {
PR_DestroyLock(gTimeStampLock);
gTimeStampLock = nsnull;
}
}
TimeStamp TimeStamp::Now()
{
// XXX this could be considerably simpler and faster if we had
// 64-bit atomic operations
PR_Lock(gTimeStampLock);
PRIntervalTime now = PR_IntervalNow();
if (now < gLastNow) {
++gRolloverCount;
// This can't happen unless you've been running for millions of years
NS_ASSERTION(gRolloverCount > 0, "Rollover in rollover count???");
}
gLastNow = now;
TimeStamp result((PRUint64(gRolloverCount) << 32) + now);
PR_Unlock(gTimeStampLock);
return result;
}
}

201
xpcom/ds/nsTimeStamp.h Normal file
Просмотреть файл

@ -0,0 +1,201 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* ***** 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 code.
*
* The Initial Developer of the Original Code is the Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Robert O'Callahan <robert@ocallahan.org>
*
* Alternatively, the contents of this file may be used under the terms of
* either 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 ***** */
#ifndef nsTimeStamp_h_
#define nsTimeStamp_h_
#include "prinrval.h"
#include "nsDebug.h"
namespace mozilla {
class TimeStamp;
/**
* Instances of this class represent the length of an interval of time.
* Negative durations are allowed, meaning the end is before the start.
*
* Internally the duration is stored as a PRInt64 in units of
* PR_TicksPerSecond().
*
* This whole class is inline so we don't need any special linkage.
*/
class TimeDuration {
public:
// The default duration is 0.
TimeDuration() : mValue(0) {}
// Default copy-constructor and assignment are OK
double ToSeconds() const { return double(mValue)/PR_TicksPerSecond(); }
static TimeDuration FromSeconds(PRInt32 aSeconds) {
// No overflow is possible here
return TimeDuration(PRInt64(aSeconds)*PR_TicksPerSecond());
}
static TimeDuration FromMilliseconds(PRInt32 aMilliseconds) {
// No overflow is possible here
return TimeDuration(PRInt64(aMilliseconds)*PR_TicksPerSecond()/1000);
}
PRBool operator<(const TimeDuration& aOther) const {
return mValue < aOther.mValue;
}
PRBool operator<=(const TimeDuration& aOther) const {
return mValue <= aOther.mValue;
}
PRBool operator>=(const TimeDuration& aOther) const {
return mValue >= aOther.mValue;
}
PRBool operator>(const TimeDuration& aOther) const {
return mValue > aOther.mValue;
}
// We could define additional operators here:
// -- convert to/from other time units
// -- add/subtract durations
// -- scale duration by a float
// but let's do that on demand.
// Comparing durations for equality should be discouraged.
private:
friend class TimeStamp;
TimeDuration(PRInt64 aTicks) : mValue(aTicks) {}
// Duration in PRIntervalTime units
PRInt64 mValue;
};
/**
* Instances of this class represent moments in time, or a special "null"
* moment. We do not use the system clock or local time, since they can be
* reset, causing apparent backward travel in time, which can confuse
* algorithms. Instead we measure elapsed time according to the system.
* This time can never go backwards (i.e. it never wraps around, at least
* not in less than five million years of system elapsed time). It might
* not advance while the system is sleeping. If TimeStamp::SetNow() is not
* called at all for hours or days, we might not notice the passage
* of some of that time.
*
* We deliberately do not expose a way to convert TimeStamps to some
* particular unit. All you can do is compute a difference between two
* TimeStamps to get a TimeDuration. You can also add a TimeDuration
* to a TimeStamp to get a new TimeStamp. You can't do something
* meaningless like add two TimeStamps.
*
* Internally this is implemented as a wrapper around PRIntervalTime.
* We detect wraparounds of PRIntervalTime and work around them.
*/
class NS_COM TimeStamp {
public:
/**
* Initialize to the "null" moment
*/
TimeStamp() : mValue(0) {}
// Default copy-constructor and assignment are OK
/**
* Return true if this is the "null" moment
*/
PRBool IsNull() const { return mValue == 0; }
/**
* Return a timestamp reflecting the current elapsed system time. This
* is monotonically increasing (i.e., does not decrease) over the
* lifetime of this process' XPCOM session.
*/
static TimeStamp Now();
/**
* Compute the difference between two timestamps. Both must be non-null.
*/
TimeDuration operator-(const TimeStamp& aOther) const {
NS_ASSERTION(!IsNull(), "Cannot compute with a null value");
NS_ASSERTION(!aOther.IsNull(), "Cannot compute with aOther null value");
return TimeDuration(mValue - aOther.mValue);
}
PRBool operator<(const TimeStamp& aOther) const {
NS_ASSERTION(!IsNull(), "Cannot compute with a null value");
NS_ASSERTION(!aOther.IsNull(), "Cannot compute with aOther null value");
return mValue < aOther.mValue;
}
PRBool operator<=(const TimeStamp& aOther) const {
NS_ASSERTION(!IsNull(), "Cannot compute with a null value");
NS_ASSERTION(!aOther.IsNull(), "Cannot compute with aOther null value");
return mValue <= aOther.mValue;
}
PRBool operator>=(const TimeStamp& aOther) const {
NS_ASSERTION(!IsNull(), "Cannot compute with a null value");
NS_ASSERTION(!aOther.IsNull(), "Cannot compute with aOther null value");
return mValue >= aOther.mValue;
}
PRBool operator>(const TimeStamp& aOther) const {
NS_ASSERTION(!IsNull(), "Cannot compute with a null value");
NS_ASSERTION(!aOther.IsNull(), "Cannot compute with aOther null value");
return mValue > aOther.mValue;
}
// We could define additional operators here:
// -- add a TimeDuration to a TimeStamp to get a TimeStamp
// but let's do that on-demand.
// Comparing TimeStamps for equality should be discouraged. Adding
// two TimeStamps, or scaling TimeStamps, is nonsense and must never
// be allowed.
static NS_HIDDEN_(nsresult) Startup();
static NS_HIDDEN_(void) Shutdown();
private:
TimeStamp(PRUint64 aValue) : mValue(aValue) {}
/**
* A value of 0 means this instance is "null". Otherwise,
* the low 32 bits represent a PRIntervalTime, and the high 32 bits
* represent a counter of the number of rollovers of PRIntervalTime
* that we've seen. This counter starts at 1 to avoid a real time
* colliding with the "null" value.
*
* PR_INTERVAL_MAX is set at 100,000 ticks per second. So the minimum
* time to wrap around is about 2^64/100000 seconds, i.e. about
* 5,849,424 years.
*/
PRUint64 mValue;
};
}
#endif /* nsTimeStamp_h_ */

Просмотреть файл

@ -96,6 +96,7 @@ CPP_UNIT_TESTS = \
TestPipe.cpp \
TestServMgr.cpp \
TestTextFormatter.cpp \
TestTimeStamp.cpp \
$(NULL)
ifndef MOZ_ENABLE_LIBXUL

Просмотреть файл

@ -0,0 +1,122 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** 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 XPCOM TimeStamp tests.
*
* The Initial Developer of the Original Code is Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* robert@ocallahan.org
*
* Alternatively, the contents of this file may be used under the terms of
* either 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 ***** */
#include "nsTimeStamp.h"
#include "TestHarness.h"
#include "prinrval.h"
#include "prthread.h"
using mozilla::TimeStamp;
using mozilla::TimeDuration;
static void Assert(PRBool aCond, const char* aMsg)
{
if (aCond) {
passed(aMsg);
} else {
fail("%s", aMsg);
}
}
int main(int argc, char** argv)
{
ScopedXPCOM xpcom("nsTimeStamp");
if (xpcom.failed())
return 1;
TimeDuration td;
Assert(td.ToSeconds() == 0.0, "TimeDuration default value 0");
Assert(TimeDuration::FromSeconds(5).ToSeconds() == 5.0,
"TimeDuration FromSeconds/ToSeconds round-trip");
Assert(TimeDuration::FromMilliseconds(5000).ToSeconds() == 5.0,
"TimeDuration FromMilliseconds/ToSeconds round-trip");
Assert(TimeDuration::FromSeconds(1) < TimeDuration::FromSeconds(2),
"TimeDuration < operator works");
Assert(!(TimeDuration::FromSeconds(1) < TimeDuration::FromSeconds(1)),
"TimeDuration < operator works");
Assert(TimeDuration::FromSeconds(2) > TimeDuration::FromSeconds(1),
"TimeDuration > operator works");
Assert(!(TimeDuration::FromSeconds(1) > TimeDuration::FromSeconds(1)),
"TimeDuration > operator works");
Assert(TimeDuration::FromSeconds(1) <= TimeDuration::FromSeconds(2),
"TimeDuration <= operator works");
Assert(TimeDuration::FromSeconds(1) <= TimeDuration::FromSeconds(1),
"TimeDuration <= operator works");
Assert(!(TimeDuration::FromSeconds(2) <= TimeDuration::FromSeconds(1)),
"TimeDuration <= operator works");
Assert(TimeDuration::FromSeconds(2) >= TimeDuration::FromSeconds(1),
"TimeDuration >= operator works");
Assert(TimeDuration::FromSeconds(1) >= TimeDuration::FromSeconds(1),
"TimeDuration >= operator works");
Assert(!(TimeDuration::FromSeconds(1) >= TimeDuration::FromSeconds(2)),
"TimeDuration >= operator works");
TimeStamp ts;
Assert(ts.IsNull(), "Default TimeStamp value null");
ts = TimeStamp::Now();
Assert(!ts.IsNull(), "TimeStamp time value non-null");
Assert((ts - ts).ToSeconds() == 0.0, "TimeStamp zero-length duration");
PR_Sleep(PR_SecondsToInterval(2));
TimeStamp ts2(TimeStamp::Now());
Assert(ts2 > ts, "TimeStamp > comparison");
Assert(!(ts > ts), "TimeStamp > comparison");
Assert(ts < ts2, "TimeStamp < comparison");
Assert(!(ts < ts), "TimeStamp < comparison");
Assert(ts <= ts2, "TimeStamp <= comparison");
Assert(ts <= ts, "TimeStamp <= comparison");
Assert(!(ts2 <= ts), "TimeStamp <= comparison");
Assert(ts2 >= ts, "TimeStamp >= comparison");
Assert(ts2 >= ts, "TimeStamp >= comparison");
Assert(!(ts >= ts2), "TimeStamp >= comparison");
// We can't be sure exactly how long PR_Sleep slept for. It should have
// slept for at least one second. We might have slept a lot longer due
// to process scheduling, but hopefully not more than 10 seconds.
td = ts2 - ts;
Assert(td.ToSeconds() > 1.0, "TimeStamp difference lower bound");
Assert(td.ToSeconds() < 20.0, "TimeStamp difference upper bound");
td = ts - ts2;
Assert(td.ToSeconds() < -1.0, "TimeStamp negative difference lower bound");
Assert(td.ToSeconds() > -20.0, "TimeStamp negative difference upper bound");
return gFailCount > 0;
}