Chromium Code Reviews| 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_.CreateConditionVariable()), | |
| 14 num_tasks_blocking_shutdown_(0), | |
| 15 is_shutting_down_(false), | |
| 16 shutdown_completed_(false) {} | |
| 17 | |
| 18 ShutdownManager::~ShutdownManager() = default; | |
| 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."; | |
|
gab
2016/02/23 22:28:22
How about (as mentioned in header):
DCHECK(!shutd
fdoray
2016/02/26 15:53:28
Done.
| |
| 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_); | |
|
gab
2016/02/23 22:28:23
Please highlight in CL description that you have a
fdoray
2016/02/26 15:53:28
Done.
| |
| 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: { | |
|
robliao
2016/02/24 01:22:44
Nit: We generally don't use braces for switch stat
fdoray
2016/02/26 15:53:28
Done.
| |
| 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 } | |
| 73 | |
| 74 void ShutdownManager::DidExecuteTask(TaskShutdownBehavior shutdown_behavior) { | |
| 75 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN || | |
| 76 shutdown_behavior == TaskShutdownBehavior::SKIP_ON_SHUTDOWN) { | |
| 77 AutoSchedulerLock auto_lock(lock_); | |
| 78 --num_tasks_blocking_shutdown_; | |
| 79 if (num_tasks_blocking_shutdown_ == 0) | |
| 80 cv_->Signal(); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 void ShutdownManager::set_is_shutting_down_for_testing(bool is_shutting_down) { | |
| 85 is_shutting_down_ = is_shutting_down; | |
| 86 subtle::MemoryBarrier(); | |
| 87 } | |
| 88 | |
| 89 bool ShutdownManager::is_shutting_down_for_testing() const { | |
| 90 subtle::MemoryBarrier(); | |
|
gab
2016/02/23 22:28:23
Locking seems fine in test code (at least for now)
fdoray
2016/02/26 15:53:28
Done.
| |
| 91 return is_shutting_down_; | |
| 92 } | |
| 93 | |
| 94 bool ShutdownManager::shutdown_completed() const { | |
| 95 subtle::MemoryBarrier(); | |
|
gab
2016/02/23 22:28:22
I'd say use a lock here for now (until you come up
fdoray
2016/02/26 15:53:28
Done.
| |
| 96 return shutdown_completed_; | |
| 97 } | |
| 98 | |
| 99 } // namespace internal | |
| 100 } // namespace base | |
| OLD | NEW |