flutter_flutter/flutter/tonic/dart_microtask_queue.cc
Adam Barth 633d674c48 Move tonic to //flutter/tonic (#2742)
Now that tonic doesn't depend on anything in //sky/engine anymore, we
can move the code to a location where its dependencies are clearer.
2016-06-10 22:36:38 -07:00

45 lines
1.1 KiB
C++

// 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 "flutter/tonic/dart_microtask_queue.h"
#include "base/trace_event/trace_event.h"
#include "flutter/tonic/dart_invoke.h"
#include "flutter/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());
DartInvokeVoid(callback.value());
}
}
}
}