提交 1f4416f8 编写于 作者: A Adam Barth

Move microtask queue into tonic

Moving the microtask queue into tonic solves three problems:

1) Removes three levels of indirection when invoking microtask
callbacks.
2) Removes the sky/engine/dom directory entirely.
3) Removes the last client of the (inefficient) DartValue class.
上级 6fb5ebe3
......@@ -15,7 +15,6 @@
#include "dart/runtime/bin/embedded_dart_io.h"
#include "dart/runtime/include/dart_api.h"
#include "dart/runtime/include/dart_tools_api.h"
#include "sky/engine/core/dom/Microtask.h"
#include "sky/engine/core/script/dom_dart_state.h"
#include "sky/engine/tonic/dart_api_scope.h"
#include "sky/engine/tonic/dart_converter.h"
......@@ -23,9 +22,9 @@
#include "sky/engine/tonic/dart_invoke.h"
#include "sky/engine/tonic/dart_isolate_scope.h"
#include "sky/engine/tonic/dart_library_natives.h"
#include "sky/engine/tonic/dart_microtask_queue.h"
#include "sky/engine/tonic/dart_state.h"
#include "sky/engine/tonic/dart_timer_heap.h"
#include "sky/engine/tonic/dart_value.h"
#include "sky/engine/wtf/text/WTFString.h"
#if defined(OS_ANDROID)
......@@ -163,23 +162,11 @@ void Logger_PrintString(Dart_NativeArguments args) {
}
}
static void ExecuteMicrotask(base::WeakPtr<DartState> dart_state,
RefPtr<DartValue> callback) {
if (!dart_state)
return;
DartIsolateScope scope(dart_state->isolate());
DartApiScope api_scope;
DartInvokeAppClosure(callback->dart_value(), 0, nullptr);
}
void ScheduleMicrotask(Dart_NativeArguments args) {
Dart_Handle closure = Dart_GetNativeArgument(args, 0);
if (LogIfError(closure) || !Dart_IsClosure(closure))
return;
DartState* dart_state = DartState::Current();
CHECK(dart_state);
Microtask::enqueueMicrotask(base::Bind(&ExecuteMicrotask,
dart_state->GetWeakPtr(), DartValue::Create(dart_state, closure)));
DartMicrotaskQueue::ScheduleMicrotask(closure);
}
void GetBaseURLString(Dart_NativeArguments args) {
......
......@@ -11,8 +11,6 @@ sky_core_files = [
"compositing/Scene.h",
"compositing/SceneBuilder.cpp",
"compositing/SceneBuilder.h",
"dom/Microtask.cpp",
"dom/Microtask.h",
"editing/CompositionUnderline.h",
"editing/CompositionUnderlineRangeFilter.cpp",
"editing/CompositionUnderlineRangeFilter.h",
......
/*
* Copyright (C) 2013 Google 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.
* 3. Neither the name of Google Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "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 THE COPYRIGHT
* OWNER 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.
*/
#include "sky/engine/core/dom/Microtask.h"
#include "base/bind.h"
#include "base/trace_event/trace_event.h"
#include "sky/engine/public/platform/WebThread.h"
#include "sky/engine/wtf/OwnPtr.h"
#include "sky/engine/wtf/Vector.h"
namespace blink {
namespace {
class Task : public WebThread::Task {
public:
explicit Task(const base::Closure& closure)
: m_closure(closure)
{
}
virtual void run() override
{
m_closure.Run();
}
private:
base::Closure m_closure;
};
}
// TODO(dart): Integrate this microtask queue with darts.
typedef Vector<OwnPtr<WebThread::Task> > MicrotaskQueue;
static MicrotaskQueue& microtaskQueue()
{
DEFINE_STATIC_LOCAL(OwnPtr<MicrotaskQueue>, queue, (adoptPtr(new MicrotaskQueue())));
return *queue;
}
void Microtask::performCheckpoint()
{
MicrotaskQueue& queue = microtaskQueue();
while(!queue.isEmpty()) {
TRACE_EVENT0("flutter", "Microtask::performCheckpoint");
MicrotaskQueue local;
swap(queue, local);
for (const auto& task : local)
task->run();
}
}
void Microtask::enqueueMicrotask(PassOwnPtr<WebThread::Task> callback)
{
microtaskQueue().append(callback);
}
void Microtask::enqueueMicrotask(const base::Closure& callback)
{
enqueueMicrotask(adoptPtr(new Task(callback)));
}
} // namespace blink
/*
* Copyright (C) 2013 Google 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.
* 3. Neither the name of Google Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "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 THE COPYRIGHT
* OWNER 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.
*/
#ifndef SKY_ENGINE_CORE_DOM_MICROTASK_H_
#define SKY_ENGINE_CORE_DOM_MICROTASK_H_
#include "base/callback_forward.h"
#include "sky/engine/public/platform/WebThread.h"
#include "sky/engine/wtf/PassOwnPtr.h"
namespace blink {
class Microtask {
public:
static void performCheckpoint();
static void enqueueMicrotask(PassOwnPtr<WebThread::Task>);
static void enqueueMicrotask(const base::Closure&);
private:
explicit Microtask();
};
} // namespace blink
#endif // SKY_ENGINE_CORE_DOM_MICROTASK_H_
......@@ -6,7 +6,6 @@
#include "sky/engine/core/script/dom_dart_state.h"
#include "sky/engine/tonic/dart_error.h"
#include "sky/engine/tonic/dart_value.h"
namespace blink {
......
......@@ -6,7 +6,6 @@
#include "sky/engine/core/script/dom_dart_state.h"
#include "sky/engine/tonic/dart_error.h"
#include "sky/engine/tonic/dart_value.h"
namespace blink {
......
......@@ -6,7 +6,6 @@
#include "sky/engine/core/script/dom_dart_state.h"
#include "sky/engine/tonic/dart_error.h"
#include "sky/engine/tonic/dart_value.h"
#include "base/logging.h"
namespace blink {
......
......@@ -6,7 +6,6 @@
#include "sky/engine/core/script/dom_dart_state.h"
#include "sky/engine/tonic/dart_error.h"
#include "sky/engine/tonic/dart_value.h"
#include "base/logging.h"
namespace blink {
......
......@@ -22,7 +22,6 @@ class DartUI;
class DOMDartState;
class DartLibraryProvider;
class DartSnapshotLoader;
class DartValue;
class View;
class DartController {
......
......@@ -33,6 +33,8 @@ source_set("tonic") {
"dart_library_natives.h",
"dart_library_provider.cc",
"dart_library_provider.h",
"dart_microtask_queue.cc",
"dart_microtask_queue.h",
"dart_persistent_value.cc",
"dart_persistent_value.h",
"dart_snapshot_loader.cc",
......@@ -45,8 +47,6 @@ source_set("tonic") {
"dart_string_cache.h",
"dart_timer_heap.cc",
"dart_timer_heap.h",
"dart_value.cc",
"dart_value.h",
"dart_wrappable.cc",
"dart_wrappable.h",
"dart_wrapper_info.h",
......
......@@ -9,7 +9,6 @@
#include "sky/engine/tonic/dart_state.h"
#include "sky/engine/tonic/dart_string.h"
#include "sky/engine/tonic/dart_string_cache.h"
#include "sky/engine/tonic/dart_value.h"
#include "sky/engine/wtf/text/StringUTF8Adaptor.h"
#include "sky/engine/wtf/text/WTFString.h"
......@@ -345,30 +344,6 @@ struct DartConverter<Vector<T>> {
}
};
////////////////////////////////////////////////////////////////////////////////
// DartValue
template <>
struct DartConverter<DartValue*> {
static Dart_Handle ToDart(DartState* state, DartValue* val) {
return val->dart_value();
}
static void SetReturnValue(Dart_NativeArguments args, DartValue* val) {
Dart_SetReturnValue(args, val->dart_value());
}
static PassRefPtr<DartValue> FromDart(Dart_Handle handle) {
return DartValue::Create(DartState::Current(), handle);
}
static PassRefPtr<DartValue> FromArguments(Dart_NativeArguments args,
int index,
Dart_Handle& exception) {
return FromDart(Dart_GetNativeArgument(args, index));
}
};
////////////////////////////////////////////////////////////////////////////////
// Dart_Handle
......
// Copyright 2016 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/tonic/dart_microtask_queue.h"
#include "base/trace_event/trace_event.h"
#include "sky/engine/tonic/dart_invoke.h"
#include "sky/engine/tonic/dart_state.h"
namespace blink {
namespace {
typedef std::vector<DartPersistentValue> MicrotaskQueue;
static MicrotaskQueue& GetQueue() {
static MicrotaskQueue* queue = new MicrotaskQueue();
return *queue;
}
} // namespace
void DartMicrotaskQueue::ScheduleMicrotask(Dart_Handle callback) {
GetQueue().emplace_back(DartState::Current(), callback);
}
void DartMicrotaskQueue::RunMicrotasks() {
MicrotaskQueue& queue = GetQueue();
while(!queue.empty()) {
TRACE_EVENT0("flutter", "DartMicrotaskQueue::RunMicrotasks");
MicrotaskQueue local;
std::swap(queue, local);
for (const auto& callback : local) {
base::WeakPtr<DartState> dart_state = callback.dart_state();
if (!dart_state.get())
continue;
DartState::Scope dart_scope(dart_state.get());
DartInvokeAppClosure(callback.value(), 0, nullptr);
}
}
}
}
// Copyright 2016 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.
#ifndef SKY_ENGINE_TONIC_DART_MICROTASK_QUEUE_H_
#define SKY_ENGINE_TONIC_DART_MICROTASK_QUEUE_H_
#include "dart/runtime/include/dart_api.h"
namespace blink {
class DartMicrotaskQueue {
public:
static void ScheduleMicrotask(Dart_Handle callback);
static void RunMicrotasks();
};
} // namespace blink
#endif // SKY_ENGINE_TONIC_DART_MICROTASK_QUEUE_H_
......@@ -12,6 +12,13 @@ namespace blink {
DartPersistentValue::DartPersistentValue() : value_(nullptr) {
}
DartPersistentValue::DartPersistentValue(DartPersistentValue&& other)
: dart_state_(other.dart_state_),
value_(other.value_) {
other.dart_state_ = base::WeakPtr<DartState>();
other.value_ = nullptr;
}
DartPersistentValue::DartPersistentValue(DartState* dart_state,
Dart_Handle value)
: value_(nullptr) {
......
......@@ -19,6 +19,7 @@ class DartState;
class DartPersistentValue {
public:
DartPersistentValue();
DartPersistentValue(DartPersistentValue&& other);
DartPersistentValue(DartState* dart_state, Dart_Handle value);
~DartPersistentValue();
......
// 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/tonic/dart_value.h"
namespace blink {
DartValue::DartValue() {
}
DartValue::DartValue(DartState* dart_state, Dart_Handle value)
: dart_value_(dart_state, value) {
}
DartValue::~DartValue() {
}
bool DartValue::Equals(DartValue* other) const {
DCHECK(other);
if (is_empty())
return other->is_empty();
if (other->is_empty())
return false;
return Dart_IdentityEquals(dart_value(), other->dart_value());
}
void DartValue::Clear() {
dart_value_.Clear();
}
} // namespace blink
// 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.
#ifndef SKY_ENGINE_TONIC_DART_VALUE_H_
#define SKY_ENGINE_TONIC_DART_VALUE_H_
#include "base/logging.h"
#include "dart/runtime/include/dart_api.h"
#include "sky/engine/tonic/dart_persistent_value.h"
#include "sky/engine/tonic/dart_state.h"
#include "sky/engine/wtf/PassRefPtr.h"
#include "sky/engine/wtf/RefCounted.h"
#include "sky/engine/wtf/RefPtr.h"
namespace blink {
// DartValue is a convience wrapper around DartPersistentValue that lets clients
// use RefPtr to keep track of the number of references to the underlying Dart
// object. Be careful when retaining RefPtr<DartValue> in the heap because the
// VM's garbage collector cannot break cycles that involve the C++ heap, which
// can lead to memory leaks.
class DartValue : public RefCounted<DartValue> {
public:
static PassRefPtr<DartValue> Create(DartState* dart_state,
Dart_Handle value) {
return adoptRef(new DartValue(dart_state, value));
}
static PassRefPtr<DartValue> Create() { return adoptRef(new DartValue()); }
~DartValue();
Dart_Handle dart_value() const { return dart_value_.value(); }
const base::WeakPtr<DartState>& dart_state() const {
return dart_value_.dart_state();
}
bool is_empty() const { return !dart_value(); }
bool is_null() const {
DCHECK(!is_empty());
return Dart_IsNull(dart_value());
}
bool is_function() const {
DCHECK(!is_empty());
return Dart_IsClosure(dart_value());
}
bool Equals(DartValue* other) const;
void Clear();
private:
DartValue();
DartValue(DartState* dart_state, Dart_Handle value);
DartPersistentValue dart_value_;
DISALLOW_COPY_AND_ASSIGN(DartValue);
};
} // namespace blink
#endif // SKY_ENGINE_TONIC_DART_VALUE_H_
......@@ -33,12 +33,12 @@
#include "base/message_loop/message_loop.h"
#include "base/rand_util.h"
#include "mojo/message_pump/message_pump_mojo.h"
#include "sky/engine/core/dom/Microtask.h"
#include "sky/engine/core/Init.h"
#include "sky/engine/core/script/dart_init.h"
#include "sky/engine/platform/LayoutTestSupport.h"
#include "sky/engine/platform/Logging.h"
#include "sky/engine/public/platform/Platform.h"
#include "sky/engine/tonic/dart_microtask_queue.h"
#include "sky/engine/wtf/Assertions.h"
#include "sky/engine/wtf/CryptographicallyRandomNumber.h"
#include "sky/engine/wtf/MainThread.h"
......@@ -57,7 +57,7 @@ void willProcessTask()
void didProcessTask()
{
Microtask::performCheckpoint();
DartMicrotaskQueue::RunMicrotasks();
// FIXME: Report memory usage to dart?
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册