1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/worker/worker_thread_registry.h"
#include <atomic>
#include <utility>
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/no_destructor.h"
#include "base/observer_list.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "content/public/renderer/worker_thread.h"
namespace content {
namespace {
using WorkerThreadObservers =
base::ObserverList<WorkerThread::Observer>::Unchecked;
struct WorkerThreadData {
WorkerThreadData() {
// Worker thread ID starts with 1 (0 is reserved for the main thread).
static std::atomic_int seq{1};
thread_id = seq++;
}
int thread_id = 0;
WorkerThreadObservers observers;
};
constinit thread_local WorkerThreadData* worker_data = nullptr;
// A task-runner that refuses to run any tasks.
class DoNothingTaskRunner : public base::SequencedTaskRunner {
public:
DoNothingTaskRunner() {}
private:
~DoNothingTaskRunner() override {}
bool PostDelayedTask(const base::Location& from_here,
base::OnceClosure task,
base::TimeDelta delay) override {
return false;
}
bool PostNonNestableDelayedTask(const base::Location& from_here,
base::OnceClosure task,
base::TimeDelta delay) override {
return false;
}
bool RunsTasksInCurrentSequence() const override { return false; }
};
} // namespace
// WorkerThread implementation:
int WorkerThread::GetCurrentId() {
return worker_data ? worker_data->thread_id : 0;
}
void WorkerThread::PostTask(int id, base::OnceClosure task) {
WorkerThreadRegistry::Instance()->PostTask(id, std::move(task));
}
void WorkerThread::AddObserver(Observer* observer) {
DCHECK(worker_data);
worker_data->observers.AddObserver(observer);
}
void WorkerThread::RemoveObserver(Observer* observer) {
DCHECK(worker_data);
worker_data->observers.RemoveObserver(observer);
}
// WorkerThreadRegistry implementation:
WorkerThreadRegistry::WorkerThreadRegistry()
: task_runner_for_dead_worker_(new DoNothingTaskRunner()) {}
int WorkerThreadRegistry::PostTaskToAllThreads(
const base::RepeatingClosure& closure) {
base::AutoLock locker(task_runner_map_lock_);
for (const auto& it : task_runner_map_)
it.second->PostTask(FROM_HERE, closure);
return static_cast<int>(task_runner_map_.size());
}
WorkerThreadRegistry* WorkerThreadRegistry::Instance() {
static base::NoDestructor<WorkerThreadRegistry> worker_thread_registry;
return worker_thread_registry.get();
}
WorkerThreadRegistry::~WorkerThreadRegistry() = default;
void WorkerThreadRegistry::DidStartCurrentWorkerThread() {
DCHECK(!worker_data);
DCHECK(!base::PlatformThread::CurrentRef().is_null());
worker_data = new WorkerThreadData();
base::AutoLock locker_(task_runner_map_lock_);
task_runner_map_[worker_data->thread_id] =
base::SingleThreadTaskRunner::GetCurrentDefault().get();
CHECK(task_runner_map_[worker_data->thread_id]);
}
void WorkerThreadRegistry::WillStopCurrentWorkerThread() {
DCHECK(worker_data);
for (auto& observer : worker_data->observers)
observer.WillStopCurrentWorkerThread();
{
base::AutoLock locker(task_runner_map_lock_);
task_runner_map_.erase(worker_data->thread_id);
}
delete worker_data;
worker_data = nullptr;
}
base::SequencedTaskRunner* WorkerThreadRegistry::GetTaskRunnerFor(
int worker_id) {
base::AutoLock locker(task_runner_map_lock_);
return base::Contains(task_runner_map_, worker_id)
? task_runner_map_[worker_id]
: task_runner_for_dead_worker_.get();
}
bool WorkerThreadRegistry::PostTask(int id, base::OnceClosure closure) {
DCHECK(id > 0);
base::AutoLock locker(task_runner_map_lock_);
auto found = task_runner_map_.find(id);
if (found == task_runner_map_.end())
return false;
return found->second->PostTask(FROM_HERE, std::move(closure));
}
} // namespace content
|