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> > runners; |
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 |
36 for (size_t i = 0; i < task_runners->pairs.size(); ++i) { | 34 scoped_refptr<BlockingTaskRunner> current_task_runner; |
37 if (task_runners->pairs[i].first->HasOneRef()) { | 35 |
38 // The SingleThreadTaskRunner is kept alive by its MessageLoop, and we | 36 for (size_t i = 0; i < task_runners->runners.size(); ++i) { |
39 // hold a second reference in the TaskRunnerPairs array. If the | 37 if (task_runners->runners[i]->thread_id_ == thread_id) { |
40 // SingleThreadTaskRunner has one ref, then it is being held alive only | 38 current_task_runner = task_runners->runners[i]; |
41 // by the BlockingTaskRunner and the MessageLoop is gone, so drop the | 39 } else if (task_runners->runners[i]->HasOneRef()) { |
42 // BlockingTaskRunner from the TaskRunnerPairs array along with the | 40 task_runners->runners.erase(task_runners->runners.begin() + i); |
43 // SingleThreadTaskRunner. | 41 i--; |
44 task_runners->pairs.erase(task_runners->pairs.begin() + i); | |
45 --i; | |
46 } | 42 } |
47 } | 43 } |
48 | 44 |
49 scoped_refptr<base::SingleThreadTaskRunner> current = | 45 if (current_task_runner) |
50 base::MessageLoopProxy::current(); | 46 return current_task_runner; |
51 for (size_t i = 0; i < task_runners->pairs.size(); ++i) { | |
52 if (task_runners->pairs[i].first == current.get()) | |
53 return task_runners->pairs[i].second.get(); | |
54 } | |
55 | 47 |
56 scoped_refptr<BlockingTaskRunner> runner = new BlockingTaskRunner(current); | 48 scoped_refptr<BlockingTaskRunner> runner = |
57 task_runners->pairs.push_back(TaskRunnerPair(current, runner)); | 49 new BlockingTaskRunner(base::MessageLoopProxy::current()); |
50 task_runners->runners.push_back(runner); | |
58 return runner; | 51 return runner; |
59 } | 52 } |
60 | 53 |
61 BlockingTaskRunner::BlockingTaskRunner( | 54 BlockingTaskRunner::BlockingTaskRunner( |
62 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 55 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
63 : task_runner_(task_runner), capture_(0) {} | 56 : thread_id_(base::PlatformThread::CurrentId()), |
57 task_runner_(task_runner), | |
58 capture_(0) { | |
59 } | |
64 | 60 |
65 BlockingTaskRunner::~BlockingTaskRunner() {} | 61 BlockingTaskRunner::~BlockingTaskRunner() {} |
66 | 62 |
63 bool BlockingTaskRunner::BelongsToCurrentThread() { | |
64 return base::PlatformThread::CurrentId() == thread_id_; | |
65 } | |
66 | |
67 bool BlockingTaskRunner::PostTask(const tracked_objects::Location& from_here, | 67 bool BlockingTaskRunner::PostTask(const tracked_objects::Location& from_here, |
68 const base::Closure& task) { | 68 const base::Closure& task) { |
69 base::AutoLock lock(lock_); | 69 base::AutoLock lock(lock_); |
70 if (!capture_) | 70 if (!capture_) |
71 return task_runner_->PostTask(from_here, task); | 71 return task_runner_->PostTask(from_here, task); |
piman
2014/05/17 00:32:31
How is this going to work if you don't have a Mess
boliu
2014/05/17 00:34:25
It can't. All PostTask must be wrapped by CaptureP
piman
2014/05/17 00:51:47
Is that true today for SingleThreadProxy? (It can'
boliu
2014/05/17 01:08:54
Right, this definitely can't work for threaded com
| |
72 captured_tasks_.push_back(task); | 72 captured_tasks_.push_back(task); |
73 return true; | 73 return true; |
74 } | 74 } |
75 | 75 |
76 void BlockingTaskRunner::SetCapture(bool capture) { | 76 void BlockingTaskRunner::SetCapture(bool capture) { |
77 DCHECK(BelongsToCurrentThread()); | 77 DCHECK(BelongsToCurrentThread()); |
78 | 78 |
79 std::vector<base::Closure> tasks; | 79 std::vector<base::Closure> tasks; |
80 | 80 |
81 { | 81 { |
(...skipping 14 matching lines...) Expand all Loading... | |
96 BlockingTaskRunner::CapturePostTasks::CapturePostTasks() | 96 BlockingTaskRunner::CapturePostTasks::CapturePostTasks() |
97 : blocking_runner_(BlockingTaskRunner::current()) { | 97 : blocking_runner_(BlockingTaskRunner::current()) { |
98 blocking_runner_->SetCapture(true); | 98 blocking_runner_->SetCapture(true); |
99 } | 99 } |
100 | 100 |
101 BlockingTaskRunner::CapturePostTasks::~CapturePostTasks() { | 101 BlockingTaskRunner::CapturePostTasks::~CapturePostTasks() { |
102 blocking_runner_->SetCapture(false); | 102 blocking_runner_->SetCapture(false); |
103 } | 103 } |
104 | 104 |
105 } // namespace cc | 105 } // namespace cc |
OLD | NEW |