From 3f2d6e7863c291a272b002a5ec6c015ef3fb1f84 Mon Sep 17 00:00:00 2001 From: klaxxi <98382069+klaxxisegv@users.noreply.github.com> Date: Mon, 18 Jul 2022 15:08:04 +0800 Subject: [PATCH] remove lock and use raw ptr for MessageLoopTaskQueues (#34249) --- fml/message_loop_impl.h | 2 +- fml/message_loop_task_queues.cc | 30 ++++++------------- fml/message_loop_task_queues.h | 10 ++----- ...oop_task_queues_merge_unmerge_unittests.cc | 2 +- fml/shared_thread_merger.h | 2 +- 5 files changed, 14 insertions(+), 32 deletions(-) diff --git a/fml/message_loop_impl.h b/fml/message_loop_impl.h index 23a2eceb15c71..270cc3c86a9ad 100644 --- a/fml/message_loop_impl.h +++ b/fml/message_loop_impl.h @@ -64,7 +64,7 @@ class MessageLoopImpl : public Wakeable, MessageLoopImpl(); private: - fml::RefPtr task_queue_; + fml::MessageLoopTaskQueues* task_queue_; TaskQueueId queue_id_; std::atomic_bool terminated_; diff --git a/fml/message_loop_task_queues.cc b/fml/message_loop_task_queues.cc index 99d191438f47f..61f38751b5ebe 100644 --- a/fml/message_loop_task_queues.cc +++ b/fml/message_loop_task_queues.cc @@ -17,13 +17,8 @@ namespace fml { -std::mutex MessageLoopTaskQueues::creation_mutex_; - const size_t TaskQueueId::kUnmerged = ULONG_MAX; -// Guarded by creation_mutex_. -fml::RefPtr MessageLoopTaskQueues::instance_; - namespace { // iOS prior to version 9 prevents c++11 thread_local and __thread specifier, @@ -37,7 +32,6 @@ class TaskSourceGradeHolder { }; } // namespace -// Guarded by creation_mutex_. FML_THREAD_LOCAL ThreadLocalUniquePtr tls_task_source_grade; @@ -48,14 +42,9 @@ TaskQueueEntry::TaskQueueEntry(TaskQueueId created_for_arg) task_source = std::make_unique(created_for); } -fml::RefPtr MessageLoopTaskQueues::GetInstance() { - std::scoped_lock creation(creation_mutex_); - if (!instance_) { - instance_ = fml::MakeRefCounted(); - tls_task_source_grade.reset( - new TaskSourceGradeHolder{TaskSourceGrade::kUnspecified}); - } - return instance_; +MessageLoopTaskQueues* MessageLoopTaskQueues::GetInstance() { + static MessageLoopTaskQueues* instance = new MessageLoopTaskQueues; + return instance; } TaskQueueId MessageLoopTaskQueues::CreateTaskQueue() { @@ -67,7 +56,10 @@ TaskQueueId MessageLoopTaskQueues::CreateTaskQueue() { } MessageLoopTaskQueues::MessageLoopTaskQueues() - : task_queue_id_counter_(0), order_(0) {} + : task_queue_id_counter_(0), order_(0) { + tls_task_source_grade.reset( + new TaskSourceGradeHolder{TaskSourceGrade::kUnspecified}); +} MessageLoopTaskQueues::~MessageLoopTaskQueues() = default; @@ -95,7 +87,6 @@ void MessageLoopTaskQueues::DisposeTasks(TaskQueueId queue_id) { } TaskSourceGrade MessageLoopTaskQueues::GetCurrentTaskSourceGrade() { - std::scoped_lock creation(creation_mutex_); return tls_task_source_grade.get()->task_source_grade; } @@ -145,11 +136,8 @@ fml::closure MessageLoopTaskQueues::GetNextTaskToRun(TaskQueueId queue_id, fml::closure invocation = top.task.GetTask(); queue_entries_.at(top.task_queue_id) ->task_source->PopTask(top.task.GetTaskSourceGrade()); - { - std::scoped_lock creation(creation_mutex_); - const auto task_source_grade = top.task.GetTaskSourceGrade(); - tls_task_source_grade.reset(new TaskSourceGradeHolder{task_source_grade}); - } + const auto task_source_grade = top.task.GetTaskSourceGrade(); + tls_task_source_grade.reset(new TaskSourceGradeHolder{task_source_grade}); return invocation; } diff --git a/fml/message_loop_task_queues.h b/fml/message_loop_task_queues.h index 703c6bcec3b58..60a0a0f0080ef 100644 --- a/fml/message_loop_task_queues.h +++ b/fml/message_loop_task_queues.h @@ -62,12 +62,11 @@ enum class FlushType { /// This also wakes up the loop at the required times. /// \see fml::MessageLoop /// \see fml::Wakeable -class MessageLoopTaskQueues - : public fml::RefCountedThreadSafe { +class MessageLoopTaskQueues { public: // Lifecycle. - static fml::RefPtr GetInstance(); + static MessageLoopTaskQueues* GetInstance(); TaskQueueId CreateTaskQueue(); @@ -152,9 +151,6 @@ class MessageLoopTaskQueues fml::TimePoint GetNextWakeTimeUnlocked(TaskQueueId queue_id) const; - static std::mutex creation_mutex_; - static fml::RefPtr instance_; - mutable std::mutex queue_mutex_; std::map> queue_entries_; @@ -162,8 +158,6 @@ class MessageLoopTaskQueues std::atomic_int order_; - FML_FRIEND_MAKE_REF_COUNTED(MessageLoopTaskQueues); - FML_FRIEND_REF_COUNTED_THREAD_SAFE(MessageLoopTaskQueues); FML_DISALLOW_COPY_ASSIGN_AND_MOVE(MessageLoopTaskQueues); }; diff --git a/fml/message_loop_task_queues_merge_unmerge_unittests.cc b/fml/message_loop_task_queues_merge_unmerge_unittests.cc index 97f4926175ed8..ff16929b5a5c3 100644 --- a/fml/message_loop_task_queues_merge_unmerge_unittests.cc +++ b/fml/message_loop_task_queues_merge_unmerge_unittests.cc @@ -27,7 +27,7 @@ class TestWakeable : public fml::Wakeable { WakeUpCall wake_up_call_; }; -static int CountRemainingTasks(fml::RefPtr task_queue, +static int CountRemainingTasks(fml::MessageLoopTaskQueues* task_queue, const TaskQueueId& queue_id, bool run_invocation = false) { const auto now = ChronoTicksSinceEpoch(); diff --git a/fml/shared_thread_merger.h b/fml/shared_thread_merger.h index b278db4e93d99..3f8ae9a6367f8 100644 --- a/fml/shared_thread_merger.h +++ b/fml/shared_thread_merger.h @@ -57,7 +57,7 @@ class SharedThreadMerger private: fml::TaskQueueId owner_; fml::TaskQueueId subsumed_; - fml::RefPtr task_queues_; + fml::MessageLoopTaskQueues* task_queues_; std::mutex mutex_; bool enabled_;