From 7ec747b1291a2778ca522227ed11ebeecf46a7de Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 27 Jan 2020 17:40:51 -0800 Subject: [PATCH] Hold a mutex when updating all CanPostTaskToAllNativeThreads::Captures members. This is in the same vein as https://github.com/flutter/engine/pull/16081 but includes holding the mutex when updating all members in the Captures struct instead of just when tracking thread IDs. --- .../embedder/tests/embedder_unittests.cc | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/shell/platform/embedder/tests/embedder_unittests.cc b/shell/platform/embedder/tests/embedder_unittests.cc index c11b5883e99c3..f4115df3cbeaa 100644 --- a/shell/platform/embedder/tests/embedder_unittests.cc +++ b/shell/platform/embedder/tests/embedder_unittests.cc @@ -3942,8 +3942,12 @@ TEST_F(EmbedderTest, CanPostTaskToAllNativeThreads) { // Waits the adequate number of callbacks to fire. fml::CountDownLatch latch; + // This class will be accessed from multiple threads concurrently to track + // thread specific information that is later checked. All updates to fields + // in this struct must be made with this mutex acquired. + + std::mutex captures_mutex; // Ensures that the expect number of distinct threads were serviced. - std::mutex thread_ids_mutex; std::set thread_ids; size_t platform_threads_count = 0; @@ -3961,22 +3965,22 @@ TEST_F(EmbedderTest, CanPostTaskToAllNativeThreads) { engine.get(), [](FlutterNativeThreadType type, void* baton) { auto captures = reinterpret_cast(baton); - switch (type) { - case kFlutterNativeThreadTypeRender: - captures->render_threads_count++; - break; - case kFlutterNativeThreadTypeWorker: - captures->worker_threads_count++; - break; - case kFlutterNativeThreadTypeUI: - captures->ui_threads_count++; - break; - case kFlutterNativeThreadTypePlatform: - captures->platform_threads_count++; - break; - } { - std::scoped_lock lock(captures->thread_ids_mutex); + std::scoped_lock lock(captures->captures_mutex); + switch (type) { + case kFlutterNativeThreadTypeRender: + captures->render_threads_count++; + break; + case kFlutterNativeThreadTypeWorker: + captures->worker_threads_count++; + break; + case kFlutterNativeThreadTypeUI: + captures->ui_threads_count++; + break; + case kFlutterNativeThreadTypePlatform: + captures->platform_threads_count++; + break; + } captures->thread_ids.insert(std::this_thread::get_id()); } captures->latch.CountDown();