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 18 matching lines...) Expand all Loading... |
29 | 29 |
30 } // namespace | 30 } // namespace |
31 | 31 |
32 struct WorkerTaskRunner::ThreadLocalState { | 32 struct WorkerTaskRunner::ThreadLocalState { |
33 explicit ThreadLocalState(int id) : id_(id) {} | 33 explicit ThreadLocalState(int id) : id_(id) {} |
34 int id_; | 34 int id_; |
35 ObserverList<WorkerTaskRunner::Observer> stop_observers_; | 35 ObserverList<WorkerTaskRunner::Observer> stop_observers_; |
36 }; | 36 }; |
37 | 37 |
38 WorkerTaskRunner::WorkerTaskRunner() { | 38 WorkerTaskRunner::WorkerTaskRunner() { |
39 // Start worker ids at 1, 0 is reserved for the main thread. | |
40 int id = id_sequence_.GetNext(); | |
41 DCHECK(!id); | |
42 } | 39 } |
43 | 40 |
44 bool WorkerTaskRunner::PostTask( | 41 bool WorkerTaskRunner::PostTask( |
45 int id, const base::Closure& closure) { | 42 int id, const base::Closure& closure) { |
46 DCHECK(id > 0); | 43 DCHECK(id > 0); |
47 base::AutoLock locker(loop_map_lock_); | 44 base::AutoLock locker(loop_map_lock_); |
48 IDToLoopMap::iterator found = loop_map_.find(id); | 45 IDToLoopMap::iterator found = loop_map_.find(id); |
49 if (found == loop_map_.end()) | 46 if (found == loop_map_.end()) |
50 return false; | 47 return false; |
51 return found->second.postTask(new RunClosureTask(closure)); | 48 return found->second.postTask(new RunClosureTask(closure)); |
(...skipping 27 matching lines...) Expand all Loading... |
79 void WorkerTaskRunner::RemoveStopObserver(Observer* obs) { | 76 void WorkerTaskRunner::RemoveStopObserver(Observer* obs) { |
80 DCHECK(CurrentWorkerId() > 0); | 77 DCHECK(CurrentWorkerId() > 0); |
81 current_tls_.Get()->stop_observers_.RemoveObserver(obs); | 78 current_tls_.Get()->stop_observers_.RemoveObserver(obs); |
82 } | 79 } |
83 | 80 |
84 WorkerTaskRunner::~WorkerTaskRunner() { | 81 WorkerTaskRunner::~WorkerTaskRunner() { |
85 } | 82 } |
86 | 83 |
87 void WorkerTaskRunner::OnWorkerRunLoopStarted(const WebWorkerRunLoop& loop) { | 84 void WorkerTaskRunner::OnWorkerRunLoopStarted(const WebWorkerRunLoop& loop) { |
88 DCHECK(!current_tls_.Get()); | 85 DCHECK(!current_tls_.Get()); |
89 int id = id_sequence_.GetNext(); | 86 DCHECK(!base::PlatformThread::CurrentRef().is_null()); |
| 87 int id = base::PlatformThread::CurrentId(); |
90 current_tls_.Set(new ThreadLocalState(id)); | 88 current_tls_.Set(new ThreadLocalState(id)); |
91 | 89 |
92 base::AutoLock locker_(loop_map_lock_); | 90 base::AutoLock locker_(loop_map_lock_); |
93 loop_map_[id] = loop; | 91 loop_map_[id] = loop; |
94 } | 92 } |
95 | 93 |
96 void WorkerTaskRunner::OnWorkerRunLoopStopped(const WebWorkerRunLoop& loop) { | 94 void WorkerTaskRunner::OnWorkerRunLoopStopped(const WebWorkerRunLoop& loop) { |
97 DCHECK(current_tls_.Get()); | 95 DCHECK(current_tls_.Get()); |
98 FOR_EACH_OBSERVER(Observer, current_tls_.Get()->stop_observers_, | 96 FOR_EACH_OBSERVER(Observer, current_tls_.Get()->stop_observers_, |
99 OnWorkerRunLoopStopped()); | 97 OnWorkerRunLoopStopped()); |
100 { | 98 { |
101 base::AutoLock locker(loop_map_lock_); | 99 base::AutoLock locker(loop_map_lock_); |
102 DCHECK(loop_map_[CurrentWorkerId()] == loop); | 100 DCHECK(loop_map_[CurrentWorkerId()] == loop); |
103 loop_map_.erase(CurrentWorkerId()); | 101 loop_map_.erase(CurrentWorkerId()); |
104 } | 102 } |
105 delete current_tls_.Get(); | 103 delete current_tls_.Get(); |
106 current_tls_.Set(NULL); | 104 current_tls_.Set(NULL); |
107 } | 105 } |
108 | 106 |
109 } // namespace content | 107 } // namespace content |
OLD | NEW |