Skip to content

Commit

Permalink
remove lock and use raw ptr for MessageLoopTaskQueues (flutter#34249)
Browse files Browse the repository at this point in the history
  • Loading branch information
klaxxisegv authored Jul 18, 2022
1 parent 8bb06ba commit 3f2d6e7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 32 deletions.
2 changes: 1 addition & 1 deletion fml/message_loop_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class MessageLoopImpl : public Wakeable,
MessageLoopImpl();

private:
fml::RefPtr<MessageLoopTaskQueues> task_queue_;
fml::MessageLoopTaskQueues* task_queue_;
TaskQueueId queue_id_;

std::atomic_bool terminated_;
Expand Down
30 changes: 9 additions & 21 deletions fml/message_loop_task_queues.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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> MessageLoopTaskQueues::instance_;

namespace {

// iOS prior to version 9 prevents c++11 thread_local and __thread specifier,
Expand All @@ -37,7 +32,6 @@ class TaskSourceGradeHolder {
};
} // namespace

// Guarded by creation_mutex_.
FML_THREAD_LOCAL ThreadLocalUniquePtr<TaskSourceGradeHolder>
tls_task_source_grade;

Expand All @@ -48,14 +42,9 @@ TaskQueueEntry::TaskQueueEntry(TaskQueueId created_for_arg)
task_source = std::make_unique<TaskSource>(created_for);
}

fml::RefPtr<MessageLoopTaskQueues> MessageLoopTaskQueues::GetInstance() {
std::scoped_lock creation(creation_mutex_);
if (!instance_) {
instance_ = fml::MakeRefCounted<MessageLoopTaskQueues>();
tls_task_source_grade.reset(
new TaskSourceGradeHolder{TaskSourceGrade::kUnspecified});
}
return instance_;
MessageLoopTaskQueues* MessageLoopTaskQueues::GetInstance() {
static MessageLoopTaskQueues* instance = new MessageLoopTaskQueues;
return instance;
}

TaskQueueId MessageLoopTaskQueues::CreateTaskQueue() {
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
10 changes: 2 additions & 8 deletions fml/message_loop_task_queues.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<MessageLoopTaskQueues> {
class MessageLoopTaskQueues {
public:
// Lifecycle.

static fml::RefPtr<MessageLoopTaskQueues> GetInstance();
static MessageLoopTaskQueues* GetInstance();

TaskQueueId CreateTaskQueue();

Expand Down Expand Up @@ -152,18 +151,13 @@ class MessageLoopTaskQueues

fml::TimePoint GetNextWakeTimeUnlocked(TaskQueueId queue_id) const;

static std::mutex creation_mutex_;
static fml::RefPtr<MessageLoopTaskQueues> instance_;

mutable std::mutex queue_mutex_;
std::map<TaskQueueId, std::unique_ptr<TaskQueueEntry>> queue_entries_;

size_t task_queue_id_counter_;

std::atomic_int order_;

FML_FRIEND_MAKE_REF_COUNTED(MessageLoopTaskQueues);
FML_FRIEND_REF_COUNTED_THREAD_SAFE(MessageLoopTaskQueues);
FML_DISALLOW_COPY_ASSIGN_AND_MOVE(MessageLoopTaskQueues);
};

Expand Down
2 changes: 1 addition & 1 deletion fml/message_loop_task_queues_merge_unmerge_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TestWakeable : public fml::Wakeable {
WakeUpCall wake_up_call_;
};

static int CountRemainingTasks(fml::RefPtr<MessageLoopTaskQueues> task_queue,
static int CountRemainingTasks(fml::MessageLoopTaskQueues* task_queue,
const TaskQueueId& queue_id,
bool run_invocation = false) {
const auto now = ChronoTicksSinceEpoch();
Expand Down
2 changes: 1 addition & 1 deletion fml/shared_thread_merger.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class SharedThreadMerger
private:
fml::TaskQueueId owner_;
fml::TaskQueueId subsumed_;
fml::RefPtr<fml::MessageLoopTaskQueues> task_queues_;
fml::MessageLoopTaskQueues* task_queues_;
std::mutex mutex_;
bool enabled_;

Expand Down

0 comments on commit 3f2d6e7

Please sign in to comment.