summaryrefslogtreecommitdiffstats
path: root/content/child/worker_task_runner.cc
diff options
context:
space:
mode:
authorSadrul Habib Chowdhury <sadrul@chromium.org>2015-02-11 15:43:41 -0500
committerSadrul Habib Chowdhury <sadrul@chromium.org>2015-02-11 20:45:41 +0000
commit344a6b0227de88bb5c4016431c64cd0e1ca3e1e8 (patch)
tree14f89378641aba79c372f038557eb97f00030788 /content/child/worker_task_runner.cc
parentf951166bde5650de692d30c542e320a1e6c0e75a (diff)
downloadchromium_src-344a6b0227de88bb5c4016431c64cd0e1ca3e1e8.zip
chromium_src-344a6b0227de88bb5c4016431c64cd0e1ca3e1e8.tar.gz
chromium_src-344a6b0227de88bb5c4016431c64cd0e1ca3e1e8.tar.bz2
workers: Use base::TaskRunner to run tasks on worker threads.
Use the worker thread's TaskRunner to post-task to the worker threads, instead of using WorkerThreadTaskRunner, which adds an extra level of indirection for posting tasks without any benefits. This is the last place WorkerThreadTaskRunner was being used. So remove it. BUG=457033 R=kinuko@chromium.org, nasko@chromium.org Review URL: https://codereview.chromium.org/903363002 Cr-Commit-Position: refs/heads/master@{#315829}
Diffstat (limited to 'content/child/worker_task_runner.cc')
-rw-r--r--content/child/worker_task_runner.cc62
1 files changed, 39 insertions, 23 deletions
diff --git a/content/child/worker_task_runner.cc b/content/child/worker_task_runner.cc
index 4fc5bde..82410fc 100644
--- a/content/child/worker_task_runner.cc
+++ b/content/child/worker_task_runner.cc
@@ -6,9 +6,13 @@
#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"
using blink::WebWorkerRunLoop;
@@ -16,43 +20,49 @@ namespace content {
namespace {
-class RunClosureTask : public WebWorkerRunLoop::Task {
+// A task-runner that refuses to run any tasks.
+class DoNothingTaskRunner : public base::TaskRunner {
public:
- RunClosureTask(const base::Closure& task) : task_(task) {}
- virtual ~RunClosureTask() {}
- virtual void Run() {
- task_.Run();
- }
+ DoNothingTaskRunner() {}
+
private:
- base::Closure task_;
+ ~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
+} // namespace
struct WorkerTaskRunner::ThreadLocalState {
ThreadLocalState() {}
ObserverList<WorkerTaskRunner::Observer> stop_observers_;
};
-WorkerTaskRunner::WorkerTaskRunner() {
+WorkerTaskRunner::WorkerTaskRunner()
+ : task_runner_for_dead_worker_(new DoNothingTaskRunner()) {
}
bool WorkerTaskRunner::PostTask(
int id, const base::Closure& closure) {
DCHECK(id > 0);
- base::AutoLock locker(loop_map_lock_);
- IDToLoopMap::iterator found = loop_map_.find(id);
- if (found == loop_map_.end())
+ 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(new RunClosureTask(closure));
+ return found->second->PostTask(FROM_HERE, closure);
}
int WorkerTaskRunner::PostTaskToAllThreads(const base::Closure& closure) {
- base::AutoLock locker(loop_map_lock_);
- IDToLoopMap::iterator it;
- for (it = loop_map_.begin(); it != loop_map_.end(); ++it)
- it->second.postTask(new RunClosureTask(closure));
- return static_cast<int>(loop_map_.size());
+ 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());
}
int WorkerTaskRunner::CurrentWorkerId() {
@@ -86,8 +96,9 @@ void WorkerTaskRunner::OnWorkerRunLoopStarted(const WebWorkerRunLoop& loop) {
current_tls_.Set(new ThreadLocalState());
int id = base::PlatformThread::CurrentId();
- base::AutoLock locker_(loop_map_lock_);
- loop_map_[id] = loop;
+ base::AutoLock locker_(task_runner_map_lock_);
+ task_runner_map_[id] = base::ThreadTaskRunnerHandle::Get().get();
+ CHECK(task_runner_map_[id]);
}
void WorkerTaskRunner::OnWorkerRunLoopStopped(const WebWorkerRunLoop& loop) {
@@ -95,12 +106,17 @@ void WorkerTaskRunner::OnWorkerRunLoopStopped(const WebWorkerRunLoop& loop) {
FOR_EACH_OBSERVER(Observer, current_tls_.Get()->stop_observers_,
OnWorkerRunLoopStopped());
{
- base::AutoLock locker(loop_map_lock_);
- DCHECK(loop_map_[CurrentWorkerId()] == loop);
- loop_map_.erase(CurrentWorkerId());
+ base::AutoLock locker(task_runner_map_lock_);
+ task_runner_map_.erase(CurrentWorkerId());
}
delete current_tls_.Get();
current_tls_.Set(NULL);
}
+base::TaskRunner* WorkerTaskRunner::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();
+}
+
} // namespace content