OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef BASE_TASK_SCHEDULER_SHUTDOWN_MANAGER_H_ |
| 6 #define BASE_TASK_SCHEDULER_SHUTDOWN_MANAGER_H_ |
| 7 |
| 8 #include "base/base_export.h" |
| 9 #include "base/macros.h" |
| 10 #include "base/synchronization/condition_variable.h" |
| 11 #include "base/task_scheduler/scheduler_lock.h" |
| 12 #include "base/task_scheduler/task_traits.h" |
| 13 |
| 14 namespace base { |
| 15 namespace task_scheduler { |
| 16 |
| 17 // Ensures proper shutdown behavior of the task scheduler. This class is thread- |
| 18 // safe. |
| 19 class BASE_EXPORT ShutdownManager { |
| 20 public: |
| 21 ShutdownManager(); |
| 22 |
| 23 // Shuts down the task scheduler. After this is called, only tasks posted with |
| 24 // the BLOCK_SHUTDOWN behavior are scheduled. Returns when: |
| 25 // - All SKIP_ON_SHUTDOWN tasks that were already running have completed their |
| 26 // execution. |
| 27 // - There is no pending BLOCK_SHUTDOWN tasks. |
| 28 void Shutdown(); |
| 29 |
| 30 // Indicates whether a task with this |shutdown_behavior| should be posted. |
| 31 bool ShouldPostTask(TaskShutdownBehavior shutdown_behavior); |
| 32 |
| 33 // Indicates whether a task with this |shutdown_behavior| should be scheduled. |
| 34 bool ShouldScheduleTask(TaskShutdownBehavior shutdown_behavior); |
| 35 |
| 36 // Notifies the shutdown manager that a task with this |shutdown_behavior| |
| 37 // completed its execution. |
| 38 void DidExecuteTask(TaskShutdownBehavior shutdown_behavior); |
| 39 |
| 40 // Returns true once Shutdown() has returned. No new task can be scheduled |
| 41 // after this point. |
| 42 bool shutdown_completed() const; |
| 43 |
| 44 // Sets |is_shutting_down_| to true. This has the same effect as calling |
| 45 // Shutdown(), but doesn't block. |
| 46 void SetIsShuttingDownForTesting(); |
| 47 |
| 48 // Returns true once Shutdown() or SetIsShuttingDownForTesting() has been |
| 49 // called. |
| 50 bool is_shutting_down_for_testing() const { return is_shutting_down_; } |
| 51 |
| 52 private: |
| 53 // Lock protecting all members. |
| 54 SchedulerLock lock_; |
| 55 |
| 56 // Condition variable signaled when |num_tasks_blocking_shutdown_| reaches |
| 57 // zero. |
| 58 ConditionVariable cv_; |
| 59 |
| 60 // Number of tasks blocking shutdown. |
| 61 size_t num_tasks_blocking_shutdown_; |
| 62 |
| 63 // True once Shutdown() has been called. |
| 64 bool is_shutting_down_; |
| 65 |
| 66 bool shutdown_completed_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(ShutdownManager); |
| 69 }; |
| 70 |
| 71 } // namespace task_scheduler |
| 72 } // namespace base |
| 73 |
| 74 #endif // BASE_TASK_SCHEDULER_SHUTDOWN_MANAGER_H_ |
OLD | NEW |