From affd7c24a933c99aa158d8e600b6c2721a5a76be Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Wed, 20 Jul 2022 11:46:58 -0700 Subject: [PATCH] Wrap the global FFI dispatcher map initialization in a call_once (flutter/engine#34788) --- engine/src/flutter/lib/ui/dart_ui.cc | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/engine/src/flutter/lib/ui/dart_ui.cc b/engine/src/flutter/lib/ui/dart_ui.cc index 0cf6cf509a..758a28ff48 100644 --- a/engine/src/flutter/lib/ui/dart_ui.cc +++ b/engine/src/flutter/lib/ui/dart_ui.cc @@ -4,6 +4,7 @@ #include "flutter/lib/ui/dart_ui.h" +#include #include #include "flutter/common/settings.h" @@ -281,14 +282,14 @@ typedef CanvasPath Path; V(SemanticsUpdateBuilder, updateNode, 36) \ V(SemanticsUpdate, dispose, 1) -#define FFI_FUNCTION_INSERT(FUNCTION, ARGS) \ - function_dispatchers.insert(std::make_pair( \ - std::string_view(#FUNCTION), \ - reinterpret_cast( \ +#define FFI_FUNCTION_INSERT(FUNCTION, ARGS) \ + g_function_dispatchers.insert(std::make_pair( \ + std::string_view(#FUNCTION), \ + reinterpret_cast( \ tonic::FfiDispatcher::Call))); #define FFI_METHOD_INSERT(CLASS, METHOD, ARGS) \ - function_dispatchers.insert( \ + g_function_dispatchers.insert( \ std::make_pair(std::string_view(#CLASS "::" #METHOD), \ reinterpret_cast( \ tonic::FfiDispatcher function_dispatchers; +std::once_flag g_dispatchers_init_flag; +std::unordered_map g_function_dispatchers; void* ResolveFfiNativeFunction(const char* name, uintptr_t args) { - auto it = function_dispatchers.find(name); - return (it != function_dispatchers.end()) ? it->second : nullptr; + auto it = g_function_dispatchers.find(name); + return (it != g_function_dispatchers.end()) ? it->second : nullptr; } void InitDispatcherMap() { - if (!function_dispatchers.empty()) { - return; - } FFI_FUNCTION_LIST(FFI_FUNCTION_INSERT) FFI_METHOD_LIST(FFI_METHOD_INSERT) } @@ -314,7 +313,7 @@ void InitDispatcherMap() { } // anonymous namespace void DartUI::InitForIsolate(const Settings& settings) { - InitDispatcherMap(); + std::call_once(g_dispatchers_init_flag, InitDispatcherMap); auto dart_ui = Dart_LookupLibrary(ToDart("dart:ui")); if (Dart_IsError(dart_ui)) {