OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/child/worker_task_runner.h" | 5 #include "content/child/worker_task_runner.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 virtual void Run() { | 23 virtual void Run() { |
24 task_.Run(); | 24 task_.Run(); |
25 } | 25 } |
26 private: | 26 private: |
27 base::Closure task_; | 27 base::Closure task_; |
28 }; | 28 }; |
29 | 29 |
30 } // namespace | 30 } // namespace |
31 | 31 |
32 struct WorkerTaskRunner::ThreadLocalState { | 32 struct WorkerTaskRunner::ThreadLocalState { |
33 explicit ThreadLocalState(int id) : id_(id) {} | 33 ThreadLocalState() {} |
34 int id_; | |
35 ObserverList<WorkerTaskRunner::Observer> stop_observers_; | 34 ObserverList<WorkerTaskRunner::Observer> stop_observers_; |
36 }; | 35 }; |
37 | 36 |
38 WorkerTaskRunner::WorkerTaskRunner() { | 37 WorkerTaskRunner::WorkerTaskRunner() { |
39 } | 38 } |
40 | 39 |
41 bool WorkerTaskRunner::PostTask( | 40 bool WorkerTaskRunner::PostTask( |
42 int id, const base::Closure& closure) { | 41 int id, const base::Closure& closure) { |
43 DCHECK(id > 0); | 42 DCHECK(id > 0); |
44 base::AutoLock locker(loop_map_lock_); | 43 base::AutoLock locker(loop_map_lock_); |
45 IDToLoopMap::iterator found = loop_map_.find(id); | 44 IDToLoopMap::iterator found = loop_map_.find(id); |
46 if (found == loop_map_.end()) | 45 if (found == loop_map_.end()) |
47 return false; | 46 return false; |
48 return found->second.postTask(new RunClosureTask(closure)); | 47 return found->second.postTask(new RunClosureTask(closure)); |
49 } | 48 } |
50 | 49 |
51 int WorkerTaskRunner::PostTaskToAllThreads(const base::Closure& closure) { | 50 int WorkerTaskRunner::PostTaskToAllThreads(const base::Closure& closure) { |
52 base::AutoLock locker(loop_map_lock_); | 51 base::AutoLock locker(loop_map_lock_); |
53 IDToLoopMap::iterator it; | 52 IDToLoopMap::iterator it; |
54 for (it = loop_map_.begin(); it != loop_map_.end(); ++it) | 53 for (it = loop_map_.begin(); it != loop_map_.end(); ++it) |
55 it->second.postTask(new RunClosureTask(closure)); | 54 it->second.postTask(new RunClosureTask(closure)); |
56 return static_cast<int>(loop_map_.size()); | 55 return static_cast<int>(loop_map_.size()); |
57 } | 56 } |
58 | 57 |
59 int WorkerTaskRunner::CurrentWorkerId() { | 58 int WorkerTaskRunner::CurrentWorkerId() { |
60 if (!current_tls_.Get()) | 59 if (!current_tls_.Get()) |
61 return 0; | 60 return 0; |
62 return current_tls_.Get()->id_; | 61 return base::PlatformThread::CurrentId(); |
63 } | 62 } |
64 | 63 |
65 WorkerTaskRunner* WorkerTaskRunner::Instance() { | 64 WorkerTaskRunner* WorkerTaskRunner::Instance() { |
66 static base::LazyInstance<WorkerTaskRunner>::Leaky | 65 static base::LazyInstance<WorkerTaskRunner>::Leaky |
67 worker_task_runner = LAZY_INSTANCE_INITIALIZER; | 66 worker_task_runner = LAZY_INSTANCE_INITIALIZER; |
68 return worker_task_runner.Pointer(); | 67 return worker_task_runner.Pointer(); |
69 } | 68 } |
70 | 69 |
71 void WorkerTaskRunner::AddStopObserver(Observer* obs) { | 70 void WorkerTaskRunner::AddStopObserver(Observer* obs) { |
72 DCHECK(CurrentWorkerId() > 0); | 71 DCHECK(CurrentWorkerId() > 0); |
73 current_tls_.Get()->stop_observers_.AddObserver(obs); | 72 current_tls_.Get()->stop_observers_.AddObserver(obs); |
74 } | 73 } |
75 | 74 |
76 void WorkerTaskRunner::RemoveStopObserver(Observer* obs) { | 75 void WorkerTaskRunner::RemoveStopObserver(Observer* obs) { |
77 DCHECK(CurrentWorkerId() > 0); | 76 DCHECK(CurrentWorkerId() > 0); |
78 current_tls_.Get()->stop_observers_.RemoveObserver(obs); | 77 current_tls_.Get()->stop_observers_.RemoveObserver(obs); |
79 } | 78 } |
80 | 79 |
81 WorkerTaskRunner::~WorkerTaskRunner() { | 80 WorkerTaskRunner::~WorkerTaskRunner() { |
82 } | 81 } |
83 | 82 |
84 void WorkerTaskRunner::OnWorkerRunLoopStarted(const WebWorkerRunLoop& loop) { | 83 void WorkerTaskRunner::OnWorkerRunLoopStarted(const WebWorkerRunLoop& loop) { |
85 DCHECK(!current_tls_.Get()); | 84 DCHECK(!current_tls_.Get()); |
86 DCHECK(!base::PlatformThread::CurrentRef().is_null()); | 85 DCHECK(!base::PlatformThread::CurrentRef().is_null()); |
| 86 current_tls_.Set(new ThreadLocalState()); |
| 87 |
87 int id = base::PlatformThread::CurrentId(); | 88 int id = base::PlatformThread::CurrentId(); |
88 current_tls_.Set(new ThreadLocalState(id)); | |
89 | |
90 base::AutoLock locker_(loop_map_lock_); | 89 base::AutoLock locker_(loop_map_lock_); |
91 loop_map_[id] = loop; | 90 loop_map_[id] = loop; |
92 } | 91 } |
93 | 92 |
94 void WorkerTaskRunner::OnWorkerRunLoopStopped(const WebWorkerRunLoop& loop) { | 93 void WorkerTaskRunner::OnWorkerRunLoopStopped(const WebWorkerRunLoop& loop) { |
95 DCHECK(current_tls_.Get()); | 94 DCHECK(current_tls_.Get()); |
96 FOR_EACH_OBSERVER(Observer, current_tls_.Get()->stop_observers_, | 95 FOR_EACH_OBSERVER(Observer, current_tls_.Get()->stop_observers_, |
97 OnWorkerRunLoopStopped()); | 96 OnWorkerRunLoopStopped()); |
98 { | 97 { |
99 base::AutoLock locker(loop_map_lock_); | 98 base::AutoLock locker(loop_map_lock_); |
100 DCHECK(loop_map_[CurrentWorkerId()] == loop); | 99 DCHECK(loop_map_[CurrentWorkerId()] == loop); |
101 loop_map_.erase(CurrentWorkerId()); | 100 loop_map_.erase(CurrentWorkerId()); |
102 } | 101 } |
103 delete current_tls_.Get(); | 102 delete current_tls_.Get(); |
104 current_tls_.Set(NULL); | 103 current_tls_.Set(NULL); |
105 } | 104 } |
106 | 105 |
107 } // namespace content | 106 } // namespace content |
OLD | NEW |