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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/child/worker_task_runner.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/child/worker_task_runner.cc
diff --git a/content/child/worker_task_runner.cc b/content/child/worker_task_runner.cc
index 02c4bab320f4145476bd1d5cee56bc02a45d1c21..a952135fab9126b385ffa50ed1fcd45c055c6d7b 100644
--- a/content/child/worker_task_runner.cc
+++ b/content/child/worker_task_runner.cc
@@ -16,11 +16,22 @@ namespace content {
namespace {
-class RunClosureTask : public WebWorkerRunLoop::Task {
+class RunLoopRunClosureTask : public WebWorkerRunLoop::Task {
+ public:
+ RunLoopRunClosureTask(const base::Closure& task) : task_(task) {}
+ virtual ~RunLoopRunClosureTask() {}
+ virtual void Run() {
+ task_.Run();
+ }
+ private:
+ base::Closure task_;
+};
+
+class RunClosureTask : public blink::WebThread::Task {
public:
RunClosureTask(const base::Closure& task) : task_(task) {}
virtual ~RunClosureTask() {}
- virtual void Run() {
+ virtual void run() {
task_.Run();
}
private:
@@ -31,10 +42,14 @@ class RunClosureTask : public WebWorkerRunLoop::Task {
struct WorkerTaskRunner::ThreadLocalState {
ThreadLocalState(int id, const WebWorkerRunLoop& loop)
- : id_(id), run_loop_(loop) {
+ : id_(id), run_loop_(loop), thread_(0) {
+ }
+ ThreadLocalState(int id, blink::WebThread* thread)
+ : id_(id), thread_(thread) {
}
int id_;
WebWorkerRunLoop run_loop_;
+ blink::WebThread* thread_;
ObserverList<WorkerTaskRunner::Observer> stop_observers_;
};
@@ -48,18 +63,31 @@ bool WorkerTaskRunner::PostTask(
int id, const base::Closure& closure) {
DCHECK(id > 0);
base::AutoLock locker(loop_map_lock_);
+
IDToLoopMap::iterator found = loop_map_.find(id);
- if (found == loop_map_.end())
- return false;
- return found->second.postTask(new RunClosureTask(closure));
+ if (found != loop_map_.end())
+ return found->second.postTask(new RunLoopRunClosureTask(closure));
+
+ IDToThreadMap::iterator thread_found = thread_map_.find(id);
+ if (thread_found == thread_map_.end()) {
+ thread_found->second->postTask(new RunClosureTask(closure));
+ return true;
+ }
+
+ return false;
}
int WorkerTaskRunner::PostTaskToAllThreads(const base::Closure& closure) {
base::AutoLock locker(loop_map_lock_);
IDToLoopMap::iterator it;
for (it = loop_map_.begin(); it != loop_map_.end(); ++it)
- it->second.postTask(new RunClosureTask(closure));
- return static_cast<int>(loop_map_.size());
+ it->second.postTask(new RunLoopRunClosureTask(closure));
+
+ IDToThreadMap::iterator iter;
+ for (iter = thread_map_.begin(); iter != thread_map_.end(); ++iter)
+ iter->second->postTask(new RunClosureTask(closure));
+
+ return static_cast<int>(loop_map_.size() + thread_map_.size());
}
int WorkerTaskRunner::CurrentWorkerId() {
@@ -109,4 +137,26 @@ void WorkerTaskRunner::OnWorkerRunLoopStopped(const WebWorkerRunLoop& loop) {
current_tls_.Set(NULL);
}
+void WorkerTaskRunner::OnWorkerThreadStarted(blink::WebThread* thread) {
+ DCHECK(!current_tls_.Get());
+ int id = id_sequence_.GetNext();
+ current_tls_.Set(new ThreadLocalState(id, thread));
+
+ base::AutoLock locker_(loop_map_lock_);
+ thread_map_[id] = thread;
+}
+
+void WorkerTaskRunner::OnWorkerThreadStopped(blink::WebThread* thread) {
+ DCHECK(current_tls_.Get());
+ FOR_EACH_OBSERVER(Observer, current_tls_.Get()->stop_observers_,
+ OnWorkerRunLoopStopped());
+ {
+ base::AutoLock locker(loop_map_lock_);
+ DCHECK(thread_map_[CurrentWorkerId()] == thread);
+ thread_map_.erase(CurrentWorkerId());
+ }
+ delete current_tls_.Get();
+ current_tls_.Set(NULL);
+}
+
} // namespace content
« 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