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 #ifndef CC_TREES_BLOCKING_TASK_RUNNER_H_ | 5 #ifndef CC_TREES_BLOCKING_TASK_RUNNER_H_ |
6 #define CC_TREES_BLOCKING_TASK_RUNNER_H_ | 6 #define CC_TREES_BLOCKING_TASK_RUNNER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
14 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
15 #include "cc/base/cc_export.h" | 15 #include "cc/base/cc_export.h" |
16 | 16 |
17 namespace cc { | 17 namespace cc { |
18 | 18 |
19 // This class wraps a SingleThreadTaskRunner but allows posted tasks to be | 19 // This class wraps a SingleThreadTaskRunner but allows posted tasks to be |
20 // run without a round trip through the message loop. This shortcutting | 20 // run without a round trip through the message loop. This shortcutting |
21 // removes guarantees about ordering. Tasks posted while the | 21 // removes guarantees about ordering. Tasks posted while the |
22 // BlockingTaskRunner is in a capturing state will run in order, and tasks | 22 // BlockingTaskRunner is in a capturing state will run in order, and tasks |
23 // posted while the BlockingTaskRunner is /not/ in a capturing state will | 23 // posted while the BlockingTaskRunner is /not/ in a capturing state will |
24 // run in order, but the two sets of tasks will *not* run in order relative | 24 // run in order, but the two sets of tasks will *not* run in order relative |
25 // to when they were posted. | 25 // to when they were posted. |
26 // | 26 // |
27 // To use this class, post tasks to the task runner returned by | 27 // To use this class, post tasks to the task runner returned by |
28 // BlockingTaskRunner::current() on the thread you want the tasks to run. | 28 // BlockingTaskRunner::Create() with the SingleThreadTaskRunner for the thread |
danakj
2014/08/28 17:10:46
i think this sentence is awkward. period after Cre
Sami
2014/08/28 18:21:16
Yah, not sure what I was going for there. Rewritte
| |
29 // Hold a reference to the BlockingTaskRunner as long as you intend to | 29 // you want the tasks to run. |
30 // post tasks to it. | |
31 // | 30 // |
32 // Then, on the thread from which the BlockingTaskRunner was created, you | 31 // Then, on the thread that the given task runner belongs to, you may |
33 // may instantiate a BlockingTaskRunner::CapturePostTasks. While this object | 32 // instantiate a BlockingTaskRunner::CapturePostTasks. While this object |
34 // exists, the task runner will collect any PostTasks called on it, posting | 33 // exists, the task runner will collect any PostTasks called on it, posting |
35 // tasks to that thread from anywhere. This CapturePostTasks object provides | 34 // tasks to that thread from anywhere. This CapturePostTasks object provides |
36 // a window in time where tasks can shortcut past the MessageLoop. As soon | 35 // a window in time where tasks can shortcut past the MessageLoop. As soon |
37 // as the CapturePostTasks object is destroyed (goes out of scope), all | 36 // as the CapturePostTasks object is destroyed (goes out of scope), all |
38 // tasks that had been posted to the thread during the window will be exectuted | 37 // tasks that had been posted to the thread during the window will be executed |
39 // immediately. | 38 // immediately. |
40 // | 39 // |
41 // Beware of re-entrancy, make sure the CapturePostTasks object is destroyed at | 40 // Beware of re-entrancy, make sure the CapturePostTasks object is destroyed at |
42 // a time when it makes sense for the embedder to call arbitrary things. | 41 // a time when it makes sense for the embedder to call arbitrary things. |
43 class CC_EXPORT BlockingTaskRunner | 42 class CC_EXPORT BlockingTaskRunner |
44 : public base::RefCountedThreadSafe<BlockingTaskRunner> { | 43 : public base::RefCountedThreadSafe<BlockingTaskRunner> { |
45 public: | 44 public: |
46 // Returns the BlockingTaskRunner for the current thread, creating one if | 45 // Creates a BlockingTaskRunner for a given SingleThreadTaskRunner. |
47 // necessary. | 46 // |task_runner| will be used to run the tasks which are posted while we are |
48 static scoped_refptr<BlockingTaskRunner> current(); | 47 // not capturing. |task_runner| should belong to same the thread on which |
48 // capturing is done. | |
49 static scoped_refptr<BlockingTaskRunner> Create( | |
50 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
danakj
2014/08/28 17:10:46
I'm starting to wonder if this class needs to be r
Sami
2014/08/28 18:21:17
Great idea, the ownership is much simpler now so a
| |
49 | 51 |
50 // While an object of this type is held alive on a thread, any tasks | 52 // While an object of this type is held alive on a thread, any tasks |
51 // posted to the thread will be captured and run as soon as the object | 53 // posted to the thread will be captured and run as soon as the object |
52 // is destroyed, shortcutting past the MessageLoop. | 54 // is destroyed, shortcutting past the task runner. |
53 class CC_EXPORT CapturePostTasks { | 55 class CC_EXPORT CapturePostTasks { |
54 public: | 56 public: |
55 CapturePostTasks(); | 57 explicit CapturePostTasks( |
58 scoped_refptr<BlockingTaskRunner> blocking_runner); | |
danakj
2014/08/28 17:10:46
make this a raw pointer
Sami
2014/08/28 18:21:16
Done.
| |
56 ~CapturePostTasks(); | 59 ~CapturePostTasks(); |
57 | 60 |
58 private: | 61 private: |
59 scoped_refptr<BlockingTaskRunner> blocking_runner_; | 62 scoped_refptr<BlockingTaskRunner> blocking_runner_; |
danakj
2014/08/28 17:10:46
and this
Sami
2014/08/28 18:21:16
Done.
| |
60 | 63 |
61 DISALLOW_COPY_AND_ASSIGN(CapturePostTasks); | 64 DISALLOW_COPY_AND_ASSIGN(CapturePostTasks); |
62 }; | 65 }; |
63 | 66 |
64 // True if tasks posted to the BlockingTaskRunner will run on the current | 67 // True if tasks posted to the BlockingTaskRunner will run on the current |
65 // thread. | 68 // thread. |
66 bool BelongsToCurrentThread(); | 69 bool BelongsToCurrentThread(); |
67 | 70 |
68 // Posts a task using the contained SingleThreadTaskRunner unless |capture_| | 71 // Posts a task using the contained SingleThreadTaskRunner unless |capture_| |
69 // is true. When |capture_| is true, tasks posted will be caught and stored | 72 // is true. When |capture_| is true, tasks posted will be caught and stored |
70 // until the capturing stops. At that time the tasks will be run directly | 73 // until the capturing stops. At that time the tasks will be run directly |
71 // instead of being posted to the SingleThreadTaskRunner. | 74 // instead of being posted to the underlying task runner. |
72 bool PostTask(const tracked_objects::Location& from_here, | 75 bool PostTask(const tracked_objects::Location& from_here, |
73 const base::Closure& task); | 76 const base::Closure& task); |
74 | 77 |
75 private: | 78 private: |
76 friend class base::RefCountedThreadSafe<BlockingTaskRunner>; | 79 friend class base::RefCountedThreadSafe<BlockingTaskRunner>; |
77 | 80 |
78 explicit BlockingTaskRunner( | 81 explicit BlockingTaskRunner( |
79 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | 82 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
80 virtual ~BlockingTaskRunner(); | 83 virtual ~BlockingTaskRunner(); |
81 | 84 |
82 void SetCapture(bool capture); | 85 void SetCapture(bool capture); |
83 | 86 |
84 base::PlatformThreadId thread_id_; | |
85 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 87 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
86 | 88 |
87 base::Lock lock_; | 89 base::Lock lock_; |
88 int capture_; | 90 int capture_; |
89 std::vector<base::Closure> captured_tasks_; | 91 std::vector<base::Closure> captured_tasks_; |
90 }; | 92 }; |
91 | 93 |
92 } // namespace cc | 94 } // namespace cc |
93 | 95 |
94 #endif // CC_TREES_BLOCKING_TASK_RUNNER_H_ | 96 #endif // CC_TREES_BLOCKING_TASK_RUNNER_H_ |
OLD | NEW |