OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "cc/trees/blocking_task_runner.h" | 5 #include "cc/trees/blocking_task_runner.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
11 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/message_loop/message_loop_proxy.h" |
12 | 12 |
13 namespace cc { | 13 namespace cc { |
14 | 14 |
15 typedef std::pair<base::SingleThreadTaskRunner*, | |
16 scoped_refptr<BlockingTaskRunner> > TaskRunnerPair; | |
17 | |
18 struct TaskRunnerPairs { | 15 struct TaskRunnerPairs { |
19 static TaskRunnerPairs* GetInstance() { | 16 static TaskRunnerPairs* GetInstance() { |
20 return Singleton<TaskRunnerPairs>::get(); | 17 return Singleton<TaskRunnerPairs>::get(); |
21 } | 18 } |
22 | 19 |
23 base::Lock lock; | 20 base::Lock lock; |
24 std::vector<TaskRunnerPair> pairs; | 21 std::vector<scoped_refptr<BlockingTaskRunner> > pairs; |
25 | 22 |
26 private: | 23 private: |
27 friend struct DefaultSingletonTraits<TaskRunnerPairs>; | 24 friend struct DefaultSingletonTraits<TaskRunnerPairs>; |
28 }; | 25 }; |
29 | 26 |
30 // static | 27 // static |
31 scoped_refptr<BlockingTaskRunner> BlockingTaskRunner::current() { | 28 scoped_refptr<BlockingTaskRunner> BlockingTaskRunner::current() { |
32 TaskRunnerPairs* task_runners = TaskRunnerPairs::GetInstance(); | 29 TaskRunnerPairs* task_runners = TaskRunnerPairs::GetInstance(); |
| 30 base::PlatformThreadId thread_id = base::PlatformThread::CurrentId(); |
33 | 31 |
34 base::AutoLock lock(task_runners->lock); | 32 base::AutoLock lock(task_runners->lock); |
35 | 33 |
| 34 // TODO(boliu): Leaks |
36 for (size_t i = 0; i < task_runners->pairs.size(); ++i) { | 35 for (size_t i = 0; i < task_runners->pairs.size(); ++i) { |
37 if (task_runners->pairs[i].first->HasOneRef()) { | 36 if (task_runners->pairs[i]->thread_id_ == thread_id) |
38 // The SingleThreadTaskRunner is kept alive by its MessageLoop, and we | 37 return task_runners->pairs[i]; |
39 // hold a second reference in the TaskRunnerPairs array. If the | |
40 // SingleThreadTaskRunner has one ref, then it is being held alive only | |
41 // by the BlockingTaskRunner and the MessageLoop is gone, so drop the | |
42 // BlockingTaskRunner from the TaskRunnerPairs array along with the | |
43 // SingleThreadTaskRunner. | |
44 task_runners->pairs.erase(task_runners->pairs.begin() + i); | |
45 --i; | |
46 } | |
47 } | 38 } |
48 | 39 |
49 scoped_refptr<base::SingleThreadTaskRunner> current = | 40 scoped_refptr<BlockingTaskRunner> runner = |
50 base::MessageLoopProxy::current(); | 41 new BlockingTaskRunner(base::MessageLoopProxy::current()); |
51 for (size_t i = 0; i < task_runners->pairs.size(); ++i) { | 42 task_runners->pairs.push_back(runner); |
52 if (task_runners->pairs[i].first == current.get()) | |
53 return task_runners->pairs[i].second.get(); | |
54 } | |
55 | |
56 scoped_refptr<BlockingTaskRunner> runner = new BlockingTaskRunner(current); | |
57 task_runners->pairs.push_back(TaskRunnerPair(current, runner)); | |
58 return runner; | 43 return runner; |
59 } | 44 } |
60 | 45 |
61 BlockingTaskRunner::BlockingTaskRunner( | 46 BlockingTaskRunner::BlockingTaskRunner( |
62 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 47 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
63 : task_runner_(task_runner), capture_(0) {} | 48 : thread_id_(base::PlatformThread::CurrentId()), |
| 49 task_runner_(task_runner), |
| 50 capture_(0) { |
| 51 } |
64 | 52 |
65 BlockingTaskRunner::~BlockingTaskRunner() {} | 53 BlockingTaskRunner::~BlockingTaskRunner() {} |
66 | 54 |
| 55 bool BlockingTaskRunner::BelongsToCurrentThread() { |
| 56 return base::PlatformThread::CurrentId() == thread_id_; |
| 57 } |
| 58 |
67 bool BlockingTaskRunner::PostTask(const tracked_objects::Location& from_here, | 59 bool BlockingTaskRunner::PostTask(const tracked_objects::Location& from_here, |
68 const base::Closure& task) { | 60 const base::Closure& task) { |
69 base::AutoLock lock(lock_); | 61 base::AutoLock lock(lock_); |
70 if (!capture_) | 62 if (!capture_) |
71 return task_runner_->PostTask(from_here, task); | 63 return task_runner_->PostTask(from_here, task); |
72 captured_tasks_.push_back(task); | 64 captured_tasks_.push_back(task); |
73 return true; | 65 return true; |
74 } | 66 } |
75 | 67 |
76 void BlockingTaskRunner::SetCapture(bool capture) { | 68 void BlockingTaskRunner::SetCapture(bool capture) { |
(...skipping 19 matching lines...) Expand all Loading... |
96 BlockingTaskRunner::CapturePostTasks::CapturePostTasks() | 88 BlockingTaskRunner::CapturePostTasks::CapturePostTasks() |
97 : blocking_runner_(BlockingTaskRunner::current()) { | 89 : blocking_runner_(BlockingTaskRunner::current()) { |
98 blocking_runner_->SetCapture(true); | 90 blocking_runner_->SetCapture(true); |
99 } | 91 } |
100 | 92 |
101 BlockingTaskRunner::CapturePostTasks::~CapturePostTasks() { | 93 BlockingTaskRunner::CapturePostTasks::~CapturePostTasks() { |
102 blocking_runner_->SetCapture(false); | 94 blocking_runner_->SetCapture(false); |
103 } | 95 } |
104 | 96 |
105 } // namespace cc | 97 } // namespace cc |
OLD | NEW |