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" |
11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
12 | 12 |
13 using blink::WebWorkerRunLoop; | 13 using blink::WebWorkerRunLoop; |
14 | 14 |
15 namespace content { | 15 namespace content { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 class RunLoopRunClosureTask : public WebWorkerRunLoop::Task { | 19 class RunClosureTask : public WebWorkerRunLoop::Task { |
20 public: | 20 public: |
21 RunLoopRunClosureTask(const base::Closure& task) : task_(task) {} | 21 RunClosureTask(const base::Closure& task) : task_(task) {} |
22 virtual ~RunLoopRunClosureTask() {} | 22 virtual ~RunClosureTask() {} |
23 virtual void Run() { | 23 virtual void Run() { |
24 task_.Run(); | 24 task_.Run(); |
25 } | 25 } |
26 private: | 26 private: |
27 base::Closure task_; | |
28 }; | |
29 | |
30 class RunClosureTask : public blink::WebThread::Task { | |
31 public: | |
32 RunClosureTask(const base::Closure& task) : task_(task) {} | |
33 virtual ~RunClosureTask() {} | |
34 virtual void run() { | |
35 task_.Run(); | |
36 } | |
37 private: | |
38 base::Closure task_; | 27 base::Closure task_; |
39 }; | 28 }; |
40 | 29 |
41 } // namespace | 30 } // namespace |
42 | 31 |
43 struct WorkerTaskRunner::ThreadLocalState { | 32 struct WorkerTaskRunner::ThreadLocalState { |
44 ThreadLocalState(int id, const WebWorkerRunLoop& loop) | 33 ThreadLocalState(int id, const WebWorkerRunLoop& loop) |
45 : id_(id), run_loop_(loop), thread_(0) { | 34 : id_(id), run_loop_(loop) { |
46 } | |
47 ThreadLocalState(int id, blink::WebThread* thread) | |
48 : id_(id), thread_(thread) { | |
49 } | 35 } |
50 int id_; | 36 int id_; |
51 WebWorkerRunLoop run_loop_; | 37 WebWorkerRunLoop run_loop_; |
52 blink::WebThread* thread_; | |
53 ObserverList<WorkerTaskRunner::Observer> stop_observers_; | 38 ObserverList<WorkerTaskRunner::Observer> stop_observers_; |
54 }; | 39 }; |
55 | 40 |
56 WorkerTaskRunner::WorkerTaskRunner() { | 41 WorkerTaskRunner::WorkerTaskRunner() { |
57 // Start worker ids at 1, 0 is reserved for the main thread. | 42 // Start worker ids at 1, 0 is reserved for the main thread. |
58 int id = id_sequence_.GetNext(); | 43 int id = id_sequence_.GetNext(); |
59 DCHECK(!id); | 44 DCHECK(!id); |
60 } | 45 } |
61 | 46 |
62 bool WorkerTaskRunner::PostTask( | 47 bool WorkerTaskRunner::PostTask( |
63 int id, const base::Closure& closure) { | 48 int id, const base::Closure& closure) { |
64 DCHECK(id > 0); | 49 DCHECK(id > 0); |
65 base::AutoLock locker(loop_map_lock_); | 50 base::AutoLock locker(loop_map_lock_); |
66 | |
67 IDToLoopMap::iterator found = loop_map_.find(id); | 51 IDToLoopMap::iterator found = loop_map_.find(id); |
68 if (found != loop_map_.end()) | 52 if (found == loop_map_.end()) |
69 return found->second.postTask(new RunLoopRunClosureTask(closure)); | 53 return false; |
70 | 54 return found->second.postTask(new RunClosureTask(closure)); |
71 IDToThreadMap::iterator thread_found = thread_map_.find(id); | |
72 if (thread_found != thread_map_.end()) { | |
73 thread_found->second->postTask(new RunClosureTask(closure)); | |
74 return true; | |
75 } | |
76 | |
77 return false; | |
78 } | 55 } |
79 | 56 |
80 int WorkerTaskRunner::PostTaskToAllThreads(const base::Closure& closure) { | 57 int WorkerTaskRunner::PostTaskToAllThreads(const base::Closure& closure) { |
81 base::AutoLock locker(loop_map_lock_); | 58 base::AutoLock locker(loop_map_lock_); |
82 IDToLoopMap::iterator it; | 59 IDToLoopMap::iterator it; |
83 for (it = loop_map_.begin(); it != loop_map_.end(); ++it) | 60 for (it = loop_map_.begin(); it != loop_map_.end(); ++it) |
84 it->second.postTask(new RunLoopRunClosureTask(closure)); | 61 it->second.postTask(new RunClosureTask(closure)); |
85 | 62 return static_cast<int>(loop_map_.size()); |
86 IDToThreadMap::iterator iter; | |
87 for (iter = thread_map_.begin(); iter != thread_map_.end(); ++iter) | |
88 iter->second->postTask(new RunClosureTask(closure)); | |
89 | |
90 return static_cast<int>(loop_map_.size() + thread_map_.size()); | |
91 } | 63 } |
92 | 64 |
93 int WorkerTaskRunner::CurrentWorkerId() { | 65 int WorkerTaskRunner::CurrentWorkerId() { |
94 if (!current_tls_.Get()) | 66 if (!current_tls_.Get()) |
95 return 0; | 67 return 0; |
96 return current_tls_.Get()->id_; | 68 return current_tls_.Get()->id_; |
97 } | 69 } |
98 | 70 |
99 WorkerTaskRunner* WorkerTaskRunner::Instance() { | 71 WorkerTaskRunner* WorkerTaskRunner::Instance() { |
100 static base::LazyInstance<WorkerTaskRunner>::Leaky | 72 static base::LazyInstance<WorkerTaskRunner>::Leaky |
(...skipping 29 matching lines...) Expand all Loading... |
130 OnWorkerRunLoopStopped()); | 102 OnWorkerRunLoopStopped()); |
131 { | 103 { |
132 base::AutoLock locker(loop_map_lock_); | 104 base::AutoLock locker(loop_map_lock_); |
133 DCHECK(loop_map_[CurrentWorkerId()] == loop); | 105 DCHECK(loop_map_[CurrentWorkerId()] == loop); |
134 loop_map_.erase(CurrentWorkerId()); | 106 loop_map_.erase(CurrentWorkerId()); |
135 } | 107 } |
136 delete current_tls_.Get(); | 108 delete current_tls_.Get(); |
137 current_tls_.Set(NULL); | 109 current_tls_.Set(NULL); |
138 } | 110 } |
139 | 111 |
140 void WorkerTaskRunner::OnWorkerThreadStarted(blink::WebThread* thread) { | |
141 DCHECK(!current_tls_.Get()); | |
142 int id = id_sequence_.GetNext(); | |
143 current_tls_.Set(new ThreadLocalState(id, thread)); | |
144 | |
145 base::AutoLock locker_(loop_map_lock_); | |
146 thread_map_[id] = thread; | |
147 } | |
148 | |
149 void WorkerTaskRunner::OnWorkerThreadStopped(blink::WebThread* thread) { | |
150 DCHECK(current_tls_.Get()); | |
151 FOR_EACH_OBSERVER(Observer, current_tls_.Get()->stop_observers_, | |
152 OnWorkerRunLoopStopped()); | |
153 { | |
154 base::AutoLock locker(loop_map_lock_); | |
155 DCHECK(thread_map_[CurrentWorkerId()] == thread); | |
156 thread_map_.erase(CurrentWorkerId()); | |
157 } | |
158 delete current_tls_.Get(); | |
159 current_tls_.Set(NULL); | |
160 } | |
161 | |
162 } // namespace content | 112 } // namespace content |
OLD | NEW |