Chromium Code Reviews| 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 DelayedTaskManager( | |
|
gab
2017/04/19 18:59:17
Add a comment that |tick_clock| can be specified f
fdoray
2017/04/20 12:08:52
Done.
| |
| 39 std::unique_ptr<TickClock> tick_clock = MakeUnique<DefaultTickClock>()); | |
| 40 ~DelayedTaskManager(); | |
| 41 | |
| 42 // Starts the delayed task manager, allowing past and future tasks to be | |
| 43 // forwarded to their callbacks as they become ripe for execution. | |
| 30 // |service_thread_task_runner| posts tasks to the TaskScheduler service | 44 // |service_thread_task_runner| posts tasks to the TaskScheduler service |
| 31 // thread. | 45 // thread. |
| 32 explicit DelayedTaskManager( | 46 void Start(scoped_refptr<TaskRunner> service_thread_task_runner); |
| 33 scoped_refptr<TaskRunner> service_thread_task_runner); | |
| 34 ~DelayedTaskManager(); | |
| 35 | 47 |
| 36 // Calls |post_task_now_callback| with |task| when |task| is ripe for | 48 // Schedules a call to |post_task_now_callback| with |task| as argument when |
| 37 // execution. | 49 // |task| is ripe for execution and Start() has been called. |
| 38 void AddDelayedTask(std::unique_ptr<Task> task, | 50 void AddDelayedTask(std::unique_ptr<Task> task, |
| 39 const PostTaskNowCallback& post_task_now_callback); | 51 PostTaskNowCallback post_task_now_callback); |
| 40 | 52 |
| 41 private: | 53 private: |
| 42 const scoped_refptr<TaskRunner> service_thread_task_runner_; | 54 // Schedules a call to |post_task_now_callback| with |task| as argument when |
| 55 // |task| is ripe for execution. Start() must have been called before this. | |
| 56 void AddDelayedTaskNow(std::unique_ptr<Task> task, | |
| 57 PostTaskNowCallback post_task_now_callback); | |
| 58 | |
| 59 const std::unique_ptr<TickClock> tick_clock_; | |
| 60 | |
| 61 AtomicFlag started_; | |
| 62 | |
| 63 // Synchronizes access to all members below before |started_| is set. Once | |
| 64 // |started_| is set: | |
| 65 // - |service_thread_task_runner| stays constant, so it can be read without | |
| 66 // holding the lock. | |
| 67 // - |tasks_added_before_start_| shouldn't be accessed anymore. | |
| 68 SchedulerLock lock_; | |
| 69 | |
| 70 scoped_refptr<TaskRunner> service_thread_task_runner_; | |
| 71 std::vector<std::pair<std::unique_ptr<Task>, PostTaskNowCallback>> | |
| 72 tasks_added_before_start_; | |
| 43 | 73 |
| 44 DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager); | 74 DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager); |
| 45 }; | 75 }; |
| 46 | 76 |
| 47 } // namespace internal | 77 } // namespace internal |
| 48 } // namespace base | 78 } // namespace base |
| 49 | 79 |
| 50 #endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ | 80 #endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
| OLD | NEW |