blob: 274987b630eec24705a99ffb4ccf27b7ccb20576 (
plain)
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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/child/worker_thread_registry.h"
#include "base/callback.h"
#include "base/lazy_instance.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/observer_list.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/thread_local.h"
#include "content/public/child/worker_thread.h"
namespace content {
namespace {
using WorkerThreadObservers = base::ObserverList<WorkerThread::Observer>;
using ThreadLocalWorkerThreadObservers =
base::ThreadLocalPointer<WorkerThreadObservers>;
// Stores a WorkerThreadObservers instance per thread.
base::LazyInstance<ThreadLocalWorkerThreadObservers> g_observers_tls =
LAZY_INSTANCE_INITIALIZER;
// A task-runner that refuses to run any tasks.
class DoNothingTaskRunner : public base::TaskRunner {
public:
DoNothingTaskRunner() {}
private:
~DoNothingTaskRunner() override {}
bool PostDelayedTask(const tracked_objects::Location& from_here,
const base::Closure& task,
base::TimeDelta delay) override {
return false;
}
bool RunsTasksOnCurrentThread() const override { return false; }
};
} // namespace
// WorkerThread implementation:
int WorkerThread::GetCurrentId() {
if (!g_observers_tls.Pointer()->Get())
return 0;
return base::PlatformThread::CurrentId();
}
void WorkerThread::PostTask(int id, const base::Closure& task) {
WorkerThreadRegistry::Instance()->PostTask(id, task);
}
void WorkerThread::AddObserver(Observer* observer) {
DCHECK(GetCurrentId() > 0);
WorkerThreadObservers* observers = g_observers_tls.Pointer()->Get();
DCHECK(observers);
observers->AddObserver(observer);
}
void WorkerThread::RemoveObserver(Observer* observer) {
DCHECK(GetCurrentId() > 0);
WorkerThreadObservers* observers = g_observers_tls.Pointer()->Get();
DCHECK(observers);
observers->RemoveObserver(observer);
}
// WorkerThreadRegistry implementation:
WorkerThreadRegistry::WorkerThreadRegistry()
: task_runner_for_dead_worker_(new DoNothingTaskRunner()) {}
int WorkerThreadRegistry::PostTaskToAllThreads(const base::Closure& 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::LazyInstance<WorkerThreadRegistry>::Leaky worker_task_runner =
LAZY_INSTANCE_INITIALIZER;
return worker_task_runner.Pointer();
}
WorkerThreadRegistry::~WorkerThreadRegistry() {}
void WorkerThreadRegistry::DidStartCurrentWorkerThread() {
DCHECK(!g_observers_tls.Pointer()->Get());
DCHECK(!base::PlatformThread::CurrentRef().is_null());
g_observers_tls.Pointer()->Set(new WorkerThreadObservers());
int id = base::PlatformThread::CurrentId();
base::AutoLock locker_(task_runner_map_lock_);
task_runner_map_[id] = base::ThreadTaskRunnerHandle::Get().get();
CHECK(task_runner_map_[id]);
}
void WorkerThreadRegistry::WillStopCurrentWorkerThread() {
WorkerThreadObservers* observers = g_observers_tls.Pointer()->Get();
DCHECK(observers);
FOR_EACH_OBSERVER(WorkerThread::Observer, *observers,
WillStopCurrentWorkerThread());
{
base::AutoLock locker(task_runner_map_lock_);
task_runner_map_.erase(WorkerThread::GetCurrentId());
}
delete observers;
g_observers_tls.Pointer()->Set(nullptr);
}
base::TaskRunner* WorkerThreadRegistry::GetTaskRunnerFor(int worker_id) {
base::AutoLock locker(task_runner_map_lock_);
return ContainsKey(task_runner_map_, worker_id)
? task_runner_map_[worker_id]
: task_runner_for_dead_worker_.get();
}
bool WorkerThreadRegistry::PostTask(int id, const base::Closure& closure) {
DCHECK(id > 0);
base::AutoLock locker(task_runner_map_lock_);
IDToTaskRunnerMap::iterator found = task_runner_map_.find(id);
if (found == task_runner_map_.end())
return false;
return found->second->PostTask(FROM_HERE, closure);
}
} // namespace content
|