Chromium Code Reviews| 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..a3ec7976a07f1fcd5183c810e0e725439628eeef |
| --- /dev/null |
| +++ b/base/task_scheduler/shutdown_manager.h |
| @@ -0,0 +1,72 @@ |
| +// 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/memory/scoped_ptr.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(); |
| + ~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. |
|
gab
2016/02/23 22:28:23
s/is/are/
fdoray
2016/02/26 15:53:28
Done.
|
| + // This cannot be called simultaneously on multiple threads. |
|
gab
2016/02/23 22:28:23
// This should only be called on shutdown from one
fdoray
2016/02/26 15:53:28
Done. "Must only be called once" implies "from one
|
| + void Shutdown(); |
| + |
| + // Indicates whether a task with this |shutdown_behavior| should be posted. |
| + bool ShouldPostTask(TaskShutdownBehavior shutdown_behavior); |
|
gab
2016/02/23 22:28:23
This actually does more than return its opinion, i
robliao
2016/02/23 22:47:02
What I like about the ShutdownManager right now is
fdoray
2016/02/24 17:01:06
OPTION 1:
Pro: ShutdownManager doesn't have to kn
|
| + |
| + // 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; |
|
gab
2016/02/23 22:28:23
Inline impl of lower_case_getters_and_setters() ab
fdoray
2016/02/26 15:53:28
Done.
|
| + |
| + private: |
| + // Lock protecting all members. |
| + SchedulerLock lock_; |
| + |
| + // Condition variable signaled when |num_tasks_blocking_shutdown_| reaches |
| + // zero. |
|
gab
2016/02/23 22:28:23
[gab TODO]
Append "while |is_shutting_down_|"?
fdoray
2016/02/26 15:53:28
Done.
|
| + scoped_ptr<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_; |
| + |
| + // True once Shutdown() has returned. No new task can be scheduled after this. |
| + bool shutdown_completed_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ShutdownManager); |
| +}; |
| + |
| +} // namespace internal |
| +} // namespace base |
| + |
| +#endif // BASE_TASK_SCHEDULER_SHUTDOWN_MANAGER_H_ |