OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/child/worker_thread_registry.h" | 5 #include "content/child/worker_thread_registry.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> |
8 | 9 |
9 #include "base/callback.h" | |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
16 #include "base/threading/thread_local.h" | 16 #include "base/threading/thread_local.h" |
17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
18 #include "content/public/child/worker_thread.h" | 18 #include "content/public/child/worker_thread.h" |
19 | 19 |
(...skipping 11 matching lines...) Expand all Loading... |
31 | 31 |
32 // A task-runner that refuses to run any tasks. | 32 // A task-runner that refuses to run any tasks. |
33 class DoNothingTaskRunner : public base::TaskRunner { | 33 class DoNothingTaskRunner : public base::TaskRunner { |
34 public: | 34 public: |
35 DoNothingTaskRunner() {} | 35 DoNothingTaskRunner() {} |
36 | 36 |
37 private: | 37 private: |
38 ~DoNothingTaskRunner() override {} | 38 ~DoNothingTaskRunner() override {} |
39 | 39 |
40 bool PostDelayedTask(const tracked_objects::Location& from_here, | 40 bool PostDelayedTask(const tracked_objects::Location& from_here, |
41 const base::Closure& task, | 41 base::Closure task, |
42 base::TimeDelta delay) override { | 42 base::TimeDelta delay) override { |
43 return false; | 43 return false; |
44 } | 44 } |
45 | 45 |
46 bool RunsTasksOnCurrentThread() const override { return false; } | 46 bool RunsTasksOnCurrentThread() const override { return false; } |
47 }; | 47 }; |
48 | 48 |
49 } // namespace | 49 } // namespace |
50 | 50 |
51 // WorkerThread implementation: | 51 // WorkerThread implementation: |
52 | 52 |
53 int WorkerThread::GetCurrentId() { | 53 int WorkerThread::GetCurrentId() { |
54 if (!g_observers_tls.Pointer()->Get()) | 54 if (!g_observers_tls.Pointer()->Get()) |
55 return 0; | 55 return 0; |
56 return base::PlatformThread::CurrentId(); | 56 return base::PlatformThread::CurrentId(); |
57 } | 57 } |
58 | 58 |
59 void WorkerThread::PostTask(int id, const base::Closure& task) { | 59 void WorkerThread::PostTask(int id, base::Closure task) { |
60 WorkerThreadRegistry::Instance()->PostTask(id, task); | 60 WorkerThreadRegistry::Instance()->PostTask(id, std::move(task)); |
61 } | 61 } |
62 | 62 |
63 void WorkerThread::AddObserver(Observer* observer) { | 63 void WorkerThread::AddObserver(Observer* observer) { |
64 DCHECK(GetCurrentId() > 0); | 64 DCHECK(GetCurrentId() > 0); |
65 WorkerThreadObservers* observers = g_observers_tls.Pointer()->Get(); | 65 WorkerThreadObservers* observers = g_observers_tls.Pointer()->Get(); |
66 DCHECK(observers); | 66 DCHECK(observers); |
67 observers->AddObserver(observer); | 67 observers->AddObserver(observer); |
68 } | 68 } |
69 | 69 |
70 void WorkerThread::RemoveObserver(Observer* observer) { | 70 void WorkerThread::RemoveObserver(Observer* observer) { |
71 DCHECK(GetCurrentId() > 0); | 71 DCHECK(GetCurrentId() > 0); |
72 WorkerThreadObservers* observers = g_observers_tls.Pointer()->Get(); | 72 WorkerThreadObservers* observers = g_observers_tls.Pointer()->Get(); |
73 DCHECK(observers); | 73 DCHECK(observers); |
74 observers->RemoveObserver(observer); | 74 observers->RemoveObserver(observer); |
75 } | 75 } |
76 | 76 |
77 // WorkerThreadRegistry implementation: | 77 // WorkerThreadRegistry implementation: |
78 | 78 |
79 WorkerThreadRegistry::WorkerThreadRegistry() | 79 WorkerThreadRegistry::WorkerThreadRegistry() |
80 : task_runner_for_dead_worker_(new DoNothingTaskRunner()) {} | 80 : task_runner_for_dead_worker_(new DoNothingTaskRunner()) {} |
81 | 81 |
82 int WorkerThreadRegistry::PostTaskToAllThreads(const base::Closure& closure) { | 82 int WorkerThreadRegistry::PostTaskToAllThreads(base::Closure closure) { |
83 base::AutoLock locker(task_runner_map_lock_); | 83 base::AutoLock locker(task_runner_map_lock_); |
84 for (const auto& it : task_runner_map_) | 84 for (const auto& it : task_runner_map_) |
85 it.second->PostTask(FROM_HERE, closure); | 85 it.second->PostTask(FROM_HERE, std::move(closure)); |
86 return static_cast<int>(task_runner_map_.size()); | 86 return static_cast<int>(task_runner_map_.size()); |
87 } | 87 } |
88 | 88 |
89 WorkerThreadRegistry* WorkerThreadRegistry::Instance() { | 89 WorkerThreadRegistry* WorkerThreadRegistry::Instance() { |
90 static base::LazyInstance<WorkerThreadRegistry>::Leaky worker_task_runner = | 90 static base::LazyInstance<WorkerThreadRegistry>::Leaky worker_task_runner = |
91 LAZY_INSTANCE_INITIALIZER; | 91 LAZY_INSTANCE_INITIALIZER; |
92 return worker_task_runner.Pointer(); | 92 return worker_task_runner.Pointer(); |
93 } | 93 } |
94 | 94 |
95 WorkerThreadRegistry::~WorkerThreadRegistry() {} | 95 WorkerThreadRegistry::~WorkerThreadRegistry() {} |
(...skipping 22 matching lines...) Expand all Loading... |
118 g_observers_tls.Pointer()->Set(nullptr); | 118 g_observers_tls.Pointer()->Set(nullptr); |
119 } | 119 } |
120 | 120 |
121 base::TaskRunner* WorkerThreadRegistry::GetTaskRunnerFor(int worker_id) { | 121 base::TaskRunner* WorkerThreadRegistry::GetTaskRunnerFor(int worker_id) { |
122 base::AutoLock locker(task_runner_map_lock_); | 122 base::AutoLock locker(task_runner_map_lock_); |
123 return base::ContainsKey(task_runner_map_, worker_id) | 123 return base::ContainsKey(task_runner_map_, worker_id) |
124 ? task_runner_map_[worker_id] | 124 ? task_runner_map_[worker_id] |
125 : task_runner_for_dead_worker_.get(); | 125 : task_runner_for_dead_worker_.get(); |
126 } | 126 } |
127 | 127 |
128 bool WorkerThreadRegistry::PostTask(int id, const base::Closure& closure) { | 128 bool WorkerThreadRegistry::PostTask(int id, base::Closure closure) { |
129 DCHECK(id > 0); | 129 DCHECK(id > 0); |
130 base::AutoLock locker(task_runner_map_lock_); | 130 base::AutoLock locker(task_runner_map_lock_); |
131 IDToTaskRunnerMap::iterator found = task_runner_map_.find(id); | 131 IDToTaskRunnerMap::iterator found = task_runner_map_.find(id); |
132 if (found == task_runner_map_.end()) | 132 if (found == task_runner_map_.end()) |
133 return false; | 133 return false; |
134 return found->second->PostTask(FROM_HERE, closure); | 134 return found->second->PostTask(FROM_HERE, std::move(closure)); |
135 } | 135 } |
136 | 136 |
137 } // namespace content | 137 } // namespace content |
OLD | NEW |