mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Re-arm timer as necessary in MessageLoopFuchsia
This commit is contained in:
parent
72d0154e2b
commit
da0fcfec7a
@ -227,6 +227,7 @@ executable("fml_unittests") {
|
||||
"memory/weak_ptr_unittest.cc",
|
||||
"message_loop_task_queues_merge_unmerge_unittests.cc",
|
||||
"message_loop_task_queues_unittests.cc",
|
||||
"message_loop_unittests.cc",
|
||||
"message_unittests.cc",
|
||||
"paths_unittests.cc",
|
||||
"platform/darwin/string_range_sanitization_unittests.mm",
|
||||
@ -243,10 +244,7 @@ executable("fml_unittests") {
|
||||
|
||||
# TODO(gw280): Figure out why these tests don't work currently on Fuchsia
|
||||
if (!is_fuchsia) {
|
||||
sources += [
|
||||
"file_unittest.cc",
|
||||
"message_loop_unittests.cc",
|
||||
]
|
||||
sources += [ "file_unittest.cc" ]
|
||||
}
|
||||
|
||||
deps = [
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
#include "flutter/fml/build_config.h"
|
||||
#include "flutter/fml/concurrent_message_loop.h"
|
||||
#include "flutter/fml/message_loop.h"
|
||||
#include "flutter/fml/message_loop_impl.h"
|
||||
#include "flutter/fml/synchronization/count_down_latch.h"
|
||||
#include "flutter/fml/synchronization/waitable_event.h"
|
||||
#include "flutter/fml/task_runner.h"
|
||||
@ -309,3 +310,32 @@ TEST(MessageLoop, CanCreateConcurrentMessageLoop) {
|
||||
latch.Wait();
|
||||
ASSERT_GE(thread_ids.size(), 1u);
|
||||
}
|
||||
|
||||
TEST(MessageLoop, TIME_SENSITIVE(WakeUpTimersAreSingletons)) {
|
||||
auto loop_impl = fml::MessageLoopImpl::Create();
|
||||
|
||||
const auto t1 = fml::TimeDelta::FromMilliseconds(10);
|
||||
const auto t2 = fml::TimeDelta::FromMilliseconds(20);
|
||||
|
||||
const auto begin = fml::TimePoint::Now();
|
||||
|
||||
// Register a task scheduled for 10ms in the future. This schedules a
|
||||
// WakeUp call on the MessageLoopImpl with that fml::TimePoint
|
||||
loop_impl->PostTask(
|
||||
[&]() {
|
||||
auto delta = fml::TimePoint::Now() - begin;
|
||||
auto ms = delta.ToMillisecondsF();
|
||||
ASSERT_GE(ms, 18);
|
||||
ASSERT_LE(ms, 22);
|
||||
|
||||
loop_impl->Terminate();
|
||||
},
|
||||
fml::TimePoint::Now() + t1);
|
||||
|
||||
// Call WakeUp manually to change the WakeUp time to the future. If the
|
||||
// timer is correctly set up to be rearmed instead of a new timer scheduled,
|
||||
// the above task will be executed at t2 instead of t1 now.
|
||||
loop_impl->WakeUp(fml::TimePoint::Now() + t2);
|
||||
|
||||
loop_impl->Run();
|
||||
}
|
||||
|
||||
@ -5,13 +5,16 @@
|
||||
#include "flutter/fml/platform/fuchsia/message_loop_fuchsia.h"
|
||||
|
||||
#include <lib/async-loop/default.h>
|
||||
#include <lib/async/cpp/task.h>
|
||||
#include <lib/zx/time.h>
|
||||
|
||||
namespace fml {
|
||||
|
||||
MessageLoopFuchsia::MessageLoopFuchsia()
|
||||
: loop_(&kAsyncLoopConfigAttachToCurrentThread) {}
|
||||
: loop_(&kAsyncLoopConfigAttachToCurrentThread) {
|
||||
auto handler = [this](async_dispatcher_t* dispatcher, async::Task* task,
|
||||
zx_status_t status) { RunExpiredTasksNow(); };
|
||||
task_.set_handler(handler);
|
||||
}
|
||||
|
||||
MessageLoopFuchsia::~MessageLoopFuchsia() = default;
|
||||
|
||||
@ -30,8 +33,12 @@ void MessageLoopFuchsia::WakeUp(fml::TimePoint time_point) {
|
||||
due_time = zx::nsec((time_point - now).ToNanoseconds());
|
||||
}
|
||||
|
||||
auto status = async::PostDelayedTask(
|
||||
loop_.dispatcher(), [this]() { RunExpiredTasksNow(); }, due_time);
|
||||
std::scoped_lock lock(task_mutex_);
|
||||
|
||||
auto status = task_.Cancel();
|
||||
FML_DCHECK(status == ZX_OK || status == ZX_ERR_NOT_FOUND);
|
||||
|
||||
status = task_.PostDelayed(loop_.dispatcher(), due_time);
|
||||
FML_DCHECK(status == ZX_OK);
|
||||
}
|
||||
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
#define FLUTTER_FML_PLATFORM_FUCHSIA_MESSAGE_LOOP_FUCHSIA_H_
|
||||
|
||||
#include <lib/async-loop/cpp/loop.h>
|
||||
#include <lib/async/cpp/task.h>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "flutter/fml/message_loop_impl.h"
|
||||
@ -25,6 +26,8 @@ class MessageLoopFuchsia : public MessageLoopImpl {
|
||||
void WakeUp(fml::TimePoint time_point) override;
|
||||
|
||||
async::Loop loop_;
|
||||
std::mutex task_mutex_;
|
||||
async::Task task_;
|
||||
|
||||
FML_FRIEND_MAKE_REF_COUNTED(MessageLoopFuchsia);
|
||||
FML_FRIEND_REF_COUNTED_THREAD_SAFE(MessageLoopFuchsia);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user