Skip to content

Commit fd58a2f

Browse files
authored
Revert "Allow embedders to schedule a callback on all engine managed threads. (flutter-team-archive#15980)"
This reverts commit 3e7a4ca.
1 parent 51e583b commit fd58a2f

10 files changed

Lines changed: 17 additions & 345 deletions

File tree

fml/concurrent_message_loop.cc

Lines changed: 12 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ ConcurrentMessageLoop::ConcurrentMessageLoop(size_t worker_count)
2626
WorkerMain();
2727
});
2828
}
29-
30-
for (const auto& worker : workers_) {
31-
worker_thread_ids_.emplace_back(worker.get_id());
32-
}
3329
}
3430

3531
ConcurrentMessageLoop::~ConcurrentMessageLoop() {
@@ -77,43 +73,25 @@ void ConcurrentMessageLoop::PostTask(const fml::closure& task) {
7773
void ConcurrentMessageLoop::WorkerMain() {
7874
while (true) {
7975
std::unique_lock lock(tasks_mutex_);
80-
tasks_condition_.wait(lock, [&]() {
81-
return tasks_.size() > 0 || shutdown_ || HasThreadTasksLocked();
82-
});
83-
84-
// Shutdown cannot be read with the task mutex unlocked.
85-
bool shutdown_now = shutdown_;
86-
fml::closure task;
87-
std::vector<fml::closure> thread_tasks;
76+
tasks_condition_.wait(lock,
77+
[&]() { return tasks_.size() > 0 || shutdown_; });
8878

89-
if (tasks_.size() != 0) {
90-
task = tasks_.front();
91-
tasks_.pop();
79+
if (tasks_.size() == 0) {
80+
// This can only be caused by shutdown.
81+
FML_DCHECK(shutdown_);
82+
break;
9283
}
9384

94-
if (HasThreadTasksLocked()) {
95-
thread_tasks = GetThreadTasksLocked();
96-
FML_DCHECK(!HasThreadTasksLocked());
97-
}
85+
auto task = tasks_.front();
86+
tasks_.pop();
9887

99-
// Don't hold onto the mutex while tasks are being executed as they could
100-
// themselves try to post more tasks to the message loop.
88+
// Don't hold onto the mutex while the task is being executed as it could
89+
// itself try to post another tasks to this message loop.
10190
lock.unlock();
10291

10392
TRACE_EVENT0("flutter", "ConcurrentWorkerWake");
104-
// Execute the primary task we woke up for.
105-
if (task) {
106-
task();
107-
}
108-
109-
// Execute any thread tasks.
110-
for (const auto& thread_task : thread_tasks) {
111-
thread_task();
112-
}
113-
114-
if (shutdown_now) {
115-
break;
116-
}
93+
// Execute the one tasks we woke up for.
94+
task();
11795
}
11896
}
11997

@@ -123,31 +101,6 @@ void ConcurrentMessageLoop::Terminate() {
123101
tasks_condition_.notify_all();
124102
}
125103

126-
void ConcurrentMessageLoop::PostTaskToAllWorkers(fml::closure task) {
127-
if (!task) {
128-
return;
129-
}
130-
131-
std::scoped_lock lock(tasks_mutex_);
132-
for (const auto& worker_thread_id : worker_thread_ids_) {
133-
thread_tasks_[worker_thread_id].emplace_back(task);
134-
}
135-
tasks_condition_.notify_all();
136-
}
137-
138-
bool ConcurrentMessageLoop::HasThreadTasksLocked() const {
139-
return thread_tasks_.count(std::this_thread::get_id()) > 0;
140-
}
141-
142-
std::vector<fml::closure> ConcurrentMessageLoop::GetThreadTasksLocked() {
143-
auto found = thread_tasks_.find(std::this_thread::get_id());
144-
FML_DCHECK(found != thread_tasks_.end());
145-
std::vector<fml::closure> pending_tasks;
146-
std::swap(pending_tasks, found->second);
147-
thread_tasks_.erase(found);
148-
return pending_tasks;
149-
}
150-
151104
ConcurrentTaskRunner::ConcurrentTaskRunner(
152105
std::weak_ptr<ConcurrentMessageLoop> weak_loop)
153106
: weak_loop_(std::move(weak_loop)) {}

fml/concurrent_message_loop.h

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

88
#include <condition_variable>
9-
#include <map>
109
#include <queue>
1110
#include <thread>
1211

@@ -31,8 +30,6 @@ class ConcurrentMessageLoop
3130

3231
void Terminate();
3332

34-
void PostTaskToAllWorkers(fml::closure task);
35-
3633
private:
3734
friend ConcurrentTaskRunner;
3835

@@ -41,8 +38,6 @@ class ConcurrentMessageLoop
4138
std::mutex tasks_mutex_;
4239
std::condition_variable tasks_condition_;
4340
std::queue<fml::closure> tasks_;
44-
std::vector<std::thread::id> worker_thread_ids_;
45-
std::map<std::thread::id, std::vector<fml::closure>> thread_tasks_;
4641
bool shutdown_ = false;
4742

4843
ConcurrentMessageLoop(size_t worker_count);
@@ -51,10 +46,6 @@ class ConcurrentMessageLoop
5146

5247
void PostTask(const fml::closure& task);
5348

54-
bool HasThreadTasksLocked() const;
55-
56-
std::vector<fml::closure> GetThreadTasksLocked();
57-
5849
FML_DISALLOW_COPY_AND_ASSIGN(ConcurrentMessageLoop);
5950
};
6051

runtime/dart_vm.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,4 @@ DartVM::GetConcurrentWorkerTaskRunner() const {
504504
return concurrent_message_loop_->GetTaskRunner();
505505
}
506506

507-
std::shared_ptr<fml::ConcurrentMessageLoop> DartVM::GetConcurrentMessageLoop() {
508-
return concurrent_message_loop_;
509-
}
510-
511507
} // namespace flutter

runtime/dart_vm.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,6 @@ class DartVM {
147147
std::shared_ptr<fml::ConcurrentTaskRunner> GetConcurrentWorkerTaskRunner()
148148
const;
149149

150-
//----------------------------------------------------------------------------
151-
/// @brief The concurrent message loop hosts threads that are used by the
152-
/// engine to perform tasks long running background tasks.
153-
/// Typically, to post tasks to this message loop, the
154-
/// `GetConcurrentWorkerTaskRunner` method may be used.
155-
///
156-
/// @see GetConcurrentWorkerTaskRunner
157-
///
158-
/// @return The concurrent message loop used by this running Dart VM
159-
/// instance.
160-
///
161-
std::shared_ptr<fml::ConcurrentMessageLoop> GetConcurrentMessageLoop();
162-
163150
private:
164151
const Settings settings_;
165152
std::shared_ptr<fml::ConcurrentMessageLoop> concurrent_message_loop_;

shell/common/shell.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -352,14 +352,6 @@ class Shell final : public PlatformView::Delegate,
352352
/// @brief Accessor for the disable GPU SyncSwitch
353353
std::shared_ptr<fml::SyncSwitch> GetIsGpuDisabledSyncSwitch() const;
354354

355-
//----------------------------------------------------------------------------
356-
/// @brief Get a pointer to the Dart VM used by this running shell
357-
/// instance.
358-
///
359-
/// @return The Dart VM pointer.
360-
///
361-
DartVM* GetDartVM();
362-
363355
private:
364356
using ServiceProtocolHandler =
365357
std::function<bool(const ServiceProtocol::Handler::ServiceProtocolMap&,
@@ -432,6 +424,8 @@ class Shell final : public PlatformView::Delegate,
432424
std::unique_ptr<Rasterizer> rasterizer,
433425
std::unique_ptr<ShellIOManager> io_manager);
434426

427+
DartVM* GetDartVM();
428+
435429
void ReportTimings();
436430

437431
// |PlatformView::Delegate|

shell/platform/embedder/embedder.cc

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,7 @@ FlutterEngineResult FlutterEnginePostDartObject(
17241724
return kSuccess;
17251725
}
17261726

1727+
FLUTTER_EXPORT
17271728
FlutterEngineResult FlutterEngineNotifyLowMemoryWarning(
17281729
FLUTTER_API_SYMBOL(FlutterEngine) raw_engine) {
17291730
auto engine = reinterpret_cast<flutter::EmbedderEngine*>(raw_engine);
@@ -1746,27 +1747,3 @@ FlutterEngineResult FlutterEngineNotifyLowMemoryWarning(
17461747
kInternalInconsistency,
17471748
"Could not dispatch the low memory notification message.");
17481749
}
1749-
1750-
FlutterEngineResult FlutterEnginePostCallbackOnAllNativeThreads(
1751-
FLUTTER_API_SYMBOL(FlutterEngine) engine,
1752-
FlutterNativeThreadCallback callback,
1753-
void* user_data) {
1754-
if (engine == nullptr) {
1755-
return LOG_EMBEDDER_ERROR(kInvalidArguments, "Invalid engine handle.");
1756-
}
1757-
1758-
if (callback == nullptr) {
1759-
return LOG_EMBEDDER_ERROR(kInvalidArguments,
1760-
"Invalid native thread callback.");
1761-
}
1762-
1763-
return reinterpret_cast<flutter::EmbedderEngine*>(engine)
1764-
->PostTaskOnEngineManagedNativeThreads(
1765-
[callback, user_data](FlutterNativeThreadType type) {
1766-
callback(type, user_data);
1767-
})
1768-
? kSuccess
1769-
: LOG_EMBEDDER_ERROR(kInvalidArguments,
1770-
"Internal error while attempting to post "
1771-
"tasks to all threads.");
1772-
}

shell/platform/embedder/embedder.h

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -934,31 +934,6 @@ typedef struct {
934934
};
935935
} FlutterEngineDartObject;
936936

937-
/// This enum allows embedders to determine the type of the engine thread in the
938-
/// FlutterNativeThreadCallback. Based on the thread type, the embedder may be
939-
/// able to tweak the thread priorities for optimum performance.
940-
typedef enum {
941-
/// The Flutter Engine considers the thread on which the FlutterEngineRun call
942-
/// is made to be the platform thread. There is only one such thread per
943-
/// engine instance.
944-
kFlutterNativeThreadTypePlatform,
945-
/// This is the thread the Flutter Engine uses to execute rendering commands
946-
/// based on the selected client rendering API. There is only one such thread
947-
/// per engine instance.
948-
kFlutterNativeThreadTypeRender,
949-
/// This is a dedicated thread on which the root Dart isolate is serviced.
950-
/// There is only one such thread per engine instance.
951-
kFlutterNativeThreadTypeUI,
952-
/// Multiple threads are used by the Flutter engine to perform long running
953-
/// background tasks.
954-
kFlutterNativeThreadTypeWorker,
955-
} FlutterNativeThreadType;
956-
957-
/// A callback made by the engine in response to
958-
/// `FlutterEnginePostCallbackOnAllNativeThreads` on all internal thread.
959-
typedef void (*FlutterNativeThreadCallback)(FlutterNativeThreadType type,
960-
void* user_data);
961-
962937
typedef struct {
963938
/// The size of this struct. Must be sizeof(FlutterProjectArgs).
964939
size_t struct_size;
@@ -1692,45 +1667,6 @@ FLUTTER_EXPORT
16921667
FlutterEngineResult FlutterEngineNotifyLowMemoryWarning(
16931668
FLUTTER_API_SYMBOL(FlutterEngine) engine);
16941669

1695-
//------------------------------------------------------------------------------
1696-
/// @brief Schedule a callback to be run on all engine managed threads.
1697-
/// The engine will attempt to service this callback the next time
1698-
/// the message loop for each managed thread is idle. Since the
1699-
/// engine manages the entire lifecycle of multiple threads, there
1700-
/// is no opportunity for the embedders to finely tune the
1701-
/// priorities of threads directly, or, perform other thread
1702-
/// specific configuration (for example, setting thread names for
1703-
/// tracing). This callback gives embedders a chance to affect such
1704-
/// tuning.
1705-
///
1706-
/// @attention This call is expensive and must be made as few times as
1707-
/// possible. The callback must also return immediately as not doing
1708-
/// so may risk performance issues (especially for callbacks of type
1709-
/// kFlutterNativeThreadTypeUI and kFlutterNativeThreadTypeRender).
1710-
///
1711-
/// @attention Some callbacks (especially the ones of type
1712-
/// kFlutterNativeThreadTypeWorker) may be called after the
1713-
/// FlutterEngine instance has shut down. Embedders must be careful
1714-
/// in handling the lifecycle of objects associated with the user
1715-
/// data baton.
1716-
///
1717-
/// @attention In case there are multiple running Flutter engine instances,
1718-
/// their workers are shared.
1719-
///
1720-
/// @param[in] engine A running engine instance.
1721-
/// @param[in] callback The callback that will get called multiple times on
1722-
/// each engine managed thread.
1723-
/// @param[in] user_data A baton passed by the engine to the callback. This
1724-
/// baton is not interpreted by the engine in any way.
1725-
///
1726-
/// @return Returns if the callback was successfully posted to all threads.
1727-
///
1728-
FLUTTER_EXPORT
1729-
FlutterEngineResult FlutterEnginePostCallbackOnAllNativeThreads(
1730-
FLUTTER_API_SYMBOL(FlutterEngine) engine,
1731-
FlutterNativeThreadCallback callback,
1732-
void* user_data);
1733-
17341670
#if defined(__cplusplus)
17351671
} // extern "C"
17361672
#endif

shell/platform/embedder/embedder_engine.cc

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -247,34 +247,7 @@ bool EmbedderEngine::RunTask(const FlutterTask* task) {
247247
task->task);
248248
}
249249

250-
bool EmbedderEngine::PostTaskOnEngineManagedNativeThreads(
251-
std::function<void(FlutterNativeThreadType)> closure) const {
252-
if (!IsValid() || closure == nullptr) {
253-
return false;
254-
}
255-
256-
const auto trampoline = [closure](FlutterNativeThreadType type,
257-
fml::RefPtr<fml::TaskRunner> runner) {
258-
runner->PostTask([closure, type] { closure(type); });
259-
};
260-
261-
// Post the task to all thread host threads.
262-
const auto& task_runners = shell_->GetTaskRunners();
263-
trampoline(kFlutterNativeThreadTypeRender, task_runners.GetGPUTaskRunner());
264-
trampoline(kFlutterNativeThreadTypeWorker, task_runners.GetIOTaskRunner());
265-
trampoline(kFlutterNativeThreadTypeUI, task_runners.GetUITaskRunner());
266-
trampoline(kFlutterNativeThreadTypePlatform,
267-
task_runners.GetPlatformTaskRunner());
268-
269-
// Post the task to all worker threads.
270-
auto vm = shell_->GetDartVM();
271-
vm->GetConcurrentMessageLoop()->PostTaskToAllWorkers(
272-
[closure]() { closure(kFlutterNativeThreadTypeWorker); });
273-
274-
return true;
275-
}
276-
277-
Shell& EmbedderEngine::GetShell() {
250+
const Shell& EmbedderEngine::GetShell() const {
278251
FML_DCHECK(shell_);
279252
return *shell_.get();
280253
}

shell/platform/embedder/embedder_engine.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ class EmbedderEngine {
8080

8181
bool RunTask(const FlutterTask* task);
8282

83-
bool PostTaskOnEngineManagedNativeThreads(
84-
std::function<void(FlutterNativeThreadType)> closure) const;
85-
86-
Shell& GetShell();
83+
const Shell& GetShell() const;
8784

8885
private:
8986
const std::unique_ptr<EmbedderThreadHost> thread_host_;

0 commit comments

Comments
 (0)