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 #ifndef COMPONENTS_SCHEDULER_CHILD_SINGLE_THREAD_IDLE_TASK_RUNNER_H_ | 5 #ifndef COMPONENTS_SCHEDULER_CHILD_SINGLE_THREAD_IDLE_TASK_RUNNER_H_ |
6 #define COMPONENTS_SCHEDULER_CHILD_SINGLE_THREAD_IDLE_TASK_RUNNER_H_ | 6 #define COMPONENTS_SCHEDULER_CHILD_SINGLE_THREAD_IDLE_TASK_RUNNER_H_ |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "components/scheduler/scheduler_export.h" |
13 | 14 |
14 namespace scheduler { | 15 namespace scheduler { |
15 | 16 |
16 // A SingleThreadIdleTaskRunner is a task runner for running idle tasks. Idle | 17 // A SingleThreadIdleTaskRunner is a task runner for running idle tasks. Idle |
17 // tasks have an unbound argument which is bound to a deadline | 18 // tasks have an unbound argument which is bound to a deadline |
18 // (in base::TimeTicks) when they are run. The idle task is expected to | 19 // (in base::TimeTicks) when they are run. The idle task is expected to |
19 // complete by this deadline. | 20 // complete by this deadline. |
20 class SingleThreadIdleTaskRunner | 21 class SingleThreadIdleTaskRunner |
21 : public base::RefCountedThreadSafe<SingleThreadIdleTaskRunner> { | 22 : public base::RefCountedThreadSafe<SingleThreadIdleTaskRunner> { |
22 public: | 23 public: |
23 typedef base::Callback<void(base::TimeTicks)> IdleTask; | 24 typedef base::Callback<void(base::TimeTicks)> IdleTask; |
24 | 25 |
| 26 // Used to request idle task deadlines and signal posting of idle tasks. |
| 27 class SCHEDULER_EXPORT Delegate { |
| 28 public: |
| 29 Delegate(); |
| 30 virtual ~Delegate(); |
| 31 |
| 32 // Signals that an idle task has been posted. This will be called on the |
| 33 // posting thread, which may not be the same thread as the |
| 34 // SingleThreadIdleTaskRunner runs on. |
| 35 virtual void OnIdleTaskPosted() = 0; |
| 36 |
| 37 // Signals that a new idle task is about to be run and returns the deadline |
| 38 // for this idle task. |
| 39 virtual base::TimeTicks WillProcessIdleTask() = 0; |
| 40 |
| 41 // Signals that an idle task has finished being run. |
| 42 virtual void DidProcessIdleTask() = 0; |
| 43 |
| 44 private: |
| 45 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 46 }; |
| 47 |
25 // NOTE Category strings must have application lifetime (statics or | 48 // NOTE Category strings must have application lifetime (statics or |
26 // literals). They may not include " chars. | 49 // literals). They may not include " chars. |
27 SingleThreadIdleTaskRunner( | 50 SingleThreadIdleTaskRunner( |
28 scoped_refptr<base::SingleThreadTaskRunner> idle_priority_task_runner, | 51 scoped_refptr<base::SingleThreadTaskRunner> idle_priority_task_runner, |
29 scoped_refptr<base::SingleThreadTaskRunner> after_wakeup_task_runner, | 52 scoped_refptr<base::SingleThreadTaskRunner> after_wakeup_task_runner, |
30 base::Callback<void(base::TimeTicks*)> deadline_supplier, | 53 Delegate* Delegate, |
31 const char* tracing_category); | 54 const char* tracing_category); |
32 | 55 |
33 virtual void PostIdleTask(const tracked_objects::Location& from_here, | 56 virtual void PostIdleTask(const tracked_objects::Location& from_here, |
34 const IdleTask& idle_task); | 57 const IdleTask& idle_task); |
35 | 58 |
36 virtual void PostNonNestableIdleTask( | 59 virtual void PostNonNestableIdleTask( |
37 const tracked_objects::Location& from_here, | 60 const tracked_objects::Location& from_here, |
38 const IdleTask& idle_task); | 61 const IdleTask& idle_task); |
39 | 62 |
40 virtual void PostIdleTaskAfterWakeup( | 63 virtual void PostIdleTaskAfterWakeup( |
41 const tracked_objects::Location& from_here, | 64 const tracked_objects::Location& from_here, |
42 const IdleTask& idle_task); | 65 const IdleTask& idle_task); |
43 | 66 |
44 bool RunsTasksOnCurrentThread() const; | 67 bool RunsTasksOnCurrentThread() const; |
45 | 68 |
46 protected: | 69 protected: |
47 virtual ~SingleThreadIdleTaskRunner(); | 70 virtual ~SingleThreadIdleTaskRunner(); |
48 | 71 |
49 private: | 72 private: |
50 friend class base::RefCountedThreadSafe<SingleThreadIdleTaskRunner>; | 73 friend class base::RefCountedThreadSafe<SingleThreadIdleTaskRunner>; |
51 | 74 |
52 void RunTask(IdleTask idle_task); | 75 void RunTask(IdleTask idle_task); |
53 | 76 |
54 scoped_refptr<base::SingleThreadTaskRunner> idle_priority_task_runner_; | 77 scoped_refptr<base::SingleThreadTaskRunner> idle_priority_task_runner_; |
55 scoped_refptr<base::SingleThreadTaskRunner> after_wakeup_task_runner_; | 78 scoped_refptr<base::SingleThreadTaskRunner> after_wakeup_task_runner_; |
56 base::Callback<void(base::TimeTicks*)> deadline_supplier_; | 79 Delegate* delegate_; // NOT OWNED |
57 const char* tracing_category_; | 80 const char* tracing_category_; |
58 base::WeakPtr<SingleThreadIdleTaskRunner> weak_scheduler_ptr_; | 81 base::WeakPtr<SingleThreadIdleTaskRunner> weak_scheduler_ptr_; |
59 base::WeakPtrFactory<SingleThreadIdleTaskRunner> weak_factory_; | 82 base::WeakPtrFactory<SingleThreadIdleTaskRunner> weak_factory_; |
60 DISALLOW_COPY_AND_ASSIGN(SingleThreadIdleTaskRunner); | 83 DISALLOW_COPY_AND_ASSIGN(SingleThreadIdleTaskRunner); |
61 }; | 84 }; |
62 | 85 |
63 } // namespace scheduler | 86 } // namespace scheduler |
64 | 87 |
65 #endif // COMPONENTS_SCHEDULER_CHILD_SINGLE_THREAD_IDLE_TASK_RUNNER_H_ | 88 #endif // COMPONENTS_SCHEDULER_CHILD_SINGLE_THREAD_IDLE_TASK_RUNNER_H_ |
OLD | NEW |