@@ -18,7 +18,8 @@ std::unique_ptr<ConcurrentMessageLoop> ConcurrentMessageLoop::Create(
1818}
1919
2020ConcurrentMessageLoop::ConcurrentMessageLoop (size_t worker_count)
21- : worker_count_(std::max<size_t >(worker_count, 1ul )) {
21+ : worker_count_(std::max<size_t >(worker_count, 1ul )),
22+ task_runner_ (new ConcurrentTaskRunner(this )) {
2223 for (size_t i = 0 ; i < worker_count_; ++i) {
2324 workers_.emplace_back ([i, this ]() {
2425 fml::Thread::SetCurrentThreadName (fml::Thread::ThreadConfig (
@@ -33,6 +34,10 @@ ConcurrentMessageLoop::ConcurrentMessageLoop(size_t worker_count)
3334}
3435
3536ConcurrentMessageLoop::~ConcurrentMessageLoop () {
37+ {
38+ std::scoped_lock lock (task_runner_->weak_loop_mutex_ );
39+ task_runner_->weak_loop_ = nullptr ;
40+ }
3641 Terminate ();
3742 for (auto & worker : workers_) {
3843 worker.join ();
@@ -43,33 +48,18 @@ size_t ConcurrentMessageLoop::GetWorkerCount() const {
4348 return worker_count_;
4449}
4550
46- std::shared_ptr<ConcurrentTaskRunner> ConcurrentMessageLoop::GetTaskRunner () {
47- return std::make_shared<ConcurrentTaskRunner>(weak_from_this ());
48- }
49-
5051void ConcurrentMessageLoop::PostTask (const fml::closure& task) {
5152 if (!task) {
5253 return ;
5354 }
5455
55- std::unique_lock lock (tasks_mutex_);
56-
57- // Don't just drop tasks on the floor in case of shutdown.
58- if (shutdown_) {
59- FML_DLOG (WARNING)
60- << " Tried to post a task to shutdown concurrent message "
61- " loop. The task will be executed on the callers thread." ;
62- lock.unlock ();
63- task ();
64- return ;
65- }
66-
67- tasks_.push (task);
68-
6956 // Unlock the mutex before notifying the condition variable because that mutex
7057 // has to be acquired on the other thread anyway. Waiting in this scope till
7158 // it is acquired there is a pessimization.
72- lock.unlock ();
59+ {
60+ std::unique_lock lock (tasks_mutex_);
61+ tasks_.push (task);
62+ }
7363
7464 tasks_condition_.notify_one ();
7565}
@@ -148,9 +138,8 @@ std::vector<fml::closure> ConcurrentMessageLoop::GetThreadTasksLocked() {
148138 return pending_tasks;
149139}
150140
151- ConcurrentTaskRunner::ConcurrentTaskRunner (
152- std::weak_ptr<ConcurrentMessageLoop> weak_loop)
153- : weak_loop_(std::move(weak_loop)) {}
141+ ConcurrentTaskRunner::ConcurrentTaskRunner (ConcurrentMessageLoop* weak_loop)
142+ : weak_loop_(weak_loop) {}
154143
155144ConcurrentTaskRunner::~ConcurrentTaskRunner () = default ;
156145
@@ -159,15 +148,12 @@ void ConcurrentTaskRunner::PostTask(const fml::closure& task) {
159148 return ;
160149 }
161150
162- if (auto loop = weak_loop_.lock ()) {
163- loop->PostTask (task);
164- return ;
151+ {
152+ std::scoped_lock lock (weak_loop_mutex_);
153+ if (weak_loop_) {
154+ weak_loop_->PostTask (task);
155+ }
165156 }
166-
167- FML_DLOG (WARNING)
168- << " Tried to post to a concurrent message loop that has already died. "
169- " Executing the task on the callers thread." ;
170- task ();
171157}
172158
173159bool ConcurrentMessageLoop::RunsTasksOnCurrentThread () {
0 commit comments