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 12 matching lines...) Expand all Loading... |
23 virtual void Run() { | 23 virtual void Run() { |
24 task_.Run(); | 24 task_.Run(); |
25 } | 25 } |
26 private: | 26 private: |
27 base::Closure task_; | 27 base::Closure task_; |
28 }; | 28 }; |
29 | 29 |
30 } // namespace | 30 } // namespace |
31 | 31 |
32 struct WorkerTaskRunner::ThreadLocalState { | 32 struct WorkerTaskRunner::ThreadLocalState { |
33 ThreadLocalState(int id, const WebWorkerRunLoop& loop) | 33 explicit ThreadLocalState(int id) : id_(id) {} |
34 : id_(id), run_loop_(loop) { | |
35 } | |
36 int id_; | 34 int id_; |
37 WebWorkerRunLoop run_loop_; | |
38 ObserverList<WorkerTaskRunner::Observer> stop_observers_; | 35 ObserverList<WorkerTaskRunner::Observer> stop_observers_; |
39 }; | 36 }; |
40 | 37 |
41 WorkerTaskRunner::WorkerTaskRunner() { | 38 WorkerTaskRunner::WorkerTaskRunner() { |
42 // Start worker ids at 1, 0 is reserved for the main thread. | 39 // Start worker ids at 1, 0 is reserved for the main thread. |
43 int id = id_sequence_.GetNext(); | 40 int id = id_sequence_.GetNext(); |
44 DCHECK(!id); | 41 DCHECK(!id); |
45 } | 42 } |
46 | 43 |
47 bool WorkerTaskRunner::PostTask( | 44 bool WorkerTaskRunner::PostTask( |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 DCHECK(CurrentWorkerId() > 0); | 80 DCHECK(CurrentWorkerId() > 0); |
84 current_tls_.Get()->stop_observers_.RemoveObserver(obs); | 81 current_tls_.Get()->stop_observers_.RemoveObserver(obs); |
85 } | 82 } |
86 | 83 |
87 WorkerTaskRunner::~WorkerTaskRunner() { | 84 WorkerTaskRunner::~WorkerTaskRunner() { |
88 } | 85 } |
89 | 86 |
90 void WorkerTaskRunner::OnWorkerRunLoopStarted(const WebWorkerRunLoop& loop) { | 87 void WorkerTaskRunner::OnWorkerRunLoopStarted(const WebWorkerRunLoop& loop) { |
91 DCHECK(!current_tls_.Get()); | 88 DCHECK(!current_tls_.Get()); |
92 int id = id_sequence_.GetNext(); | 89 int id = id_sequence_.GetNext(); |
93 current_tls_.Set(new ThreadLocalState(id, loop)); | 90 current_tls_.Set(new ThreadLocalState(id)); |
94 | 91 |
95 base::AutoLock locker_(loop_map_lock_); | 92 base::AutoLock locker_(loop_map_lock_); |
96 loop_map_[id] = loop; | 93 loop_map_[id] = loop; |
97 } | 94 } |
98 | 95 |
99 void WorkerTaskRunner::OnWorkerRunLoopStopped(const WebWorkerRunLoop& loop) { | 96 void WorkerTaskRunner::OnWorkerRunLoopStopped(const WebWorkerRunLoop& loop) { |
100 DCHECK(current_tls_.Get()); | 97 DCHECK(current_tls_.Get()); |
101 FOR_EACH_OBSERVER(Observer, current_tls_.Get()->stop_observers_, | 98 FOR_EACH_OBSERVER(Observer, current_tls_.Get()->stop_observers_, |
102 OnWorkerRunLoopStopped()); | 99 OnWorkerRunLoopStopped()); |
103 { | 100 { |
104 base::AutoLock locker(loop_map_lock_); | 101 base::AutoLock locker(loop_map_lock_); |
105 DCHECK(loop_map_[CurrentWorkerId()] == loop); | 102 DCHECK(loop_map_[CurrentWorkerId()] == loop); |
106 loop_map_.erase(CurrentWorkerId()); | 103 loop_map_.erase(CurrentWorkerId()); |
107 } | 104 } |
108 delete current_tls_.Get(); | 105 delete current_tls_.Get(); |
109 current_tls_.Set(NULL); | 106 current_tls_.Set(NULL); |
110 } | 107 } |
111 | 108 |
112 } // namespace content | 109 } // namespace content |
OLD | NEW |