Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/callback.h" | |
| 6 #include "base/lazy_instance.h" | |
| 7 #include "base/logging.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/observer_list.h" | |
| 10 #include "webkit/glue/worker_task_runner.h" | |
| 11 | |
| 12 using WebKit::WebWorkerRunLoop; | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 class RunClosureTask : public WebKit::WebWorkerRunLoop::Task { | |
| 17 public: | |
| 18 RunClosureTask(const base::Closure& task) : task_(task) { } | |
| 19 virtual ~RunClosureTask() { } | |
| 20 virtual void Run() { | |
| 21 task_.Run(); | |
| 22 } | |
| 23 private: | |
| 24 base::Closure task_; | |
| 25 }; | |
| 26 | |
| 27 } // unnamed namespace | |
| 28 | |
| 29 namespace webkit_glue { | |
| 30 | |
| 31 struct WorkerTaskRunner::ThreadLocalState { | |
| 32 int id_; | |
| 33 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.
| |
| 34 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.
| |
| 35 }; | |
| 36 | |
| 37 WorkerTaskRunner::WorkerTaskRunner() { | |
| 38 // Start worker ids at 1, 0 is reserved for the main thread. | |
| 39 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.
| |
| 40 } | |
| 41 | |
| 42 void WorkerTaskRunner::PostTask( | |
| 43 int id, const base::Closure& closure) { | |
| 44 DCHECK(id > 0); | |
| 45 base::AutoLock locker_(loop_map_lock_); | |
| 46 IDToLoopMap::iterator found = loop_map_.find(id); | |
| 47 if (found != loop_map_.end()) | |
| 48 found->second.postTask(new RunClosureTask(closure)); | |
| 49 } | |
| 50 | |
| 51 int WorkerTaskRunner::CurrentWorkerId() { | |
| 52 if (!current_tls_.Get()) | |
| 53 return 0; | |
| 54 return current_tls_.Get()->id_; | |
| 55 } | |
| 56 | |
| 57 WorkerTaskRunner* WorkerTaskRunner::Instance() { | |
| 58 static base::LazyInstance<WorkerTaskRunner, | |
| 59 base::LeakyLazyInstanceTraits<WorkerTaskRunner> > | |
| 60 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
| |
| 61 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'
| |
| 62 } | |
| 63 | |
| 64 void WorkerTaskRunner::AddStopObserver(Observer* obs) { | |
| 65 DCHECK(CurrentWorkerId() > 0); | |
| 66 current_tls_.Get()->stop_observers_.AddObserver(obs); | |
| 67 } | |
| 68 | |
| 69 void WorkerTaskRunner::RemoveStopObserver(Observer* obs) { | |
| 70 DCHECK(CurrentWorkerId() > 0); | |
| 71 current_tls_.Get()->stop_observers_.RemoveObserver(obs); | |
| 72 } | |
| 73 | |
| 74 WorkerTaskRunner::~WorkerTaskRunner() { | |
| 75 } | |
| 76 | |
| 77 void WorkerTaskRunner::OnWorkerRunLoopStarted(const WebWorkerRunLoop& loop) { | |
| 78 DCHECK(!CurrentWorkerId()); | |
| 79 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.
| |
| 80 int id = id_sequence_.GetNext(); | |
| 81 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.
| |
| 82 tls->id_ = id; | |
| 83 tls->run_loop_ = loop; | |
| 84 current_tls_.Set(tls); | |
| 85 | |
| 86 base::AutoLock locker_(loop_map_lock_); | |
| 87 loop_map_[id] = loop; | |
| 88 } | |
| 89 | |
| 90 void WorkerTaskRunner::OnWorkerRunLoopStopped(const WebWorkerRunLoop& loop) { | |
| 91 DCHECK(CurrentWorkerId() > 0); | |
| 92 DCHECK(current_tls_.Get()); | |
|
michaeln
2011/12/10 01:19:32
ditto
dgrogan
2011/12/10 01:37:40
Done.
| |
| 93 FOR_EACH_OBSERVER(Observer, current_tls_.Get()->stop_observers_, | |
| 94 OnWorkerRunLoopStopped()); | |
| 95 | |
| 96 delete current_tls_.Get(); | |
|
dgrogan
2011/12/10 01:11:12
Oops, this has to go below the CurrentWorkerId cal
| |
| 97 current_tls_.Set(NULL); | |
| 98 | |
| 99 base::AutoLock locker_(loop_map_lock_); | |
| 100 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
| |
| 101 loop_map_.erase(CurrentWorkerId()); | |
| 102 } | |
| 103 | |
| 104 } // namespace webkit_glue | |
| OLD | NEW |