Make thread-local-task-runner a shared_ptr

This commit is contained in:
Attila Uygun 2023-06-12 19:16:18 +02:00
parent 83400a0b52
commit 305b23738c
5 changed files with 16 additions and 18 deletions

View File

@ -4,12 +4,14 @@
#include "base/log.h" #include "base/log.h"
namespace base {
namespace { namespace {
void PostTaskAndReplyRelay(base::Location from, void PostTaskAndReplyRelay(Location from,
base::Closure task_cb, Closure task_cb,
base::Closure reply_cb, Closure reply_cb,
base::TaskRunner* destination) { std::shared_ptr<TaskRunner> destination) {
task_cb(); task_cb();
if (reply_cb) if (reply_cb)
@ -18,22 +20,20 @@ void PostTaskAndReplyRelay(base::Location from,
} // namespace } // namespace
namespace base {
// The task runner that belongs to the thread it's created in. Tasks to be run // The task runner that belongs to the thread it's created in. Tasks to be run
// on a specific thread can be posted to this task runner. // on a specific thread can be posted to this task runner.
// TaskRunner::GetThreadLocalTaskRunner()->SingleConsumerRun() is expected to be // TaskRunner::GetThreadLocalTaskRunner()->SingleConsumerRun() is expected to be
// periodically called. // periodically called.
thread_local std::unique_ptr<TaskRunner> TaskRunner::thread_local_task_runner; thread_local std::shared_ptr<TaskRunner> TaskRunner::thread_local_task_runner;
void TaskRunner::CreateThreadLocalTaskRunner() { void TaskRunner::CreateThreadLocalTaskRunner() {
DCHECK(!thread_local_task_runner); DCHECK(!thread_local_task_runner);
thread_local_task_runner = std::make_unique<TaskRunner>(); thread_local_task_runner = std::make_shared<TaskRunner>();
} }
TaskRunner* TaskRunner::GetThreadLocalTaskRunner() { std::shared_ptr<TaskRunner> TaskRunner::GetThreadLocalTaskRunner() {
return thread_local_task_runner.get(); return thread_local_task_runner;
} }
void TaskRunner::PostTask(Location from, Closure task) { void TaskRunner::PostTask(Location from, Closure task) {
@ -49,8 +49,8 @@ void TaskRunner::PostTaskAndReply(Location from, Closure task, Closure reply) {
DCHECK(reply) << LOCATION(from); DCHECK(reply) << LOCATION(from);
DCHECK(thread_local_task_runner) << LOCATION(from); DCHECK(thread_local_task_runner) << LOCATION(from);
auto relay = std::bind(::PostTaskAndReplyRelay, from, std::move(task), auto relay = std::bind(PostTaskAndReplyRelay, from, std::move(task),
std::move(reply), thread_local_task_runner.get()); std::move(reply), thread_local_task_runner);
PostTask(from, std::move(relay)); PostTask(from, std::move(relay));
} }

View File

@ -66,7 +66,7 @@ class TaskRunner {
void WaitForCompletion(); void WaitForCompletion();
static void CreateThreadLocalTaskRunner(); static void CreateThreadLocalTaskRunner();
static TaskRunner* GetThreadLocalTaskRunner(); static std::shared_ptr<TaskRunner> GetThreadLocalTaskRunner();
private: private:
using Task = std::tuple<Location, Closure>; using Task = std::tuple<Location, Closure>;
@ -76,7 +76,7 @@ class TaskRunner {
std::atomic<size_t> task_count_{0}; std::atomic<size_t> task_count_{0};
std::atomic<bool> cancel_tasks_{false}; std::atomic<bool> cancel_tasks_{false};
static thread_local std::unique_ptr<TaskRunner> thread_local_task_runner; static thread_local std::shared_ptr<TaskRunner> thread_local_task_runner;
void CancelTasksInternal(); void CancelTasksInternal();

View File

@ -11,8 +11,6 @@
namespace base { namespace base {
class TaskRunner;
// Feed the ThreadPool tasks (in the form of Closure objects) and they will be // Feed the ThreadPool tasks (in the form of Closure objects) and they will be
// called on any thread from the pool. // called on any thread from the pool.
class ThreadPool { class ThreadPool {

View File

@ -84,7 +84,7 @@ class AudioMixer : public AudioSink::Delegate {
std::list<std::shared_ptr<Resource>> end_list_; std::list<std::shared_ptr<Resource>> end_list_;
base::TaskRunner* main_thread_task_runner_; std::shared_ptr<base::TaskRunner> main_thread_task_runner_;
std::unique_ptr<AudioSink> audio_sink_; std::unique_ptr<AudioSink> audio_sink_;

View File

@ -144,7 +144,7 @@ class RendererOpenGL final : public Renderer {
std::counting_semaphore<> draw_complete_semaphore_{0}; std::counting_semaphore<> draw_complete_semaphore_{0};
base::TaskRunner* main_thread_task_runner_; std::shared_ptr<base::TaskRunner> main_thread_task_runner_;
#endif // THREADED_RENDERING #endif // THREADED_RENDERING
// Stats. // Stats.