Wrap concurrent message loop tasks in an autorelease pool on iOS/Mac platforms (flutter/engine#42459)

See https://github.com/flutter/flutter/issues/127482
This commit is contained in:
Jason Simmons
2023-06-02 12:58:43 -07:00
committed by GitHub
parent 195f28dd70
commit ea621b65bb
6 changed files with 65 additions and 14 deletions

View File

@@ -863,6 +863,7 @@ ORIGIN: ../../../flutter/fml/command_line.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/compiler_specific.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/concurrent_message_loop.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/concurrent_message_loop.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/concurrent_message_loop_factory.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/container.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/dart/dart_converter.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/dart/dart_converter.h + ../../../flutter/LICENSE
@@ -921,6 +922,7 @@ ORIGIN: ../../../flutter/fml/platform/android/scoped_java_ref.cc + ../../../flut
ORIGIN: ../../../flutter/fml/platform/android/scoped_java_ref.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/platform/darwin/cf_utils.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/platform/darwin/cf_utils.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/platform/darwin/concurrent_message_loop_factory.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/platform/darwin/message_loop_darwin.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/platform/darwin/message_loop_darwin.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/fml/platform/darwin/paths_darwin.mm + ../../../flutter/LICENSE
@@ -3534,6 +3536,7 @@ FILE: ../../../flutter/fml/command_line.h
FILE: ../../../flutter/fml/compiler_specific.h
FILE: ../../../flutter/fml/concurrent_message_loop.cc
FILE: ../../../flutter/fml/concurrent_message_loop.h
FILE: ../../../flutter/fml/concurrent_message_loop_factory.cc
FILE: ../../../flutter/fml/container.h
FILE: ../../../flutter/fml/dart/dart_converter.cc
FILE: ../../../flutter/fml/dart/dart_converter.h
@@ -3592,6 +3595,7 @@ FILE: ../../../flutter/fml/platform/android/scoped_java_ref.cc
FILE: ../../../flutter/fml/platform/android/scoped_java_ref.h
FILE: ../../../flutter/fml/platform/darwin/cf_utils.cc
FILE: ../../../flutter/fml/platform/darwin/cf_utils.h
FILE: ../../../flutter/fml/platform/darwin/concurrent_message_loop_factory.mm
FILE: ../../../flutter/fml/platform/darwin/message_loop_darwin.h
FILE: ../../../flutter/fml/platform/darwin/message_loop_darwin.mm
FILE: ../../../flutter/fml/platform/darwin/paths_darwin.mm

View File

@@ -131,9 +131,15 @@ source_set("fml") {
libs = []
if (is_ios || is_mac) {
sources += [ "platform/posix/shared_mutex_posix.cc" ]
sources += [
"platform/darwin/concurrent_message_loop_factory.mm",
"platform/posix/shared_mutex_posix.cc",
]
} else {
sources += [ "synchronization/shared_mutex_std.cc" ]
sources += [
"concurrent_message_loop_factory.cc",
"synchronization/shared_mutex_std.cc",
]
}
if (is_ios || is_mac) {

View File

@@ -11,12 +11,6 @@
namespace fml {
std::shared_ptr<ConcurrentMessageLoop> ConcurrentMessageLoop::Create(
size_t worker_count) {
return std::shared_ptr<ConcurrentMessageLoop>{
new ConcurrentMessageLoop(worker_count)};
}
ConcurrentMessageLoop::ConcurrentMessageLoop(size_t worker_count)
: worker_count_(std::max<size_t>(worker_count, 1ul)) {
for (size_t i = 0; i < worker_count_; ++i) {
@@ -60,7 +54,7 @@ void ConcurrentMessageLoop::PostTask(const fml::closure& task) {
<< "Tried to post a task to shutdown concurrent message "
"loop. The task will be executed on the callers thread.";
lock.unlock();
task();
ExecuteTask(task);
return;
}
@@ -103,12 +97,12 @@ void ConcurrentMessageLoop::WorkerMain() {
TRACE_EVENT0("flutter", "ConcurrentWorkerWake");
// Execute the primary task we woke up for.
if (task) {
task();
ExecuteTask(task);
}
// Execute any thread tasks.
for (const auto& thread_task : thread_tasks) {
thread_task();
ExecuteTask(thread_task);
}
if (shutdown_now) {
@@ -117,6 +111,10 @@ void ConcurrentMessageLoop::WorkerMain() {
}
}
void ConcurrentMessageLoop::ExecuteTask(const fml::closure& task) {
task();
}
void ConcurrentMessageLoop::Terminate() {
std::scoped_lock lock(tasks_mutex_);
shutdown_ = true;

View File

@@ -24,7 +24,7 @@ class ConcurrentMessageLoop
static std::shared_ptr<ConcurrentMessageLoop> Create(
size_t worker_count = std::thread::hardware_concurrency());
~ConcurrentMessageLoop();
virtual ~ConcurrentMessageLoop();
size_t GetWorkerCount() const;
@@ -36,6 +36,10 @@ class ConcurrentMessageLoop
bool RunsTasksOnCurrentThread();
protected:
explicit ConcurrentMessageLoop(size_t worker_count);
virtual void ExecuteTask(const fml::closure& task);
private:
friend ConcurrentTaskRunner;
@@ -48,8 +52,6 @@ class ConcurrentMessageLoop
std::map<std::thread::id, std::vector<fml::closure>> thread_tasks_;
bool shutdown_ = false;
explicit ConcurrentMessageLoop(size_t worker_count);
void WorkerMain();
void PostTask(const fml::closure& task);

View File

@@ -0,0 +1,15 @@
// Copyright 2013 The Flutter 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/fml/concurrent_message_loop.h"
namespace fml {
std::shared_ptr<ConcurrentMessageLoop> ConcurrentMessageLoop::Create(
size_t worker_count) {
return std::shared_ptr<ConcurrentMessageLoop>{
new ConcurrentMessageLoop(worker_count)};
}
} // namespace fml

View File

@@ -0,0 +1,26 @@
// Copyright 2013 The Flutter 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/fml/concurrent_message_loop.h"
namespace fml {
class ConcurrentMessageLoopDarwin : public ConcurrentMessageLoop {
friend class ConcurrentMessageLoop;
protected:
explicit ConcurrentMessageLoopDarwin(size_t worker_count) : ConcurrentMessageLoop(worker_count) {}
void ExecuteTask(const fml::closure& task) override {
@autoreleasepool {
task();
}
}
};
std::shared_ptr<ConcurrentMessageLoop> ConcurrentMessageLoop::Create(size_t worker_count) {
return std::shared_ptr<ConcurrentMessageLoop>{new ConcurrentMessageLoopDarwin(worker_count)};
}
} // namespace fml