Chromium Code Reviews| Index: webkit/glue/worker_task_runner.cc |
| diff --git a/webkit/glue/worker_task_runner.cc b/webkit/glue/worker_task_runner.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d3037fecd2b98a5b2c7449c750ab13b07c3808b1 |
| --- /dev/null |
| +++ b/webkit/glue/worker_task_runner.cc |
| @@ -0,0 +1,104 @@ |
| +// 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 "base/memory/scoped_ptr.h" |
| +#include "base/observer_list.h" |
| +#include "webkit/glue/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_; |
| +}; |
| + |
| +} // unnamed namespace |
| + |
| +namespace webkit_glue { |
| + |
| +struct WorkerTaskRunner::ThreadLocalState { |
| + int id_; |
| + WebKit::WebWorkerRunLoop run_loop_; |
|
michaeln
2011/12/10 01:19:32
since your using this on line 12, no need for WebK
dgrogan
2011/12/10 01:37:40
Done.
|
| + ObserverList<webkit_glue::WorkerTaskRunner::Observer> stop_observers_; |
|
michaeln
2011/12/10 01:19:32
no need for webkit_glue:: since we're in that name
dgrogan
2011/12/10 01:37:40
Done.
|
| +}; |
| + |
| +WorkerTaskRunner::WorkerTaskRunner() { |
| + // Start worker ids at 1, 0 is reserved for the main thread. |
| + DCHECK(!id_sequence_.GetNext()); |
|
michaeln
2011/12/10 01:19:32
DCHECKs dont exist in release builds
dgrogan
2011/12/10 01:37:40
Right. Fixed.
|
| +} |
| + |
| +void 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()) |
| + found->second.postTask(new RunClosureTask(closure)); |
| +} |
| + |
| +int WorkerTaskRunner::CurrentWorkerId() { |
| + if (!current_tls_.Get()) |
| + return 0; |
| + return current_tls_.Get()->id_; |
| +} |
| + |
| +WorkerTaskRunner* WorkerTaskRunner::Instance() { |
| + static base::LazyInstance<WorkerTaskRunner, |
| + base::LeakyLazyInstanceTraits<WorkerTaskRunner> > |
| + worker_task_runner_ = LAZY_INSTANCE_INITIALIZER; |
|
michaeln
2011/12/10 01:19:32
this wrapping looks odd to me, can't distinguish b
dgrogan
2011/12/10 01:37:40
Any better?
michaeln
2011/12/10 02:08:34
yes, thnx
|
| + return worker_task_runner_.Pointer(); |
|
michaeln
2011/12/10 01:19:32
not a data member, so no need for the underbar, fr
dgrogan
2011/12/10 01:37:40
Underscore removed. I didn't add a g_ because it'
|
| +} |
| + |
| +void WorkerTaskRunner::AddStopObserver(Observer* obs) { |
| + DCHECK(CurrentWorkerId() > 0); |
| + current_tls_.Get()->stop_observers_.AddObserver(obs); |
| +} |
| + |
| +void WorkerTaskRunner::RemoveStopObserver(Observer* obs) { |
| + DCHECK(CurrentWorkerId() > 0); |
| + current_tls_.Get()->stop_observers_.RemoveObserver(obs); |
| +} |
| + |
| +WorkerTaskRunner::~WorkerTaskRunner() { |
| +} |
| + |
| +void WorkerTaskRunner::OnWorkerRunLoopStarted(const WebWorkerRunLoop& loop) { |
| + DCHECK(!CurrentWorkerId()); |
| + DCHECK(!current_tls_.Get()); |
|
michaeln
2011/12/10 01:19:32
just the 2nd DCHECK is probably sufficient
dgrogan
2011/12/10 01:37:40
Done.
|
| + int id = id_sequence_.GetNext(); |
| + ThreadLocalState* tls = new ThreadLocalState; |
|
michaeln
2011/12/10 01:19:32
a ctor might be nice...
current_tls_.Set(new Sta
dgrogan
2011/12/10 01:37:40
Done.
|
| + tls->id_ = id; |
| + tls->run_loop_ = loop; |
| + current_tls_.Set(tls); |
| + |
| + base::AutoLock locker_(loop_map_lock_); |
| + loop_map_[id] = loop; |
| +} |
| + |
| +void WorkerTaskRunner::OnWorkerRunLoopStopped(const WebWorkerRunLoop& loop) { |
| + DCHECK(CurrentWorkerId() > 0); |
| + DCHECK(current_tls_.Get()); |
|
michaeln
2011/12/10 01:19:32
ditto
dgrogan
2011/12/10 01:37:40
Done.
|
| + FOR_EACH_OBSERVER(Observer, current_tls_.Get()->stop_observers_, |
| + OnWorkerRunLoopStopped()); |
| + |
| + delete current_tls_.Get(); |
|
dgrogan
2011/12/10 01:11:12
Oops, this has to go below the CurrentWorkerId cal
|
| + current_tls_.Set(NULL); |
| + |
| + base::AutoLock locker_(loop_map_lock_); |
| + DCHECK(loop_map_[CurrentWorkerId()] == loop); |
|
michaeln
2011/12/10 01:19:32
is the value of CurrentWorkerId() always 0 at this
dgrogan
2011/12/10 01:37:40
Yes, it was. But now that the current_tls_ lines
|
| + loop_map_.erase(CurrentWorkerId()); |
| +} |
| + |
| +} // namespace webkit_glue |