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