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..9ca42c339f2ee51a7929693e0dd2122e45ef9ee7 |
--- /dev/null |
+++ b/base/task_scheduler/shutdown_manager.h |
@@ -0,0 +1,71 @@ |
+// 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 internal { |
+ |
+// Ensures proper shutdown behavior of the task scheduler. Unless otherwise |
+// noted, the methods of this class are 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. |
+ // This cannot be called simultaneously on multiple threads. |
+ 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); |
+ |
+ void set_is_shutting_down_for_testing(bool is_shutting_down_for_testing); |
+ bool is_shutting_down_for_testing() const; |
+ |
+ bool shutdown_completed() const; |
+ |
+ 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 when Shutdown() is waiting for tasks blocking shutdown to complete |
+ // their execution. |
+ bool is_shutting_down_; |
+ |
+ // Returns true once Shutdown() has returned. No new task can be scheduled |
+ // after this point. |
+ bool shutdown_completed_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ShutdownManager); |
+}; |
+ |
+} // namespace internal |
+} // namespace base |
+ |
+#endif // BASE_TASK_SCHEDULER_SHUTDOWN_MANAGER_H_ |