Skip to content

Commit 8ec994b

Browse files
George Wrightgw280
authored andcommitted
Re-arm timer as necessary in MessageLoopFuchsia
1 parent 1d05c83 commit 8ec994b

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

fml/BUILD.gn

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ executable("fml_unittests") {
227227
"memory/weak_ptr_unittest.cc",
228228
"message_loop_task_queues_merge_unmerge_unittests.cc",
229229
"message_loop_task_queues_unittests.cc",
230+
"message_loop_unittests.cc",
230231
"message_unittests.cc",
231232
"paths_unittests.cc",
232233
"platform/darwin/string_range_sanitization_unittests.mm",
@@ -243,10 +244,7 @@ executable("fml_unittests") {
243244

244245
# TODO(gw280): Figure out why these tests don't work currently on Fuchsia
245246
if (!is_fuchsia) {
246-
sources += [
247-
"file_unittest.cc",
248-
"message_loop_unittests.cc",
249-
]
247+
sources += [ "file_unittest.cc" ]
250248
}
251249

252250
deps = [

fml/message_loop_unittests.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "flutter/fml/build_config.h"
1111
#include "flutter/fml/concurrent_message_loop.h"
1212
#include "flutter/fml/message_loop.h"
13+
#include "flutter/fml/message_loop_impl.h"
1314
#include "flutter/fml/synchronization/count_down_latch.h"
1415
#include "flutter/fml/synchronization/waitable_event.h"
1516
#include "flutter/fml/task_runner.h"
@@ -309,3 +310,32 @@ TEST(MessageLoop, CanCreateConcurrentMessageLoop) {
309310
latch.Wait();
310311
ASSERT_GE(thread_ids.size(), 1u);
311312
}
313+
314+
TEST(MessageLoop, TIME_SENSITIVE(WakeUpTimersAreSingletons)) {
315+
auto loop_impl = fml::MessageLoopImpl::Create();
316+
317+
const auto t1 = fml::TimeDelta::FromMilliseconds(10);
318+
const auto t2 = fml::TimeDelta::FromMilliseconds(20);
319+
320+
const auto begin = fml::TimePoint::Now();
321+
322+
// Register a task scheduled for 10ms in the future. This schedules a
323+
// WakeUp call on the MessageLoopImpl with that fml::TimePoint
324+
loop_impl->PostTask(
325+
[&]() {
326+
auto delta = fml::TimePoint::Now() - begin;
327+
auto ms = delta.ToMillisecondsF();
328+
ASSERT_GE(ms, 18);
329+
ASSERT_LE(ms, 22);
330+
331+
loop_impl->Terminate();
332+
},
333+
fml::TimePoint::Now() + t1);
334+
335+
// Call WakeUp manually to change the WakeUp time to the future. If the
336+
// timer is correctly set up to be rearmed instead of a new timer scheduled,
337+
// the above task will be executed at t2 instead of t1 now.
338+
loop_impl->WakeUp(fml::TimePoint::Now() + t2);
339+
340+
loop_impl->Run();
341+
}

fml/platform/fuchsia/message_loop_fuchsia.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
#include "flutter/fml/platform/fuchsia/message_loop_fuchsia.h"
66

77
#include <lib/async-loop/default.h>
8-
#include <lib/async/cpp/task.h>
98
#include <lib/zx/time.h>
109

1110
namespace fml {
1211

1312
MessageLoopFuchsia::MessageLoopFuchsia()
14-
: loop_(&kAsyncLoopConfigAttachToCurrentThread) {}
13+
: loop_(&kAsyncLoopConfigAttachToCurrentThread) {
14+
auto handler = [this](async_dispatcher_t* dispatcher, async::Task* task,
15+
zx_status_t status) { RunExpiredTasksNow(); };
16+
task_.set_handler(handler);
17+
}
1518

1619
MessageLoopFuchsia::~MessageLoopFuchsia() = default;
1720

@@ -30,8 +33,12 @@ void MessageLoopFuchsia::WakeUp(fml::TimePoint time_point) {
3033
due_time = zx::nsec((time_point - now).ToNanoseconds());
3134
}
3235

33-
auto status = async::PostDelayedTask(
34-
loop_.dispatcher(), [this]() { RunExpiredTasksNow(); }, due_time);
36+
std::scoped_lock lock(task_mutex_);
37+
38+
auto status = task_.Cancel();
39+
FML_DCHECK(status == ZX_OK || status == ZX_ERR_NOT_FOUND);
40+
41+
status = task_.PostDelayed(loop_.dispatcher(), due_time);
3542
FML_DCHECK(status == ZX_OK);
3643
}
3744

fml/platform/fuchsia/message_loop_fuchsia.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define FLUTTER_FML_PLATFORM_FUCHSIA_MESSAGE_LOOP_FUCHSIA_H_
77

88
#include <lib/async-loop/cpp/loop.h>
9+
#include <lib/async/cpp/task.h>
910

1011
#include "flutter/fml/macros.h"
1112
#include "flutter/fml/message_loop_impl.h"
@@ -25,6 +26,8 @@ class MessageLoopFuchsia : public MessageLoopImpl {
2526
void WakeUp(fml::TimePoint time_point) override;
2627

2728
async::Loop loop_;
29+
std::mutex task_mutex_;
30+
async::Task task_;
2831

2932
FML_FRIEND_MAKE_REF_COUNTED(MessageLoopFuchsia);
3033
FML_FRIEND_REF_COUNTED_THREAD_SAFE(MessageLoopFuchsia);

0 commit comments

Comments
 (0)