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

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

Issue 8785013: Track webcore worker message loops in chromium. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add static accessor to WorkerTaskRunner, use custom ObserverList Created 9 years 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
OLDNEW
(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 "content/common/worker_task_runner.h"
9
10 using WebKit::WebWorkerRunLoop;
11
12 namespace {
13
14 class RunClosureTask : public WebKit::WebWorkerRunLoop::Task {
15 public:
16 RunClosureTask(const base::Closure& task) : task_(task) { }
17 virtual ~RunClosureTask() { }
18 virtual void Run() {
19 task_.Run();
20 }
21 private:
22 base::Closure task_;
23 };
24
25 }
26
27 WorkerTaskRunner::WorkerTaskRunner() : id_sequence_(0) {
28 }
29
30 void WorkerTaskRunner::PostTask(
31 int id, const base::Closure& closure) {
32 DCHECK(id > 0);
33 base::AutoLock locker_(lock_);
34 IDToLoopMap::iterator found = loop_map_.find(id);
35 if (found != loop_map_.end())
36 found->second.postTask(new RunClosureTask(closure));
37 }
38
39 int WorkerTaskRunner::CurrentWorkerId() {
40 if (!current_tls_.Get())
41 return 0;
42 return current_tls_.Get()->first;
43 }
44
45 WorkerTaskRunner* WorkerTaskRunner::Instance() {
46 static base::LazyInstance<WorkerTaskRunner,
47 base::LeakyLazyInstanceTraits<WorkerTaskRunner> >
48 worker_task_runner_;
michaeln 2011/12/08 03:10:47 other usages of LazyInstance assign the LAZY_INSTA
dgrogan 2011/12/09 03:20:41 You're right. Changed.
49 return worker_task_runner_.Pointer();
50 }
51
52 void WorkerTaskRunner::AddObserver(Observer* obs) {
53 base::AutoLock locker(observer_lock_);
54 observer_list_.insert(obs);
55 }
56
57 int WorkerTaskRunner::RemoveObserver(Observer* obs) {
58 base::AutoLock locker(observer_lock_);
59 return observer_list_.erase(obs);
60 }
61
62 WorkerTaskRunner::~WorkerTaskRunner() {
63 }
64
65 void WorkerTaskRunner::OnWorkerRunLoopStarted(const WebWorkerRunLoop& loop) {
66 DCHECK(!current_tls_.Get());
67 {
68 base::AutoLock locker_(lock_);
69 int id = ++id_sequence_;
70 current_tls_.Set(new std::pair<int, WebWorkerRunLoop>(id, loop));
71 loop_map_[id] = loop;
72 }
73 base::AutoLock locker(observer_lock_);
74 for(ObserverList::iterator iter = observer_list_.begin();
75 iter != observer_list_.end(); iter++) {
76 (*iter)->OnWorkerRunLoopStarted();
darin (slow to review) 2011/12/08 22:41:21 It is usually bad design to hold locks while calli
michaeln 2011/12/09 00:43:32 This is generally true, but also not much an issue
dgrogan 2011/12/09 03:20:41 Darin and I talked about this today, mostly in the
77 }
78 }
79
80 void WorkerTaskRunner::OnWorkerRunLoopStopped(
81 const WebWorkerRunLoop& loop) {
82 DCHECK(CurrentWorkerId() > 0);
83 DCHECK(current_tls_.Get());
84 {
85 base::AutoLock lock(observer_lock_);
86 for(ObserverList::iterator iter = observer_list_.begin();
87 iter != observer_list_.end(); iter++) {
88 (*iter)->OnWorkerRunLoopStopped();
89 }
90 }
91 base::AutoLock locker_(lock_);
92 DCHECK(loop_map_[CurrentWorkerId()] == loop);
93 loop_map_.erase(CurrentWorkerId());
94 delete current_tls_.Get();
95 current_tls_.Set(NULL);
96 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698