Index: base/task_scheduler/shutdown_manager.h |
diff --git a/base/task_scheduler/shutdown_manager.h b/base/task_scheduler/shutdown_manager.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6bf0640aabfe6cdfa5899a9a91152d3cef796785 |
--- /dev/null |
+++ b/base/task_scheduler/shutdown_manager.h |
@@ -0,0 +1,74 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef BASE_TASK_SCHEDULER_SHUTDOWN_MANAGER_H_ |
+#define BASE_TASK_SCHEDULER_SHUTDOWN_MANAGER_H_ |
+ |
+#include "base/base_export.h" |
+#include "base/macros.h" |
+#include "base/synchronization/condition_variable.h" |
+#include "base/task_scheduler/scheduler_lock.h" |
+#include "base/task_scheduler/task_traits.h" |
+ |
+namespace base { |
+namespace task_scheduler { |
+ |
+// Ensures proper shutdown behavior of the task scheduler. This class is thread- |
+// safe. |
+class BASE_EXPORT ShutdownManager { |
+ public: |
+ ShutdownManager(); |
+ |
+ // Shuts down the task scheduler. After this is called, only tasks posted with |
+ // the BLOCK_SHUTDOWN behavior are scheduled. Returns when: |
+ // - All SKIP_ON_SHUTDOWN tasks that were already running have completed their |
+ // execution. |
+ // - There is no pending BLOCK_SHUTDOWN tasks. |
+ void Shutdown(); |
+ |
+ // Indicates whether a task with this |shutdown_behavior| should be posted. |
+ bool ShouldPostTask(TaskShutdownBehavior shutdown_behavior); |
+ |
+ // Indicates whether a task with this |shutdown_behavior| should be scheduled. |
+ bool ShouldScheduleTask(TaskShutdownBehavior shutdown_behavior); |
+ |
+ // Notifies the shutdown manager that a task with this |shutdown_behavior| |
+ // completed its execution. |
+ void DidExecuteTask(TaskShutdownBehavior shutdown_behavior); |
+ |
+ // Returns true once Shutdown() has returned. No new task can be scheduled |
+ // after this point. |
+ bool shutdown_completed() const; |
+ |
+ // Sets |is_shutting_down_| to true. This has the same effect as calling |
+ // Shutdown(), but doesn't block. |
+ void SetIsShuttingDownForTesting(); |
+ |
+ // Returns true once Shutdown() or SetIsShuttingDownForTesting() has been |
+ // called. |
+ bool is_shutting_down_for_testing() const { return is_shutting_down_; } |
+ |
+ private: |
+ // Lock protecting all members. |
+ SchedulerLock lock_; |
+ |
+ // Condition variable signaled when |num_tasks_blocking_shutdown_| reaches |
+ // zero. |
+ ConditionVariable cv_; |
+ |
+ // Number of tasks blocking shutdown. |
+ size_t num_tasks_blocking_shutdown_; |
+ |
+ // True once Shutdown() has been called. |
+ bool is_shutting_down_; |
+ |
+ bool shutdown_completed_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ShutdownManager); |
+}; |
+ |
+} // namespace task_scheduler |
+} // namespace base |
+ |
+#endif // BASE_TASK_SCHEDULER_SHUTDOWN_MANAGER_H_ |