提交 bf14e27a 编写于 作者: A Adam Barth

Move SharedTimer implementation into sky/engine

Now that sky/engine can talk to base directly, there's no reason to use
Platform::current to implement SharedTimer. Instead, we can just implement it
directly in SharedTimer.cpp.

R=ojan@chromium.org, eseidel@chromium.org

Review URL: https://codereview.chromium.org/886263002
上级 3b32bd6a
/*
* Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sky/engine/config.h"
#include "sky/engine/platform/SharedTimer.h"
#include "sky/engine/public/platform/Platform.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "sky/engine/wtf/CurrentTime.h"
namespace blink {
namespace {
class SharedTimerImpl {
WTF_MAKE_NONCOPYABLE(SharedTimerImpl);
public:
SharedTimerImpl()
: m_sharedTimerFunction(nullptr)
, m_sharedTimerFireTime(0)
{
}
static SharedTimerImpl& instance()
{
DEFINE_STATIC_LOCAL(SharedTimerImpl, instance, ());
return instance;
}
void setSharedTimerFiredFunction(void (*function)())
{
m_sharedTimerFunction = function;
}
void setSharedTimerFireInterval(double intervalSeconds)
{
double now = monotonicallyIncreasingTime();
m_sharedTimerFireTime = intervalSeconds + now;
// By converting between double and int64 representation, we run the risk
// of losing precision due to rounding errors. Performing computations in
// microseconds reduces this risk somewhat. But there still is the potential
// of us computing a fire time for the timer that is shorter than what we
// need.
// As the event loop will check event deadlines prior to actually firing
// them, there is a risk of needlessly rescheduling events and of
// needlessly looping if sleep times are too short even by small amounts.
// This results in measurable performance degradation unless we use ceil() to
// always round up the sleep times.
int64 interval = static_cast<int64>(ceil(intervalSeconds * base::Time::kMillisecondsPerSecond)
* base::Time::kMicrosecondsPerMillisecond);
if (interval < 0)
interval = 0;
m_sharedTimer.Stop();
m_sharedTimer.Start(FROM_HERE,
base::TimeDelta::FromMicroseconds(interval), this, &SharedTimerImpl::timerFired);
}
void stopSharedTimer()
{
m_sharedTimer.Stop();
}
private:
void timerFired()
{
if (m_sharedTimerFunction)
m_sharedTimerFunction();
}
base::OneShotTimer<SharedTimerImpl> m_sharedTimer;
void (*m_sharedTimerFunction)();
double m_sharedTimerFireTime;
};
}
void setSharedTimerFiredFunction(void (*f)())
{
blink::Platform::current()->setSharedTimerFiredFunction(f);
SharedTimerImpl::instance().setSharedTimerFiredFunction(f);
}
void setSharedTimerFireInterval(double interval)
{
blink::Platform::current()->setSharedTimerFireInterval(interval);
SharedTimerImpl::instance().setSharedTimerFireInterval(interval);
}
void stopSharedTimer()
{
blink::Platform::current()->stopSharedTimer();
SharedTimerImpl::instance().stopSharedTimer();
}
} // namespace blink
......@@ -16,12 +16,7 @@
namespace sky {
PlatformImpl::PlatformImpl()
: main_loop_(base::MessageLoop::current()),
shared_timer_func_(NULL),
shared_timer_fire_time_(0.0),
shared_timer_fire_time_was_set_while_suspended_(false),
shared_timer_suspended_(0) {
PlatformImpl::PlatformImpl() {
}
PlatformImpl::~PlatformImpl() {
......@@ -31,53 +26,6 @@ blink::WebString PlatformImpl::defaultLocale() {
return blink::WebString::fromUTF8("en-US");
}
double PlatformImpl::currentTime() {
return base::Time::Now().ToDoubleT();
}
double PlatformImpl::monotonicallyIncreasingTime() {
return base::TimeTicks::Now().ToInternalValue() /
static_cast<double>(base::Time::kMicrosecondsPerSecond);
}
void PlatformImpl::setSharedTimerFiredFunction(void (*func)()) {
shared_timer_func_ = func;
}
void PlatformImpl::setSharedTimerFireInterval(
double interval_seconds) {
shared_timer_fire_time_ = interval_seconds + monotonicallyIncreasingTime();
if (shared_timer_suspended_) {
shared_timer_fire_time_was_set_while_suspended_ = true;
return;
}
// By converting between double and int64 representation, we run the risk
// of losing precision due to rounding errors. Performing computations in
// microseconds reduces this risk somewhat. But there still is the potential
// of us computing a fire time for the timer that is shorter than what we
// need.
// As the event loop will check event deadlines prior to actually firing
// them, there is a risk of needlessly rescheduling events and of
// needlessly looping if sleep times are too short even by small amounts.
// This results in measurable performance degradation unless we use ceil() to
// always round up the sleep times.
int64 interval = static_cast<int64>(
ceil(interval_seconds * base::Time::kMillisecondsPerSecond)
* base::Time::kMicrosecondsPerMillisecond);
if (interval < 0)
interval = 0;
shared_timer_.Stop();
shared_timer_.Start(FROM_HERE, base::TimeDelta::FromMicroseconds(interval),
this, &PlatformImpl::DoTimeout);
}
void PlatformImpl::stopSharedTimer() {
shared_timer_.Stop();
}
blink::WebUnitTestSupport* PlatformImpl::unitTestSupport() {
return &unit_test_support_;
}
......
......@@ -22,27 +22,8 @@ class PlatformImpl : public blink::Platform {
// blink::Platform methods:
virtual blink::WebUnitTestSupport* unitTestSupport();
virtual blink::WebString defaultLocale();
virtual double currentTime();
virtual double monotonicallyIncreasingTime();
virtual void setSharedTimerFiredFunction(void (*func)());
virtual void setSharedTimerFireInterval(double interval_seconds);
virtual void stopSharedTimer();
private:
void SuspendSharedTimer();
void ResumeSharedTimer();
void DoTimeout() {
if (shared_timer_func_ && !shared_timer_suspended_)
shared_timer_func_();
}
base::MessageLoop* main_loop_;
base::OneShotTimer<PlatformImpl> shared_timer_;
void (*shared_timer_func_)();
double shared_timer_fire_time_;
bool shared_timer_fire_time_was_set_while_suspended_;
int shared_timer_suspended_; // counter
WebUnitTestSupportImpl unit_test_support_;
DISALLOW_COPY_AND_ASSIGN(PlatformImpl);
......
......@@ -21,15 +21,8 @@ namespace sky {
PlatformImpl::PlatformImpl(mojo::ApplicationImpl* app)
: main_loop_(base::MessageLoop::current()),
main_thread_task_runner_(base::MessageLoop::current()->task_runner()),
shared_timer_func_(NULL),
shared_timer_fire_time_(0.0),
shared_timer_fire_time_was_set_while_suspended_(false),
shared_timer_suspended_(0) {
main_thread_task_runner_(base::MessageLoop::current()->task_runner()) {
app->ConnectToService("mojo:network_service", &network_service_);
mojo::CookieStorePtr cookie_store;
network_service_->GetCookieStore(GetProxy(&cookie_store));
}
PlatformImpl::~PlatformImpl() {
......@@ -39,47 +32,6 @@ blink::WebString PlatformImpl::defaultLocale() {
return blink::WebString::fromUTF8("en-US");
}
void PlatformImpl::setSharedTimerFiredFunction(void (*func)()) {
shared_timer_func_ = func;
}
void PlatformImpl::setSharedTimerFireInterval(
double interval_seconds) {
double now = base::TimeTicks::Now().ToInternalValue() /
static_cast<double>(base::Time::kMicrosecondsPerSecond);
shared_timer_fire_time_ = interval_seconds + now;
if (shared_timer_suspended_) {
shared_timer_fire_time_was_set_while_suspended_ = true;
return;
}
// By converting between double and int64 representation, we run the risk
// of losing precision due to rounding errors. Performing computations in
// microseconds reduces this risk somewhat. But there still is the potential
// of us computing a fire time for the timer that is shorter than what we
// need.
// As the event loop will check event deadlines prior to actually firing
// them, there is a risk of needlessly rescheduling events and of
// needlessly looping if sleep times are too short even by small amounts.
// This results in measurable performance degradation unless we use ceil() to
// always round up the sleep times.
int64 interval = static_cast<int64>(
ceil(interval_seconds * base::Time::kMillisecondsPerSecond)
* base::Time::kMicrosecondsPerMillisecond);
if (interval < 0)
interval = 0;
shared_timer_.Stop();
shared_timer_.Start(FROM_HERE, base::TimeDelta::FromMicroseconds(interval),
this, &PlatformImpl::DoTimeout);
}
void PlatformImpl::stopSharedTimer() {
shared_timer_.Stop();
}
base::SingleThreadTaskRunner* PlatformImpl::mainThreadTaskRunner() {
return main_thread_task_runner_.get();
}
......
......@@ -25,31 +25,15 @@ class PlatformImpl : public blink::Platform {
// blink::Platform methods:
virtual blink::WebString defaultLocale();
virtual void setSharedTimerFiredFunction(void (*func)());
virtual void setSharedTimerFireInterval(double interval_seconds);
virtual void stopSharedTimer();
virtual base::SingleThreadTaskRunner* mainThreadTaskRunner();
virtual mojo::NetworkService* networkService();
virtual blink::WebURLLoader* createURLLoader();
virtual blink::WebURLError cancelledError(const blink::WebURL& url) const;
private:
void SuspendSharedTimer();
void ResumeSharedTimer();
void DoTimeout() {
if (shared_timer_func_ && !shared_timer_suspended_)
shared_timer_func_();
}
mojo::NetworkServicePtr network_service_;
base::MessageLoop* main_loop_;
scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
base::OneShotTimer<PlatformImpl> shared_timer_;
void (*shared_timer_func_)();
double shared_timer_fire_time_;
bool shared_timer_fire_time_was_set_while_suspended_;
int shared_timer_suspended_; // counter
DISALLOW_COPY_AND_ASSIGN(PlatformImpl);
};
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册