Chromium Code Reviews| Index: content/common/worker_task_runner.cc |
| diff --git a/content/common/worker_task_runner.cc b/content/common/worker_task_runner.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ed8c93fe8dad400f1e0f71265f43eccf75ffeff6 |
| --- /dev/null |
| +++ b/content/common/worker_task_runner.cc |
| @@ -0,0 +1,96 @@ |
| +// Copyright (c) 2011 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 "base/callback.h" |
| +#include "base/lazy_instance.h" |
| +#include "base/logging.h" |
| +#include "content/common/worker_task_runner.h" |
| + |
| +using WebKit::WebWorkerRunLoop; |
| + |
| +namespace { |
| + |
| +class RunClosureTask : public WebKit::WebWorkerRunLoop::Task { |
| + public: |
| + RunClosureTask(const base::Closure& task) : task_(task) { } |
| + virtual ~RunClosureTask() { } |
| + virtual void Run() { |
| + task_.Run(); |
| + } |
| + private: |
| + base::Closure task_; |
| +}; |
| + |
| +} |
| + |
| +WorkerTaskRunner::WorkerTaskRunner() : id_sequence_(0) { |
| +} |
| + |
| +void WorkerTaskRunner::PostTask( |
| + int id, const base::Closure& closure) { |
| + DCHECK(id > 0); |
| + base::AutoLock locker_(lock_); |
| + IDToLoopMap::iterator found = loop_map_.find(id); |
| + if (found != loop_map_.end()) |
| + found->second.postTask(new RunClosureTask(closure)); |
| +} |
| + |
| +int WorkerTaskRunner::CurrentWorkerId() { |
| + if (!current_tls_.Get()) |
| + return 0; |
| + return current_tls_.Get()->first; |
| +} |
| + |
| +WorkerTaskRunner* WorkerTaskRunner::Instance() { |
| + static base::LazyInstance<WorkerTaskRunner, |
| + base::LeakyLazyInstanceTraits<WorkerTaskRunner> > |
| + worker_task_runner_; |
|
michaeln
2011/12/08 03:10:47
other usages of LazyInstance assign the LAZY_INSTA
dgrogan
2011/12/09 03:20:41
You're right. Changed.
|
| + return worker_task_runner_.Pointer(); |
| +} |
| + |
| +void WorkerTaskRunner::AddObserver(Observer* obs) { |
| + base::AutoLock locker(observer_lock_); |
| + observer_list_.insert(obs); |
| +} |
| + |
| +int WorkerTaskRunner::RemoveObserver(Observer* obs) { |
| + base::AutoLock locker(observer_lock_); |
| + return observer_list_.erase(obs); |
| +} |
| + |
| +WorkerTaskRunner::~WorkerTaskRunner() { |
| +} |
| + |
| +void WorkerTaskRunner::OnWorkerRunLoopStarted(const WebWorkerRunLoop& loop) { |
| + DCHECK(!current_tls_.Get()); |
| + { |
| + base::AutoLock locker_(lock_); |
| + int id = ++id_sequence_; |
| + current_tls_.Set(new std::pair<int, WebWorkerRunLoop>(id, loop)); |
| + loop_map_[id] = loop; |
| + } |
| + base::AutoLock locker(observer_lock_); |
| + for(ObserverList::iterator iter = observer_list_.begin(); |
| + iter != observer_list_.end(); iter++) { |
| + (*iter)->OnWorkerRunLoopStarted(); |
|
darin (slow to review)
2011/12/08 22:41:21
It is usually bad design to hold locks while calli
michaeln
2011/12/09 00:43:32
This is generally true, but also not much an issue
dgrogan
2011/12/09 03:20:41
Darin and I talked about this today, mostly in the
|
| + } |
| +} |
| + |
| +void WorkerTaskRunner::OnWorkerRunLoopStopped( |
| + const WebWorkerRunLoop& loop) { |
| + DCHECK(CurrentWorkerId() > 0); |
| + DCHECK(current_tls_.Get()); |
| + { |
| + base::AutoLock lock(observer_lock_); |
| + for(ObserverList::iterator iter = observer_list_.begin(); |
| + iter != observer_list_.end(); iter++) { |
| + (*iter)->OnWorkerRunLoopStopped(); |
| + } |
| + } |
| + base::AutoLock locker_(lock_); |
| + DCHECK(loop_map_[CurrentWorkerId()] == loop); |
| + loop_map_.erase(CurrentWorkerId()); |
| + delete current_tls_.Get(); |
| + current_tls_.Set(NULL); |
| +} |