OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ | 5 #ifndef BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
6 #define BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ | 6 #define BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> |
| 10 #include <vector> |
9 | 11 |
10 #include "base/base_export.h" | 12 #include "base/base_export.h" |
11 #include "base/callback_forward.h" | 13 #include "base/callback.h" |
12 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" |
13 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/synchronization/atomic_flag.h" |
| 18 #include "base/task_scheduler/scheduler_lock.h" |
| 19 #include "base/time/default_tick_clock.h" |
| 20 #include "base/time/tick_clock.h" |
14 | 21 |
15 namespace base { | 22 namespace base { |
16 | 23 |
17 class TaskRunner; | 24 class TaskRunner; |
18 | 25 |
19 namespace internal { | 26 namespace internal { |
20 | 27 |
21 struct Task; | 28 struct Task; |
22 | 29 |
23 // The DelayedTaskManager forwards tasks to various scheduler components when | 30 // The DelayedTaskManager forwards tasks to post task callbacks when they become |
24 // they become ripe for execution. This class is thread-safe. | 31 // ripe for execution. Tasks are not forwarded before Start() is called. This |
| 32 // class is thread-safe. |
25 class BASE_EXPORT DelayedTaskManager { | 33 class BASE_EXPORT DelayedTaskManager { |
26 public: | 34 public: |
27 // Posts |task| for execution immediately. | 35 // Posts |task| for execution immediately. |
28 using PostTaskNowCallback = Callback<void(std::unique_ptr<Task> task)>; | 36 using PostTaskNowCallback = OnceCallback<void(std::unique_ptr<Task> task)>; |
29 | 37 |
| 38 // |tick_clock| can be specified for testing. |
| 39 DelayedTaskManager( |
| 40 std::unique_ptr<TickClock> tick_clock = MakeUnique<DefaultTickClock>()); |
| 41 ~DelayedTaskManager(); |
| 42 |
| 43 // Starts the delayed task manager, allowing past and future tasks to be |
| 44 // forwarded to their callbacks as they become ripe for execution. |
30 // |service_thread_task_runner| posts tasks to the TaskScheduler service | 45 // |service_thread_task_runner| posts tasks to the TaskScheduler service |
31 // thread. | 46 // thread. |
32 explicit DelayedTaskManager( | 47 void Start(scoped_refptr<TaskRunner> service_thread_task_runner); |
33 scoped_refptr<TaskRunner> service_thread_task_runner); | |
34 ~DelayedTaskManager(); | |
35 | 48 |
36 // Calls |post_task_now_callback| with |task| when |task| is ripe for | 49 // Schedules a call to |post_task_now_callback| with |task| as argument when |
37 // execution. | 50 // |task| is ripe for execution and Start() has been called. |
38 void AddDelayedTask(std::unique_ptr<Task> task, | 51 void AddDelayedTask(std::unique_ptr<Task> task, |
39 const PostTaskNowCallback& post_task_now_callback); | 52 PostTaskNowCallback post_task_now_callback); |
40 | 53 |
41 private: | 54 private: |
42 const scoped_refptr<TaskRunner> service_thread_task_runner_; | 55 // Schedules a call to |post_task_now_callback| with |task| as argument when |
| 56 // |delay| expires. Start() must have been called before this. |
| 57 void AddDelayedTaskNow(std::unique_ptr<Task> task, |
| 58 TimeDelta delay, |
| 59 PostTaskNowCallback post_task_now_callback); |
| 60 |
| 61 const std::unique_ptr<TickClock> tick_clock_; |
| 62 |
| 63 AtomicFlag started_; |
| 64 |
| 65 // Synchronizes access to all members below before |started_| is set. Once |
| 66 // |started_| is set: |
| 67 // - |service_thread_task_runner| doest not change, so it can be read without |
| 68 // holding the lock. |
| 69 // - |tasks_added_before_start_| isn't accessed anymore. |
| 70 SchedulerLock lock_; |
| 71 |
| 72 scoped_refptr<TaskRunner> service_thread_task_runner_; |
| 73 std::vector<std::pair<std::unique_ptr<Task>, PostTaskNowCallback>> |
| 74 tasks_added_before_start_; |
43 | 75 |
44 DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager); | 76 DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager); |
45 }; | 77 }; |
46 | 78 |
47 } // namespace internal | 79 } // namespace internal |
48 } // namespace base | 80 } // namespace base |
49 | 81 |
50 #endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ | 82 #endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
OLD | NEW |