Index: base/task_scheduler/shutdown_manager.cc |
diff --git a/base/task_scheduler/shutdown_manager.cc b/base/task_scheduler/shutdown_manager.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e3854b86bc5e6351b30bdaa090940de872f5fd4c |
--- /dev/null |
+++ b/base/task_scheduler/shutdown_manager.cc |
@@ -0,0 +1,93 @@ |
+// 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. |
+ |
+#include "base/task_scheduler/shutdown_manager.h" |
+ |
+#include "base/atomicops.h" |
+ |
+namespace base { |
+namespace task_scheduler { |
+ |
+ShutdownManager::ShutdownManager() |
+ : cv_(lock_.RawLockForConditionVariable()), |
+ num_tasks_blocking_shutdown_(0), |
+ is_shutting_down_(false), |
+ shutdown_completed_(false) {} |
+ |
+void ShutdownManager::Shutdown() { |
+ is_shutting_down_ = true; |
+ |
+ // Wait until the number of tasks blocking shutdown is zero. |
+ AutoSchedulerLock auto_lock(lock_); |
+ while (num_tasks_blocking_shutdown_ != 0) |
+ cv_.Wait(); |
+ |
+ shutdown_completed_ = true; |
+} |
+ |
+bool ShutdownManager::ShouldPostTask(TaskShutdownBehavior shutdown_behavior) { |
+ AutoSchedulerLock auto_lock(lock_); |
+ |
+ if (shutdown_completed_) |
+ return false; |
+ |
+ if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN) { |
+ ++num_tasks_blocking_shutdown_; |
+ return true; |
+ } |
+ |
+ return !is_shutting_down_; |
+} |
+ |
+bool ShutdownManager::ShouldScheduleTask( |
+ TaskShutdownBehavior shutdown_behavior) { |
+ AutoSchedulerLock auto_lock(lock_); |
+ |
+ if (shutdown_completed_) |
+ return false; |
+ |
+ switch (shutdown_behavior) { |
+ case TaskShutdownBehavior::BLOCK_SHUTDOWN: { |
+ return true; |
+ } |
+ |
+ case TaskShutdownBehavior::SKIP_ON_SHUTDOWN: { |
+ if (is_shutting_down_) |
+ return false; |
+ ++num_tasks_blocking_shutdown_; |
robliao
2016/02/11 22:49:30
Should this be here?
fdoray
2016/02/12 04:16:19
I think the code is correct. A SKIP_ON_SHUTDOWN ta
|
+ return true; |
+ } |
+ |
+ case TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN: { |
+ return !is_shutting_down_; |
+ } |
+ |
+ default: { |
+ NOTREACHED(); |
+ return false; |
+ } |
+ } |
+} |
+ |
+void ShutdownManager::DidExecuteTask(TaskShutdownBehavior shutdown_behavior) { |
+ if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN || |
+ shutdown_behavior == TaskShutdownBehavior::SKIP_ON_SHUTDOWN) { |
+ AutoSchedulerLock auto_lock(lock_); |
+ --num_tasks_blocking_shutdown_; |
+ if (num_tasks_blocking_shutdown_ == 0) |
+ cv_.Signal(); |
+ } |
+} |
+ |
+bool ShutdownManager::shutdown_completed() const { |
+ subtle::MemoryBarrier(); |
+ return shutdown_completed_; |
+} |
+ |
+void ShutdownManager::SetIsShuttingDownForTesting() { |
+ is_shutting_down_ = true; |
+} |
+ |
+} // namespace task_scheduler |
+} // namespace base |