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 #include "base/task_scheduler/shutdown_manager.h" |
| 6 |
| 7 #include "base/atomicops.h" |
| 8 |
| 9 namespace base { |
| 10 namespace internal { |
| 11 |
| 12 ShutdownManager::ShutdownManager() |
| 13 : cv_(lock_.RawLockForConditionVariable()), |
| 14 num_tasks_blocking_shutdown_(0), |
| 15 is_shutting_down_(false), |
| 16 shutdown_completed_(false) { |
| 17 subtle::MemoryBarrier(); |
| 18 } |
| 19 |
| 20 void ShutdownManager::Shutdown() { |
| 21 AutoSchedulerLock auto_lock(lock_); |
| 22 |
| 23 DCHECK(!is_shutting_down_) << "ShutdownManager::Shutdown can't be called " |
| 24 "simultaneously on multiple threads."; |
| 25 is_shutting_down_ = true; |
| 26 |
| 27 // Wait until the number of tasks blocking shutdown is zero. |
| 28 while (num_tasks_blocking_shutdown_ != 0) |
| 29 cv_.Wait(); |
| 30 |
| 31 is_shutting_down_ = false; |
| 32 shutdown_completed_ = true; |
| 33 } |
| 34 |
| 35 bool ShutdownManager::ShouldPostTask(TaskShutdownBehavior shutdown_behavior) { |
| 36 AutoSchedulerLock auto_lock(lock_); |
| 37 |
| 38 if (shutdown_completed_) |
| 39 return false; |
| 40 |
| 41 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN) { |
| 42 ++num_tasks_blocking_shutdown_; |
| 43 return true; |
| 44 } |
| 45 |
| 46 return !is_shutting_down_; |
| 47 } |
| 48 |
| 49 bool ShutdownManager::ShouldScheduleTask( |
| 50 TaskShutdownBehavior shutdown_behavior) { |
| 51 AutoSchedulerLock auto_lock(lock_); |
| 52 |
| 53 if (shutdown_completed_) |
| 54 return false; |
| 55 |
| 56 switch (shutdown_behavior) { |
| 57 case TaskShutdownBehavior::BLOCK_SHUTDOWN: { |
| 58 return true; |
| 59 } |
| 60 |
| 61 case TaskShutdownBehavior::SKIP_ON_SHUTDOWN: { |
| 62 if (is_shutting_down_) |
| 63 return false; |
| 64 ++num_tasks_blocking_shutdown_; |
| 65 return true; |
| 66 } |
| 67 |
| 68 case TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN: { |
| 69 return !is_shutting_down_; |
| 70 } |
| 71 |
| 72 default: { |
| 73 NOTREACHED(); |
| 74 return false; |
| 75 } |
| 76 } |
| 77 } |
| 78 |
| 79 void ShutdownManager::DidExecuteTask(TaskShutdownBehavior shutdown_behavior) { |
| 80 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN || |
| 81 shutdown_behavior == TaskShutdownBehavior::SKIP_ON_SHUTDOWN) { |
| 82 AutoSchedulerLock auto_lock(lock_); |
| 83 --num_tasks_blocking_shutdown_; |
| 84 if (num_tasks_blocking_shutdown_ == 0) |
| 85 cv_.Signal(); |
| 86 } |
| 87 } |
| 88 |
| 89 void ShutdownManager::set_is_shutting_down_for_testing(bool is_shutting_down) { |
| 90 is_shutting_down_ = is_shutting_down; |
| 91 } |
| 92 |
| 93 bool ShutdownManager::is_shutting_down_for_testing() const { |
| 94 subtle::MemoryBarrier(); |
| 95 return is_shutting_down_; |
| 96 } |
| 97 |
| 98 bool ShutdownManager::shutdown_completed() const { |
| 99 subtle::MemoryBarrier(); |
| 100 return shutdown_completed_; |
| 101 } |
| 102 |
| 103 } // namespace internal |
| 104 } // namespace base |
OLD | NEW |