I am reading the code in flutter engine at
https://github.com/flutter/engine/blob/main/shell/common/platform_view.cc#L58
I want to ask that if latch.Signal()
is reached before latch.Wait()
, will it wait indefinitely? If this case will never happen, how it is ensured?
void PlatformView::NotifyCreated() {
std::unique_ptr<Surface> surface;
// Threading: We want to use the platform view on the non-platform thread.
// Using the weak pointer is illegal. But, we are going to introduce a latch
// so that the platform view is not collected till the surface is obtained.
auto* platform_view = this;
fml::ManualResetWaitableEvent latch;
fml::TaskRunner::RunNowOrPostTask(
task_runners_.GetRasterTaskRunner(), [platform_view, &surface, &latch]() {
surface = platform_view->CreateRenderingSurface();
if (surface && !surface->IsValid()) {
surface.reset();
}
latch.Signal();
});
latch.Wait();
if (!surface) {
FML_LOG(ERROR) << "Failed to create platform view rendering surface";
return;
}
delegate_.OnPlatformViewCreated(std::move(surface));
}
explain the code detail and some more information about the mutex it uses. Analyze CPU scheduling possibilities.
if you look at its current implementation
the latch has a boolean that it sets when signaled, then when Wait
happens later it sees the boolean was already set and it doesn't wait.