Bug 842550 - Switch to using TimeTicks rather than Time in message loops. rs=cjones, a=tef+

The ipc/chromium/src/base/ changes here (except those mentioned below)
are the majority of the base/ changes (excluding those that patch code
that does not exist yet in our copy) in:
> From: jar@chromium.org <jar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>
> Date: Sat, 6 Nov 2010 22:23:29 +0000 (+0000)
> Subject: Switch to using TimeTicks rather than Time in message loops
> X-Git-Url: http://git.chromium.org/gitweb/?p=chromium.git;a=commitdiff_plain;h=f592c218c18bd1f8308489aaef2e329244ced330
>
> Switch to using TimeTicks rather than Time in message loops
>
> Switch to using TimeTicks rather than Time so that we
> are not dependent on changes in the system clock.
>
> r=mbelshe,darin
> Review URL: http://codereview.chromium.org/3884001
>
> git-svn-id: svn://svn.chromium.org/chrome/trunk/src@65322 0039d316-1c4b-4281-b951-d872f2087c98

The ipc/glue changes, and the message_pump_android.* and
message_pump_qt.* changes in ipc/chromium/src/base/, change signatures
to match.
This commit is contained in:
jar@chromium.org, L. David Baron 2013-02-21 18:10:59 -08:00
Родитель 13325b4413
Коммит 8dd61233f7
19 изменённых файлов: 63 добавлений и 53 удалений

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

@ -35,6 +35,7 @@
using base::Time;
using base::TimeDelta;
using base::TimeTicks;
// A lazily created thread local storage for quick access to a thread's message
// loop, if one exists. This should be safe and free of static constructors.
@ -281,7 +282,7 @@ void MessageLoop::PostTask_Helper(
if (delay_ms > 0) {
pending_task.delayed_run_time =
Time::Now() + TimeDelta::FromMilliseconds(delay_ms);
TimeTicks::Now() + TimeDelta::FromMilliseconds(delay_ms);
} else {
DCHECK(delay_ms == 0) << "delay should not be negative";
}
@ -448,13 +449,13 @@ bool MessageLoop::DoWork() {
return false;
}
bool MessageLoop::DoDelayedWork(Time* next_delayed_work_time) {
bool MessageLoop::DoDelayedWork(TimeTicks* next_delayed_work_time) {
if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) {
*next_delayed_work_time = Time();
*next_delayed_work_time = TimeTicks();
return false;
}
if (delayed_work_queue_.top().delayed_run_time > Time::Now()) {
if (delayed_work_queue_.top().delayed_run_time > TimeTicks::Now()) {
*next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
return false;
}

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

@ -292,10 +292,10 @@ public:
// This structure is copied around by value.
struct PendingTask {
Task* task; // The task to run.
base::Time delayed_run_time; // The time when the task should be run.
int sequence_num; // Used to facilitate sorting by run time.
bool nestable; // True if OK to dispatch from a nested loop.
Task* task; // The task to run.
base::TimeTicks delayed_run_time; // The time when the task should be run.
int sequence_num; // Secondary sort key for run time.
bool nestable; // OK to dispatch from a nested loop.
PendingTask(Task* task, bool nestable)
: task(task), sequence_num(0), nestable(nestable) {
@ -370,7 +370,7 @@ public:
// base::MessagePump::Delegate methods:
virtual bool DoWork();
virtual bool DoDelayedWork(base::Time* next_delayed_work_time);
virtual bool DoDelayedWork(base::TimeTicks* next_delayed_work_time);
virtual bool DoIdleWork();
Type type_;

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

@ -9,7 +9,7 @@
namespace base {
class Time;
class TimeTicks;
class MessagePump : public RefCountedThreadSafe<MessagePump> {
public:
@ -32,7 +32,7 @@ class MessagePump : public RefCountedThreadSafe<MessagePump> {
// |next_delayed_work_time| is null (per Time::is_null), then the queue of
// future delayed work (timer events) is currently empty, and no additional
// calls to this function need to be scheduled.
virtual bool DoDelayedWork(Time* next_delayed_work_time) = 0;
virtual bool DoDelayedWork(TimeTicks* next_delayed_work_time) = 0;
// Called from within Run just before the message pump goes to sleep.
// Returns true to indicate that idle work was done.
@ -122,7 +122,7 @@ class MessagePump : public RefCountedThreadSafe<MessagePump> {
// Schedule a DoDelayedWork callback to happen at the specified time,
// cancelling any pending DoDelayedWork callback. This method may only be
// used on the thread that called Run.
virtual void ScheduleDelayedWork(const Time& delayed_work_time) = 0;
virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time) = 0;
};
} // namespace base

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

@ -114,7 +114,7 @@ void MessagePumpForUI::ScheduleWork() {
mozilla::NotifyEvent();
}
void MessagePumpForUI::ScheduleDelayedWork(const Time& delayed_work_time) {
void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) {
// We need to wake up the loop in case the poll timeout needs to be
// adjusted. This will cause us to try to do work, but that's ok.
delayed_work_time_ = delayed_work_time;

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

@ -33,7 +33,7 @@ class MessagePumpForUI : public MessagePump {
virtual void Run(Delegate* delegate);
virtual void Quit();
virtual void ScheduleWork();
virtual void ScheduleDelayedWork(const Time& delayed_work_time);
virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time);
// Internal methods used for processing the pump callbacks. They are
// public for simplicity but should not be used directly.
@ -60,7 +60,7 @@ class MessagePumpForUI : public MessagePump {
RunState* state_;
// This is the time when we need to do delayed work.
Time delayed_work_time_;
TimeTicks delayed_work_time_;
bool work_scheduled;

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

@ -41,13 +41,13 @@ void MessagePumpDefault::Run(Delegate* delegate) {
if (delayed_work_time_.is_null()) {
event_.Wait();
} else {
TimeDelta delay = delayed_work_time_ - Time::Now();
TimeDelta delay = delayed_work_time_ - TimeTicks::Now();
if (delay > TimeDelta()) {
event_.TimedWait(delay);
} else {
// It looks like delayed_work_time_ indicates a time in the past, so we
// need to call DoDelayedWork now.
delayed_work_time_ = Time();
delayed_work_time_ = TimeTicks();
}
}
// Since event_ is auto-reset, we don't need to do anything special here
@ -67,7 +67,8 @@ void MessagePumpDefault::ScheduleWork() {
event_.Signal();
}
void MessagePumpDefault::ScheduleDelayedWork(const Time& delayed_work_time) {
void MessagePumpDefault::ScheduleDelayedWork(
const TimeTicks& delayed_work_time) {
// We know that we can't be blocked on Wait right now since this method can
// only be called on the same thread as Run, so we only need to update our
// record of how long to sleep when we do sleep.

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

@ -20,7 +20,7 @@ class MessagePumpDefault : public MessagePump {
virtual void Run(Delegate* delegate);
virtual void Quit();
virtual void ScheduleWork();
virtual void ScheduleDelayedWork(const Time& delayed_work_time);
virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time);
protected:
// This flag is set to false when Run should return.
@ -30,7 +30,7 @@ class MessagePumpDefault : public MessagePump {
WaitableEvent event_;
// The time at which we should call DoDelayedWork.
Time delayed_work_time_;
TimeTicks delayed_work_time_;
private:
DISALLOW_COPY_AND_ASSIGN(MessagePumpDefault);

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

@ -21,7 +21,7 @@ const char kWorkScheduled = '\0';
// Return a timeout suitable for the glib loop, -1 to block forever,
// 0 to return right away, or a timeout in milliseconds from now.
int GetTimeIntervalMilliseconds(base::Time from) {
int GetTimeIntervalMilliseconds(const base::TimeTicks& from) {
if (from.is_null())
return -1;
@ -29,7 +29,7 @@ int GetTimeIntervalMilliseconds(base::Time from) {
// value in milliseconds. If there are 5.5ms left, should the delay be 5 or
// 6? It should be 6 to avoid executing delayed work too early.
int delay = static_cast<int>(
ceil((from - base::Time::Now()).InMillisecondsF()));
ceil((from - base::TimeTicks::Now()).InMillisecondsF()));
// If this value is negative, then we need to run delayed work soon.
return delay < 0 ? 0 : delay;
@ -309,7 +309,7 @@ void MessagePumpForUI::ScheduleWork() {
}
}
void MessagePumpForUI::ScheduleDelayedWork(const Time& delayed_work_time) {
void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) {
// We need to wake up the loop in case the poll timeout needs to be
// adjusted. This will cause us to try to do work, but that's ok.
delayed_work_time_ = delayed_work_time;

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

@ -59,7 +59,7 @@ class MessagePumpForUI : public MessagePump {
virtual void Run(Delegate* delegate) { RunWithDispatcher(delegate, NULL); }
virtual void Quit();
virtual void ScheduleWork();
virtual void ScheduleDelayedWork(const Time& delayed_work_time);
virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time);
// Internal methods used for processing the pump callbacks. They are
// public for simplicity but should not be used directly. HandlePrepare
@ -116,7 +116,7 @@ class MessagePumpForUI : public MessagePump {
GMainContext* context_;
// This is the time when we need to do delayed work.
Time delayed_work_time_;
TimeTicks delayed_work_time_;
// The work source. It is shared by all calls to Run and destroyed when
// the message pump is destroyed.

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

@ -330,7 +330,7 @@ void MessagePumpLibevent::Run(Delegate* delegate) {
if (delayed_work_time_.is_null()) {
event_base_loop(event_base_, EVLOOP_ONCE);
} else {
TimeDelta delay = delayed_work_time_ - Time::Now();
TimeDelta delay = delayed_work_time_ - TimeTicks::Now();
if (delay > TimeDelta()) {
struct timeval poll_tv;
poll_tv.tv_sec = delay.InSeconds();
@ -340,7 +340,7 @@ void MessagePumpLibevent::Run(Delegate* delegate) {
} else {
// It looks like delayed_work_time_ indicates a time in the past, so we
// need to call DoDelayedWork now.
delayed_work_time_ = Time();
delayed_work_time_ = TimeTicks();
}
}
}
@ -364,7 +364,8 @@ void MessagePumpLibevent::ScheduleWork() {
<< "[nwrite:" << nwrite << "] [errno:" << errno << "]";
}
void MessagePumpLibevent::ScheduleDelayedWork(const Time& delayed_work_time) {
void MessagePumpLibevent::ScheduleDelayedWork(
const TimeTicks& delayed_work_time) {
// We know that we can't be blocked on Wait right now since this method can
// only be called on the same thread as Run, so we only need to update our
// record of how long to sleep when we do sleep.

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

@ -134,7 +134,7 @@ class MessagePumpLibevent : public MessagePump {
virtual void Run(Delegate* delegate);
virtual void Quit();
virtual void ScheduleWork();
virtual void ScheduleDelayedWork(const Time& delayed_work_time);
virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time);
private:
@ -148,7 +148,7 @@ class MessagePumpLibevent : public MessagePump {
bool in_run_;
// The time at which we should call DoDelayedWork.
Time delayed_work_time_;
TimeTicks delayed_work_time_;
// Libevent dispatcher. Watches all sockets registered with it, and sends
// readiness callbacks when a socket is ready for I/O.

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

@ -43,7 +43,7 @@ class NSAutoreleasePool;
namespace base {
class Time;
class TimeTicks;
class MessagePumpCFRunLoopBase : public MessagePump {
// Needs access to CreateAutoreleasePool.
@ -60,7 +60,7 @@ class MessagePumpCFRunLoopBase : public MessagePump {
virtual void DoRun(Delegate* delegate) = 0;
virtual void ScheduleWork();
virtual void ScheduleDelayedWork(const Time& delayed_work_time);
virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time);
protected:
// Accessors for private data members to be used by subclasses.

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

@ -225,11 +225,17 @@ void MessagePumpCFRunLoopBase::ScheduleWork() {
// Must be called on the run loop thread.
void MessagePumpCFRunLoopBase::ScheduleDelayedWork(
const Time& delayed_work_time) {
const TimeTicks& delayed_work_time) {
// TODO(jar): We may need a more efficient way to go between these times, but
// the difference will change not only when we sleep/wake, it will also change
// when the user changes the wall clock time :-/.
Time absolute_work_time =
(delayed_work_time - TimeTicks::Now()) + Time::Now();
Time::Exploded exploded;
delayed_work_time.UTCExplode(&exploded);
absolute_work_time.UTCExplode(&exploded);
double seconds = exploded.second +
(static_cast<double>((delayed_work_time.ToInternalValue()) %
(static_cast<double>((absolute_work_time.ToInternalValue()) %
Time::kMicrosecondsPerSecond) /
Time::kMicrosecondsPerSecond);
CFGregorianDate gregorian = {
@ -320,12 +326,12 @@ bool MessagePumpCFRunLoopBase::RunDelayedWork() {
// released promptly even in the absence of UI events.
MessagePumpScopedAutoreleasePool autorelease_pool(this);
Time next_time;
TimeTicks next_time;
delegate_->DoDelayedWork(&next_time);
bool more_work = !next_time.is_null();
if (more_work) {
TimeDelta delay = next_time - Time::Now();
TimeDelta delay = next_time - TimeTicks::Now();
if (delay > TimeDelta()) {
// There's more delayed work to be done in the future.
ScheduleDelayedWork(next_time);

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

@ -65,7 +65,7 @@ MessagePumpQt::event(QEvent *e)
}
void
MessagePumpQt::scheduleDelayedIfNeeded(const Time& delayed_work_time)
MessagePumpQt::scheduleDelayedIfNeeded(const TimeTicks& delayed_work_time)
{
if (delayed_work_time.is_null()) {
return;
@ -75,7 +75,7 @@ MessagePumpQt::scheduleDelayedIfNeeded(const Time& delayed_work_time)
mTimer->stop();
}
TimeDelta later = delayed_work_time - Time::Now();
TimeDelta later = delayed_work_time - TimeTicks::Now();
// later.InMilliseconds() returns an int64_t, QTimer only accepts int's for start(),
// std::min only works on exact same types.
int laterMsecs = later.InMilliseconds() > std::numeric_limits<int>::max() ?
@ -183,7 +183,7 @@ void MessagePumpForUI::ScheduleWork() {
new QEvent((QEvent::Type) sPokeEvent));
}
void MessagePumpForUI::ScheduleDelayedWork(const Time& delayed_work_time) {
void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) {
// On GLib implementation, a work source is defined which explicitly checks the
// time that has passed. Here, on Qt we can use a QTimer that enqueues our
// event signal in an event queue.

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

@ -29,7 +29,7 @@ class MessagePumpQt : public QObject {
~MessagePumpQt();
virtual bool event (QEvent *e);
void scheduleDelayedIfNeeded(const Time& delayed_work_time);
void scheduleDelayedIfNeeded(const TimeTicks& delayed_work_time);
public slots:
void dispatchDelayed();
@ -50,7 +50,7 @@ class MessagePumpForUI : public MessagePump {
virtual void Run(Delegate* delegate);
virtual void Quit();
virtual void ScheduleWork();
virtual void ScheduleDelayedWork(const Time& delayed_work_time);
virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time);
// Internal methods used for processing the pump callbacks. They are
// public for simplicity but should not be used directly.
@ -73,7 +73,7 @@ class MessagePumpForUI : public MessagePump {
RunState* state_;
// This is the time when we need to do delayed work.
Time delayed_work_time_;
TimeTicks delayed_work_time_;
// MessagePump implementation for Qt based on the GLib implement.
// On Qt we use a QObject base class and the

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

@ -70,7 +70,8 @@ int MessagePumpWin::GetCurrentDelay() const {
// Be careful here. TimeDelta has a precision of microseconds, but we want a
// value in milliseconds. If there are 5.5ms left, should the delay be 5 or
// 6? It should be 6 to avoid executing delayed work too early.
double timeout = ceil((delayed_work_time_ - Time::Now()).InMillisecondsF());
double timeout =
ceil((delayed_work_time_ - TimeTicks::Now()).InMillisecondsF());
// If this value is negative, then we need to run delayed work soon.
int delay = static_cast<int>(timeout);
@ -104,7 +105,7 @@ void MessagePumpForUI::ScheduleWork() {
PostMessage(message_hwnd_, WM_NULL, 0, 0);
}
void MessagePumpForUI::ScheduleDelayedWork(const Time& delayed_work_time) {
void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) {
//
// We would *like* to provide high resolution timers. Windows timers using
// SetTimer() have a 10ms granularity. We have to use WM_TIMER as a wakeup
@ -431,7 +432,7 @@ void MessagePumpForIO::ScheduleWork() {
DCHECK(ret);
}
void MessagePumpForIO::ScheduleDelayedWork(const Time& delayed_work_time) {
void MessagePumpForIO::ScheduleDelayedWork(const TimeTicks& delayed_work_time) {
// We know that we can't be blocked right now since this method can only be
// called on the same thread as Run, so we only need to update our record of
// how long to sleep when we do sleep.

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

@ -96,7 +96,7 @@ class MessagePumpWin : public MessagePump {
ObserverList<Observer> observers_;
// The time at which delayed work should run.
Time delayed_work_time_;
TimeTicks delayed_work_time_;
// A boolean value used to indicate if there is a kMsgDoWork message pending
// in the Windows Message queue. There is at most one such message, and it
@ -162,7 +162,7 @@ class MessagePumpForUI : public MessagePumpWin {
// MessagePump methods:
virtual void ScheduleWork();
virtual void ScheduleDelayedWork(const Time& delayed_work_time);
virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time);
// Applications can call this to encourage us to process all pending WM_PAINT
// messages. This method will process all paint messages the Windows Message
@ -299,7 +299,7 @@ class MessagePumpForIO : public MessagePumpWin {
// MessagePump methods:
virtual void ScheduleWork();
virtual void ScheduleDelayedWork(const Time& delayed_work_time);
virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time);
// Register the handler to be used when asynchronous IO for the given file
// completes. The registration persists as long as |file_handle| is valid, so

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

@ -22,7 +22,7 @@
using mozilla::ipc::DoWorkRunnable;
using mozilla::ipc::MessagePump;
using mozilla::ipc::MessagePumpForChildProcess;
using base::Time;
using base::TimeTicks;
NS_IMPL_THREADSAFE_ISUPPORTS2(DoWorkRunnable, nsIRunnable, nsITimerCallback)
@ -147,7 +147,7 @@ MessagePump::ScheduleWorkForNestedLoop()
}
void
MessagePump::ScheduleDelayedWork(const base::Time& aDelayedTime)
MessagePump::ScheduleDelayedWork(const base::TimeTicks& aDelayedTime)
{
if (!mDelayedWorkTimer) {
mDelayedWorkTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
@ -165,7 +165,7 @@ MessagePump::ScheduleDelayedWork(const base::Time& aDelayedTime)
delayed_work_time_ = aDelayedTime;
base::TimeDelta delay = aDelayedTime - base::Time::Now();
base::TimeDelta delay = aDelayedTime - base::TimeTicks::Now();
uint32_t delayMS = uint32_t(delay.InMilliseconds());
mDelayedWorkTimer->InitWithCallback(mDoWorkEvent, delayMS,
nsITimer::TYPE_ONE_SHOT);

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

@ -46,7 +46,7 @@ public:
virtual void Run(base::MessagePump::Delegate* aDelegate);
virtual void ScheduleWork();
virtual void ScheduleWorkForNestedLoop();
virtual void ScheduleDelayedWork(const base::Time& delayed_work_time);
virtual void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time);
void DoDelayedWork(base::MessagePump::Delegate* aDelegate);