forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support message loops whose tasks are executed concurrently. (flutter…
…#8419) The number of workers depends on what the platform deem appropriate for the system at runtime.
- Loading branch information
1 parent
8ae84ec
commit ca1d163
Showing
13 changed files
with
195 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// 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" | ||
|
||
#include <algorithm> | ||
|
||
#include "flutter/fml/thread.h" | ||
#include "flutter/fml/trace_event.h" | ||
|
||
namespace fml { | ||
|
||
ConcurrentMessageLoop::ConcurrentMessageLoop() | ||
: worker_count_(std::max(std::thread::hardware_concurrency(), 1u)), | ||
shutdown_latch_(worker_count_), | ||
shutdown_(false) { | ||
for (size_t i = 0; i < worker_count_; ++i) { | ||
workers_.emplace_back([i, this]() { | ||
fml::Thread::SetCurrentThreadName( | ||
std::string{"io.flutter.worker." + std::to_string(i + 1)}); | ||
WorkerMain(); | ||
}); | ||
} | ||
} | ||
|
||
ConcurrentMessageLoop::~ConcurrentMessageLoop() { | ||
Terminate(); | ||
shutdown_latch_.Wait(); | ||
for (auto& worker : workers_) { | ||
worker.join(); | ||
} | ||
} | ||
|
||
// |fml::MessageLoopImpl| | ||
void ConcurrentMessageLoop::Run() { | ||
FML_CHECK(false); | ||
} | ||
|
||
// |fml::MessageLoopImpl| | ||
void ConcurrentMessageLoop::Terminate() { | ||
std::lock_guard<std::mutex> lock(wait_condition_mutex_); | ||
shutdown_ = true; | ||
wait_condition_.notify_all(); | ||
} | ||
|
||
// |fml::MessageLoopImpl| | ||
void ConcurrentMessageLoop::WakeUp(fml::TimePoint time_point) { | ||
// Assume that the clocks are not the same. | ||
const auto duration = std::chrono::nanoseconds( | ||
(time_point - fml::TimePoint::Now()).ToNanoseconds()); | ||
next_wake_ = std::chrono::high_resolution_clock::now() + duration; | ||
wait_condition_.notify_all(); | ||
} | ||
|
||
void ConcurrentMessageLoop::WorkerMain() { | ||
while (!shutdown_) { | ||
std::unique_lock<std::mutex> lock(wait_condition_mutex_); | ||
if (!shutdown_) { | ||
wait_condition_.wait(lock); | ||
} | ||
TRACE_EVENT0("fml", "ConcurrentWorkerWake"); | ||
RunSingleExpiredTaskNow(); | ||
} | ||
|
||
RunExpiredTasksNow(); | ||
shutdown_latch_.CountDown(); | ||
} | ||
|
||
} // namespace fml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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. | ||
|
||
#ifndef FLUTTER_FML_CONCURRENT_MESSAGE_LOOP_H_ | ||
#define FLUTTER_FML_CONCURRENT_MESSAGE_LOOP_H_ | ||
|
||
#include <atomic> | ||
#include <chrono> | ||
#include <condition_variable> | ||
#include <thread> | ||
#include <vector> | ||
|
||
#include "flutter/fml/macros.h" | ||
#include "flutter/fml/message_loop_impl.h" | ||
#include "flutter/fml/synchronization/count_down_latch.h" | ||
#include "flutter/fml/synchronization/thread_annotations.h" | ||
|
||
namespace fml { | ||
|
||
class ConcurrentMessageLoop : public MessageLoopImpl { | ||
private: | ||
const size_t worker_count_; | ||
std::mutex wait_condition_mutex_; | ||
std::condition_variable wait_condition_; | ||
std::vector<std::thread> workers_; | ||
CountDownLatch shutdown_latch_; | ||
std::chrono::high_resolution_clock::time_point next_wake_; | ||
std::atomic_bool shutdown_; | ||
|
||
ConcurrentMessageLoop(); | ||
|
||
~ConcurrentMessageLoop(); | ||
|
||
// |fml::MessageLoopImpl| | ||
void Run() override; | ||
|
||
// |fml::MessageLoopImpl| | ||
void Terminate() override; | ||
|
||
// |fml::MessageLoopImpl| | ||
void WakeUp(fml::TimePoint time_point) override; | ||
|
||
static void WorkerMain(ConcurrentMessageLoop* loop); | ||
|
||
void WorkerMain(); | ||
|
||
FML_FRIEND_MAKE_REF_COUNTED(ConcurrentMessageLoop); | ||
FML_FRIEND_REF_COUNTED_THREAD_SAFE(ConcurrentMessageLoop); | ||
FML_DISALLOW_COPY_AND_ASSIGN(ConcurrentMessageLoop); | ||
}; | ||
|
||
} // namespace fml | ||
|
||
#endif // FLUTTER_FML_CONCURRENT_MESSAGE_LOOP_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters