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 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 ThreadLocalState(int id, const WebWorkerRunLoop& loop) |
| 33 : id_(id), run_loop_(loop) { |
| 34 } |
| 35 int id_; |
| 36 WebWorkerRunLoop run_loop_; |
| 37 ObserverList<WorkerTaskRunner::Observer> stop_observers_; |
| 38 }; |
| 39 |
| 40 WorkerTaskRunner::WorkerTaskRunner() { |
| 41 // Start worker ids at 1, 0 is reserved for the main thread. |
| 42 int id = id_sequence_.GetNext(); |
| 43 DCHECK(!id); |
| 44 } |
| 45 |
| 46 void WorkerTaskRunner::PostTask( |
| 47 int id, const base::Closure& closure) { |
| 48 DCHECK(id > 0); |
| 49 base::AutoLock locker(loop_map_lock_); |
| 50 IDToLoopMap::iterator found = loop_map_.find(id); |
| 51 if (found != loop_map_.end()) |
| 52 found->second.postTask(new RunClosureTask(closure)); |
| 53 } |
| 54 |
| 55 int WorkerTaskRunner::CurrentWorkerId() { |
| 56 if (!current_tls_.Get()) |
| 57 return 0; |
| 58 return current_tls_.Get()->id_; |
| 59 } |
| 60 |
| 61 WorkerTaskRunner* WorkerTaskRunner::Instance() { |
| 62 static base::LazyInstance<WorkerTaskRunner, |
| 63 base::LeakyLazyInstanceTraits<WorkerTaskRunner> > |
| 64 worker_task_runner = LAZY_INSTANCE_INITIALIZER; |
| 65 return worker_task_runner.Pointer(); |
| 66 } |
| 67 |
| 68 void WorkerTaskRunner::AddStopObserver(Observer* obs) { |
| 69 DCHECK(CurrentWorkerId() > 0); |
| 70 current_tls_.Get()->stop_observers_.AddObserver(obs); |
| 71 } |
| 72 |
| 73 void WorkerTaskRunner::RemoveStopObserver(Observer* obs) { |
| 74 DCHECK(CurrentWorkerId() > 0); |
| 75 current_tls_.Get()->stop_observers_.RemoveObserver(obs); |
| 76 } |
| 77 |
| 78 WorkerTaskRunner::~WorkerTaskRunner() { |
| 79 } |
| 80 |
| 81 void WorkerTaskRunner::OnWorkerRunLoopStarted(const WebWorkerRunLoop& loop) { |
| 82 DCHECK(!current_tls_.Get()); |
| 83 int id = id_sequence_.GetNext(); |
| 84 current_tls_.Set(new ThreadLocalState(id, loop)); |
| 85 |
| 86 base::AutoLock locker_(loop_map_lock_); |
| 87 loop_map_[id] = loop; |
| 88 } |
| 89 |
| 90 void WorkerTaskRunner::OnWorkerRunLoopStopped(const WebWorkerRunLoop& loop) { |
| 91 DCHECK(current_tls_.Get()); |
| 92 FOR_EACH_OBSERVER(Observer, current_tls_.Get()->stop_observers_, |
| 93 OnWorkerRunLoopStopped()); |
| 94 { |
| 95 base::AutoLock locker(loop_map_lock_); |
| 96 DCHECK(loop_map_[CurrentWorkerId()] == loop); |
| 97 loop_map_.erase(CurrentWorkerId()); |
| 98 } |
| 99 delete current_tls_.Get(); |
| 100 current_tls_.Set(NULL); |
| 101 } |
| 102 |
| 103 } // namespace webkit_glue |
OLD | NEW |