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" |
13 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/task_scheduler/scheduler_lock.h" | |
14 | 17 |
15 namespace base { | 18 namespace base { |
16 | 19 |
17 class TaskRunner; | 20 class TaskRunner; |
18 | 21 |
19 namespace internal { | 22 namespace internal { |
20 | 23 |
21 struct Task; | 24 struct Task; |
22 | 25 |
23 // The DelayedTaskManager forwards tasks to various scheduler components when | 26 // The DelayedTaskManager forwards tasks to post task callbacks when they become |
24 // they become ripe for execution. This class is thread-safe. | 27 // ripe for execution. Tasks are not forwarded before Start() is called. This |
28 // class is thread-safe. | |
25 class BASE_EXPORT DelayedTaskManager { | 29 class BASE_EXPORT DelayedTaskManager { |
26 public: | 30 public: |
27 // Posts |task| for execution immediately. | 31 // Posts |task| for execution immediately. |
28 using PostTaskNowCallback = Callback<void(std::unique_ptr<Task> task)>; | 32 using PostTaskNowCallback = OnceCallback<void(std::unique_ptr<Task> task)>; |
29 | 33 |
34 DelayedTaskManager(); | |
35 ~DelayedTaskManager(); | |
36 | |
37 // Starts the delayed task manager, allowing past and future tasks to be | |
38 // forwarded to their callbacks as they become ripe for execution. | |
30 // |service_thread_task_runner| posts tasks to the TaskScheduler service | 39 // |service_thread_task_runner| posts tasks to the TaskScheduler service |
31 // thread. | 40 // thread. |
32 explicit DelayedTaskManager( | 41 void Start(scoped_refptr<TaskRunner> service_thread_task_runner); |
33 scoped_refptr<TaskRunner> service_thread_task_runner); | |
34 ~DelayedTaskManager(); | |
35 | 42 |
36 // Calls |post_task_now_callback| with |task| when |task| is ripe for | 43 // Calls |post_task_now_callback| with |task| when |task| is ripe for |
37 // execution. | 44 // execution. |
38 void AddDelayedTask(std::unique_ptr<Task> task, | 45 void AddDelayedTask(std::unique_ptr<Task> task, |
39 const PostTaskNowCallback& post_task_now_callback); | 46 PostTaskNowCallback post_task_now_callback); |
40 | 47 |
41 private: | 48 private: |
42 const scoped_refptr<TaskRunner> service_thread_task_runner_; | 49 // Synchronizes access to all members. |
50 SchedulerLock lock_; | |
gab
2017/04/12 19:15:51
The lock is irrelevant after threads have all "see
fdoray
2017/04/19 15:37:55
Done.
| |
51 | |
52 scoped_refptr<TaskRunner> service_thread_task_runner_; | |
53 std::vector<std::pair<std::unique_ptr<Task>, PostTaskNowCallback>> | |
54 tasks_added_before_start_; | |
43 | 55 |
44 DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager); | 56 DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager); |
45 }; | 57 }; |
46 | 58 |
47 } // namespace internal | 59 } // namespace internal |
48 } // namespace base | 60 } // namespace base |
49 | 61 |
50 #endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ | 62 #endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
OLD | NEW |