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_BASE_BLOCKING_TASK_RUNNER_H_ |
6 #define CC_TREES_BLOCKING_TASK_RUNNER_H_ | 6 #define CC_BASE_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/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.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" | |
16 | 15 |
17 namespace cc { | 16 namespace cc { |
18 | 17 |
19 // This class wraps a SingleThreadTaskRunner but allows posted tasks to be | 18 // This class wraps a SingleThreadTaskRunner but allows posted tasks to be |
20 // run without a round trip through the message loop. This shortcutting | 19 // run without a round trip through the message loop. This shortcutting |
21 // removes guarantees about ordering. Tasks posted while the | 20 // removes guarantees about ordering. Tasks posted while the |
22 // BlockingTaskRunner is in a capturing state will run in order, and tasks | 21 // BlockingTaskRunner is in a capturing state will run in order, and tasks |
23 // posted while the BlockingTaskRunner is /not/ in a capturing state will | 22 // 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 | 23 // run in order, but the two sets of tasks will *not* run in order relative |
25 // to when they were posted. | 24 // to when they were posted. |
26 // | 25 // |
27 // To use this class, post tasks to the task runner returned by | 26 // To use this class, post tasks to the task runner returned by |
28 // BlockingTaskRunner::Create(). The thread it is created on identifies the | 27 // BlockingTaskRunner::Create(). The thread it is created on identifies the |
29 // thread you want the tasks to run on. The SingleThreadTaskRunner which is | 28 // thread you want the tasks to run on. The SingleThreadTaskRunner which is |
30 // passed into Create() is used to run tasks that are posted when not in a | 29 // passed into Create() is used to run tasks that are posted when not in a |
31 // capturing state. | 30 // capturing state. |
32 // | 31 // |
33 // Then, on the thread that the given task runner belongs to, you may | 32 // Then, on the thread that the given task runner belongs to, you may |
34 // instantiate a BlockingTaskRunner::CapturePostTasks. While this object | 33 // instantiate a BlockingTaskRunner::CapturePostTasks. While this object |
35 // exists, the task runner will collect any PostTasks called on it, posting | 34 // exists, the task runner will collect any PostTasks called on it, posting |
36 // tasks to that thread from anywhere. This CapturePostTasks object provides | 35 // tasks to that thread from anywhere. This CapturePostTasks object provides |
37 // a window in time where tasks can shortcut past the MessageLoop. As soon | 36 // a window in time where tasks can shortcut past the MessageLoop. As soon |
38 // as the CapturePostTasks object is destroyed (goes out of scope), all | 37 // as the CapturePostTasks object is destroyed (goes out of scope), all |
39 // tasks that had been posted to the thread during the window will be executed | 38 // tasks that had been posted to the thread during the window will be executed |
40 // immediately. | 39 // immediately. |
41 // | 40 // |
42 // Beware of re-entrancy, make sure the CapturePostTasks object is destroyed at | 41 // Beware of re-entrancy, make sure the CapturePostTasks object is destroyed at |
43 // a time when it makes sense for the embedder to call arbitrary things. | 42 // a time when it makes sense for the embedder to call arbitrary things. |
44 class CC_EXPORT BlockingTaskRunner { | 43 class BlockingTaskRunner { |
45 public: | 44 public: |
46 // Creates a BlockingTaskRunner for a given SingleThreadTaskRunner. | 45 // Creates a BlockingTaskRunner for a given SingleThreadTaskRunner. |
47 // |task_runner| will be used to run the tasks which are posted while we are | 46 // |task_runner| will be used to run the tasks which are posted while we are |
48 // not capturing. |task_runner| should belong to same the thread on which | 47 // not capturing. |task_runner| should belong to same the thread on which |
49 // capturing is done. | 48 // capturing is done. |
50 static scoped_ptr<BlockingTaskRunner> Create( | 49 static scoped_ptr<BlockingTaskRunner> Create( |
51 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | 50 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
52 | 51 |
53 ~BlockingTaskRunner(); | 52 ~BlockingTaskRunner(); |
54 | 53 |
55 // While an object of this type is held alive on a thread, any tasks | 54 // While an object of this type is held alive on a thread, any tasks |
56 // posted to the thread will be captured and run as soon as the object | 55 // posted to the thread will be captured and run as soon as the object |
57 // is destroyed, shortcutting past the task runner. | 56 // is destroyed, shortcutting past the task runner. |
58 class CC_EXPORT CapturePostTasks { | 57 class CapturePostTasks { |
59 public: | 58 public: |
60 explicit CapturePostTasks(BlockingTaskRunner* blocking_runner); | 59 explicit CapturePostTasks(BlockingTaskRunner* blocking_runner); |
61 ~CapturePostTasks(); | 60 ~CapturePostTasks(); |
62 | 61 |
63 private: | 62 private: |
64 BlockingTaskRunner* blocking_runner_; | 63 BlockingTaskRunner* blocking_runner_; |
65 | 64 |
66 DISALLOW_COPY_AND_ASSIGN(CapturePostTasks); | 65 DISALLOW_COPY_AND_ASSIGN(CapturePostTasks); |
67 }; | 66 }; |
68 | 67 |
(...skipping 19 matching lines...) Expand all Loading... |
88 | 87 |
89 base::Lock lock_; | 88 base::Lock lock_; |
90 int capture_; | 89 int capture_; |
91 std::vector<base::Closure> captured_tasks_; | 90 std::vector<base::Closure> captured_tasks_; |
92 | 91 |
93 DISALLOW_COPY_AND_ASSIGN(BlockingTaskRunner); | 92 DISALLOW_COPY_AND_ASSIGN(BlockingTaskRunner); |
94 }; | 93 }; |
95 | 94 |
96 } // namespace cc | 95 } // namespace cc |
97 | 96 |
98 #endif // CC_TREES_BLOCKING_TASK_RUNNER_H_ | 97 #endif // CC_BASE_BLOCKING_TASK_RUNNER_H_ |
OLD | NEW |