Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: content/child/worker_task_runner.cc

Issue 393283003: Add WorkerThread based methods for start/stop of worker threads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add math.h to make gn build happy. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/child/worker_task_runner.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 RunClosureTask : public WebWorkerRunLoop::Task { 19 class RunLoopRunClosureTask : public WebWorkerRunLoop::Task {
20 public: 20 public:
21 RunClosureTask(const base::Closure& task) : task_(task) {} 21 RunLoopRunClosureTask(const base::Closure& task) : task_(task) {}
22 virtual ~RunClosureTask() {} 22 virtual ~RunLoopRunClosureTask() {}
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:
27 base::Closure task_; 38 base::Closure task_;
28 }; 39 };
29 40
30 } // namespace 41 } // namespace
31 42
32 struct WorkerTaskRunner::ThreadLocalState { 43 struct WorkerTaskRunner::ThreadLocalState {
33 ThreadLocalState(int id, const WebWorkerRunLoop& loop) 44 ThreadLocalState(int id, const WebWorkerRunLoop& loop)
34 : id_(id), run_loop_(loop) { 45 : id_(id), run_loop_(loop), thread_(0) {
46 }
47 ThreadLocalState(int id, blink::WebThread* thread)
48 : id_(id), thread_(thread) {
35 } 49 }
36 int id_; 50 int id_;
37 WebWorkerRunLoop run_loop_; 51 WebWorkerRunLoop run_loop_;
52 blink::WebThread* thread_;
38 ObserverList<WorkerTaskRunner::Observer> stop_observers_; 53 ObserverList<WorkerTaskRunner::Observer> stop_observers_;
39 }; 54 };
40 55
41 WorkerTaskRunner::WorkerTaskRunner() { 56 WorkerTaskRunner::WorkerTaskRunner() {
42 // Start worker ids at 1, 0 is reserved for the main thread. 57 // Start worker ids at 1, 0 is reserved for the main thread.
43 int id = id_sequence_.GetNext(); 58 int id = id_sequence_.GetNext();
44 DCHECK(!id); 59 DCHECK(!id);
45 } 60 }
46 61
47 bool WorkerTaskRunner::PostTask( 62 bool WorkerTaskRunner::PostTask(
48 int id, const base::Closure& closure) { 63 int id, const base::Closure& closure) {
49 DCHECK(id > 0); 64 DCHECK(id > 0);
50 base::AutoLock locker(loop_map_lock_); 65 base::AutoLock locker(loop_map_lock_);
66
51 IDToLoopMap::iterator found = loop_map_.find(id); 67 IDToLoopMap::iterator found = loop_map_.find(id);
52 if (found == loop_map_.end()) 68 if (found != loop_map_.end())
53 return false; 69 return found->second.postTask(new RunLoopRunClosureTask(closure));
54 return found->second.postTask(new RunClosureTask(closure)); 70
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;
55 } 78 }
56 79
57 int WorkerTaskRunner::PostTaskToAllThreads(const base::Closure& closure) { 80 int WorkerTaskRunner::PostTaskToAllThreads(const base::Closure& closure) {
58 base::AutoLock locker(loop_map_lock_); 81 base::AutoLock locker(loop_map_lock_);
59 IDToLoopMap::iterator it; 82 IDToLoopMap::iterator it;
60 for (it = loop_map_.begin(); it != loop_map_.end(); ++it) 83 for (it = loop_map_.begin(); it != loop_map_.end(); ++it)
61 it->second.postTask(new RunClosureTask(closure)); 84 it->second.postTask(new RunLoopRunClosureTask(closure));
62 return static_cast<int>(loop_map_.size()); 85
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());
63 } 91 }
64 92
65 int WorkerTaskRunner::CurrentWorkerId() { 93 int WorkerTaskRunner::CurrentWorkerId() {
66 if (!current_tls_.Get()) 94 if (!current_tls_.Get())
67 return 0; 95 return 0;
68 return current_tls_.Get()->id_; 96 return current_tls_.Get()->id_;
69 } 97 }
70 98
71 WorkerTaskRunner* WorkerTaskRunner::Instance() { 99 WorkerTaskRunner* WorkerTaskRunner::Instance() {
72 static base::LazyInstance<WorkerTaskRunner>::Leaky 100 static base::LazyInstance<WorkerTaskRunner>::Leaky
(...skipping 29 matching lines...) Expand all
102 OnWorkerRunLoopStopped()); 130 OnWorkerRunLoopStopped());
103 { 131 {
104 base::AutoLock locker(loop_map_lock_); 132 base::AutoLock locker(loop_map_lock_);
105 DCHECK(loop_map_[CurrentWorkerId()] == loop); 133 DCHECK(loop_map_[CurrentWorkerId()] == loop);
106 loop_map_.erase(CurrentWorkerId()); 134 loop_map_.erase(CurrentWorkerId());
107 } 135 }
108 delete current_tls_.Get(); 136 delete current_tls_.Get();
109 current_tls_.Set(NULL); 137 current_tls_.Set(NULL);
110 } 138 }
111 139
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
112 } // namespace content 162 } // namespace content
OLDNEW
« no previous file with comments | « content/child/worker_task_runner.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698