From 9cacbff47a6e43de95a77ba791cf65a956a74b6c Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Fri, 9 Mar 2018 11:19:23 -0800 Subject: [PATCH] Switch from fxl::Mutex to std::mutex (flutter/engine#4764) We're going to remove fxl::Mutex soon. --- engine/src/flutter/flow/export_node.h | 2 -- engine/src/flutter/fml/message_loop_impl.cc | 6 +++--- engine/src/flutter/fml/message_loop_impl.h | 9 ++++----- .../src/flutter/lib/ui/painting/resource_context.cc | 13 +++++++------ engine/src/flutter/lib/ui/painting/utils.cc | 4 ++-- engine/src/flutter/lib/ui/painting/utils.h | 8 ++++---- engine/src/flutter/shell/common/shell.cc | 9 +++++---- engine/src/flutter/shell/common/shell.h | 8 +++----- engine/src/flutter/synchronization/pipeline.h | 12 +++++------- 9 files changed, 33 insertions(+), 38 deletions(-) diff --git a/engine/src/flutter/flow/export_node.h b/engine/src/flutter/flow/export_node.h index bf14a04192..24f94fcebf 100644 --- a/engine/src/flutter/flow/export_node.h +++ b/engine/src/flutter/flow/export_node.h @@ -14,8 +14,6 @@ #include "lib/fxl/build_config.h" #include "lib/fxl/macros.h" #include "lib/fxl/memory/ref_counted.h" -#include "lib/fxl/synchronization/mutex.h" -#include "lib/fxl/synchronization/thread_annotations.h" #include "lib/ui/scenic/client/resources.h" #include "third_party/skia/include/core/SkPoint.h" diff --git a/engine/src/flutter/fml/message_loop_impl.cc b/engine/src/flutter/fml/message_loop_impl.cc index 8ccf508376..cec9b7e448 100644 --- a/engine/src/flutter/fml/message_loop_impl.cc +++ b/engine/src/flutter/fml/message_loop_impl.cc @@ -94,7 +94,7 @@ void MessageLoopImpl::DoRun() { // should be destructed on the message loop's thread. We have just returned // from the implementations |Run| method which we know is on the correct // thread. Drop all pending tasks on the floor. - fxl::MutexLocker lock(&delayed_tasks_mutex_); + std::lock_guard lock(delayed_tasks_mutex_); delayed_tasks_ = {}; } @@ -111,7 +111,7 @@ void MessageLoopImpl::RegisterTask(fxl::Closure task, // |task| synchronously within this function. return; } - fxl::MutexLocker lock(&delayed_tasks_mutex_); + std::lock_guard lock(delayed_tasks_mutex_); delayed_tasks_.push({++order_, std::move(task), target_time}); WakeUp(delayed_tasks_.top().target_time); } @@ -121,7 +121,7 @@ void MessageLoopImpl::RunExpiredTasks() { std::vector invocations; { - fxl::MutexLocker lock(&delayed_tasks_mutex_); + std::lock_guard lock(delayed_tasks_mutex_); if (delayed_tasks_.empty()) { return; diff --git a/engine/src/flutter/fml/message_loop_impl.h b/engine/src/flutter/fml/message_loop_impl.h index 551c8ca064..bfdb2064cb 100644 --- a/engine/src/flutter/fml/message_loop_impl.h +++ b/engine/src/flutter/fml/message_loop_impl.h @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -15,8 +16,6 @@ #include "lib/fxl/functional/closure.h" #include "lib/fxl/macros.h" #include "lib/fxl/memory/ref_counted.h" -#include "lib/fxl/synchronization/mutex.h" -#include "lib/fxl/synchronization/thread_annotations.h" #include "lib/fxl/time/time_point.h" namespace fml { @@ -73,9 +72,9 @@ class MessageLoopImpl : public fxl::RefCountedThreadSafe { priority_queue, DelayedTaskCompare>; std::set task_observers_; - fxl::Mutex delayed_tasks_mutex_; - DelayedTaskQueue delayed_tasks_ FXL_GUARDED_BY(delayed_tasks_mutex_); - size_t order_ FXL_GUARDED_BY(delayed_tasks_mutex_); + std::mutex delayed_tasks_mutex_; + DelayedTaskQueue delayed_tasks_; + size_t order_; std::atomic_bool terminated_; void RegisterTask(fxl::Closure task, fxl::TimePoint target_time); diff --git a/engine/src/flutter/lib/ui/painting/resource_context.cc b/engine/src/flutter/lib/ui/painting/resource_context.cc index 7ec6a90f34..1b9f71548c 100644 --- a/engine/src/flutter/lib/ui/painting/resource_context.cc +++ b/engine/src/flutter/lib/ui/painting/resource_context.cc @@ -4,24 +4,25 @@ #include "flutter/lib/ui/painting/resource_context.h" +#include + #include "lib/fxl/logging.h" -#include "lib/fxl/synchronization/mutex.h" namespace blink { namespace { static GrContext* g_context = nullptr; -static fxl::Mutex g_mutex; +static std::mutex g_mutex; static volatile bool g_freeze = false; } // namespace ResourceContext::ResourceContext() { - g_mutex.Lock(); + g_mutex.lock(); } ResourceContext::~ResourceContext() { - g_mutex.Unlock(); + g_mutex.unlock(); } void ResourceContext::Set(sk_sp context) { @@ -38,12 +39,12 @@ std::unique_ptr ResourceContext::Acquire() { } void ResourceContext::Freeze() { - fxl::MutexLocker lock(&g_mutex); + std::lock_guard lock(g_mutex); g_freeze = true; } void ResourceContext::Unfreeze() { - fxl::MutexLocker lock(&g_mutex); + std::lock_guard lock(g_mutex); g_freeze = false; } diff --git a/engine/src/flutter/lib/ui/painting/utils.cc b/engine/src/flutter/lib/ui/painting/utils.cc index 6bffd565d7..b3f87135ac 100644 --- a/engine/src/flutter/lib/ui/painting/utils.cc +++ b/engine/src/flutter/lib/ui/painting/utils.cc @@ -22,7 +22,7 @@ SkiaUnrefQueue& SkiaUnrefQueue::Get() { } void SkiaUnrefQueue::Unref(SkRefCnt* object) { - fxl::MutexLocker lock(&mutex_); + std::lock_guard lock(mutex_); objects_.push_back(object); if (!drain_pending_) { drain_pending_ = true; @@ -33,7 +33,7 @@ void SkiaUnrefQueue::Unref(SkRefCnt* object) { void SkiaUnrefQueue::Drain() { std::deque skia_objects; { - fxl::MutexLocker lock(&mutex_); + std::lock_guard lock(mutex_); objects_.swap(skia_objects); drain_pending_ = false; } diff --git a/engine/src/flutter/lib/ui/painting/utils.h b/engine/src/flutter/lib/ui/painting/utils.h index 6802171337..02782c6796 100644 --- a/engine/src/flutter/lib/ui/painting/utils.h +++ b/engine/src/flutter/lib/ui/painting/utils.h @@ -2,9 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "lib/fxl/synchronization/mutex.h" #include "third_party/skia/include/core/SkRefCnt.h" +#include #include namespace blink { @@ -22,9 +22,9 @@ class SkiaUnrefQueue { static SkiaUnrefQueue instance_; - fxl::Mutex mutex_; - std::deque objects_ FXL_GUARDED_BY(mutex_); - bool drain_pending_ FXL_GUARDED_BY(mutex_); + std::mutex mutex_; + std::deque objects_; + bool drain_pending_; }; template diff --git a/engine/src/flutter/shell/common/shell.cc b/engine/src/flutter/shell/common/shell.cc index 3b56c6a46b..f32356a4e4 100644 --- a/engine/src/flutter/shell/common/shell.cc +++ b/engine/src/flutter/shell/common/shell.cc @@ -84,7 +84,8 @@ void Shell::InitStandalone(fxl::CommandLine command_line, fml::icu::InitializeICU(icu_data_path); - if (!command_line.HasOption(FlagForSwitch(Switch::SkiaDeterministicRendering))) + if (!command_line.HasOption( + FlagForSwitch(Switch::SkiaDeterministicRendering))) SkGraphics::Init(); blink::Settings settings; @@ -203,7 +204,7 @@ void Shell::AddPlatformView(PlatformView* platform_view) { if (platform_view == nullptr) { return; } - fxl::MutexLocker lock(&platform_views_mutex_); + std::lock_guard lock(platform_views_mutex_); platform_views_.insert(platform_view); } @@ -211,7 +212,7 @@ void Shell::RemovePlatformView(PlatformView* platform_view) { if (platform_view == nullptr) { return; } - fxl::MutexLocker lock(&platform_views_mutex_); + std::lock_guard lock(platform_views_mutex_); platform_views_.erase(platform_view); } @@ -220,7 +221,7 @@ void Shell::IteratePlatformViews( if (iterator == nullptr) { return; } - fxl::MutexLocker lock(&platform_views_mutex_); + std::lock_guard lock(platform_views_mutex_); for (PlatformView* view : platform_views_) { if (!iterator(view)) { return; diff --git a/engine/src/flutter/shell/common/shell.h b/engine/src/flutter/shell/common/shell.h index 96cf3a2fa0..92c315dcf2 100644 --- a/engine/src/flutter/shell/common/shell.h +++ b/engine/src/flutter/shell/common/shell.h @@ -5,6 +5,7 @@ #ifndef SHELL_COMMON_SHELL_H_ #define SHELL_COMMON_SHELL_H_ +#include #include #include "flutter/fml/thread.h" @@ -14,8 +15,6 @@ #include "lib/fxl/macros.h" #include "lib/fxl/memory/ref_ptr.h" #include "lib/fxl/memory/weak_ptr.h" -#include "lib/fxl/synchronization/mutex.h" -#include "lib/fxl/synchronization/thread_annotations.h" #include "lib/fxl/synchronization/thread_checker.h" #include "lib/fxl/synchronization/waitable_event.h" #include "lib/fxl/tasks/task_runner.h" @@ -68,9 +67,8 @@ class Shell { std::unique_ptr gpu_thread_checker_; std::unique_ptr ui_thread_checker_; TracingController tracing_controller_; - mutable fxl::Mutex platform_views_mutex_; - std::unordered_set platform_views_ - FXL_GUARDED_BY(platform_views_mutex_); + mutable std::mutex platform_views_mutex_; + std::unordered_set platform_views_; static void Init(fxl::CommandLine command_line, const std::string& bundle_path); diff --git a/engine/src/flutter/synchronization/pipeline.h b/engine/src/flutter/synchronization/pipeline.h index 9897b20a4f..3b17fb2c4d 100644 --- a/engine/src/flutter/synchronization/pipeline.h +++ b/engine/src/flutter/synchronization/pipeline.h @@ -11,10 +11,9 @@ #include "lib/fxl/functional/closure.h" #include "lib/fxl/macros.h" #include "lib/fxl/memory/ref_counted.h" -#include "lib/fxl/synchronization/mutex.h" -#include "lib/fxl/synchronization/thread_annotations.h" #include +#include #include namespace flutter { @@ -120,7 +119,7 @@ class Pipeline : public fxl::RefCountedThreadSafe> { size_t items_count = 0; { - fxl::MutexLocker lock(&queue_mutex_); + std::lock_guard lock(queue_mutex_); std::tie(resource, trace_id) = std::move(queue_.front()); queue_.pop(); items_count = queue_.size(); @@ -142,14 +141,13 @@ class Pipeline : public fxl::RefCountedThreadSafe> { private: Semaphore empty_; Semaphore available_; - fxl::Mutex queue_mutex_; - std::queue> queue_ - FXL_GUARDED_BY(queue_mutex_); + std::mutex queue_mutex_; + std::queue> queue_; std::atomic_size_t last_trace_id_; void ProducerCommit(ResourcePtr resource, size_t trace_id) { { - fxl::MutexLocker lock(&queue_mutex_); + std::lock_guard lock(queue_mutex_); queue_.emplace(std::move(resource), trace_id); }