Index: chrome/browser/browser_process_impl.cc |
diff --git a/chrome/browser/browser_process_impl.cc b/chrome/browser/browser_process_impl.cc |
index 7f492c575c22e6ae13b52c0ff07ba29c145f9eb6..39038dce93043e01cfa53c5545ae4fdb8d6a68b6 100644 |
--- a/chrome/browser/browser_process_impl.cc |
+++ b/chrome/browser/browser_process_impl.cc |
@@ -379,12 +379,83 @@ unsigned int BrowserProcessImpl::ReleaseModule() { |
return module_ref_count_; |
} |
+namespace { |
+ |
+class RundownTaskCounter : |
+ public base::RefCountedThreadSafe<RundownTaskCounter> { |
+ public: |
+ // Creates a rundown task counter with |count|. |
+ explicit RundownTaskCounter(size_t count); |
+ |
+ // Posts a rundown task to |task_runner|. |
+ void Post(base::SequencedTaskRunner* task_runner); |
+ |
+ // Decrements the counter and releases the wait on zero. |
+ void Decrement(); |
erikwright (departed)
2014/06/24 16:10:53
Make this private.
Sigurður Ásgeirsson
2014/06/24 17:17:26
Done.
|
+ |
+ // Waits until the count is zero or |max_time| has passed. |
+ bool TimedWait(const base::TimeDelta& max_time); |
+ |
+ private: |
+ friend class base::RefCountedThreadSafe<RundownTaskCounter>; |
+ ~RundownTaskCounter() {} |
+ |
+ // Keeps track of how many posts are remaining. |
+ size_t posts_remaining_; |
+ |
+ base::AtomicRefCount count_; |
+ base::WaitableEvent waitable_event_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(RundownTaskCounter); |
+}; |
+ |
+RundownTaskCounter::RundownTaskCounter(size_t count) |
+ : posts_remaining_(count), count_(count), waitable_event_(true, false) { |
+ DCHECK_GE(count, 1U); |
+} |
+ |
+void RundownTaskCounter::Post(base::SequencedTaskRunner* task_runner) { |
+ DCHECK_GT(posts_remaining_, 0U); |
+ |
+ --posts_remaining_; |
+ task_runner->PostTask(FROM_HERE, |
+ base::Bind(&RundownTaskCounter::Decrement, this)); |
+} |
+ |
+void RundownTaskCounter::Decrement() { |
+ if (!base::AtomicRefCountDec(&count_)) { |
+ waitable_event_.Signal(); |
+ } |
+} |
+ |
+bool RundownTaskCounter::TimedWait(const base::TimeDelta& max_time) { |
+ // If this check fires, the counter was initialized with the wrong count. |
+ DCHECK_EQ(posts_remaining_, 0U); |
+ return waitable_event_.TimedWait(max_time); |
+} |
+ |
+} // namespace |
+ |
void BrowserProcessImpl::EndSession() { |
// Mark all the profiles as clean. |
ProfileManager* pm = profile_manager(); |
std::vector<Profile*> profiles(pm->GetLoadedProfiles()); |
- for (size_t i = 0; i < profiles.size(); ++i) |
- profiles[i]->SetExitType(Profile::EXIT_SESSION_ENDED); |
+ |
+ // We create the rundown task counter with a count for each profile. |
+ size_t rundown_count = profiles.size(); |
+#if !defined(OS_CHROMEOS) |
+ // Add one for local state on Windows. |
erikwright (departed)
2014/06/24 16:10:53
This comment is inaccurate - it's on non-ChromeOS
Sigurður Ásgeirsson
2014/06/24 17:17:26
Removed.
|
+ rundown_count += 1; |
+#endif |
+ scoped_refptr<RundownTaskCounter> rundown_counter( |
+ new RundownTaskCounter(rundown_count)); |
+ |
+ for (size_t i = 0; i < profiles.size(); ++i) { |
+ Profile* profile = profiles[i]; |
+ profile->SetExitType(Profile::EXIT_SESSION_ENDED); |
+ |
+ rundown_counter->Post(profile->GetIOTaskRunner()); |
+ } |
// Tell the metrics service it was cleanly shutdown. |
MetricsService* metrics = g_browser_process->metrics_service(); |
@@ -395,6 +466,8 @@ void BrowserProcessImpl::EndSession() { |
// On ChromeOS, chrome gets killed when hangs, so no need to |
// commit metrics::prefs::kStabilitySessionEndCompleted change immediately. |
local_state()->CommitPendingWrite(); |
+ |
+ rundown_counter->Post(local_state_task_runner_); |
#endif |
} |
@@ -405,7 +478,9 @@ void BrowserProcessImpl::EndSession() { |
// otherwise on startup we'll think we crashed. So we block until done and |
// then proceed with normal shutdown. |
#if defined(USE_X11) || defined(OS_WIN) |
- // Create a waitable event to block on file writing being complete. |
+ // Do a best-effort wait on the successful countdown of rundown tasks. Note |
+ // that if we don't complete "quickly enough", Windows will terminate our |
+ // process. |
// |
// On Windows, we previously posted a message to FILE and then ran a nested |
// message loop, waiting for that message to be processed until quitting. |
@@ -416,16 +491,8 @@ void BrowserProcessImpl::EndSession() { |
// GPU process synchronously. Because the system may not be allowing |
// processes to launch, this can result in a hang. See |
// http://crbug.com/318527. |
- scoped_ptr<base::WaitableEvent> done_writing( |
- new base::WaitableEvent(false, false)); |
- BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
- base::Bind(Signal, done_writing.get())); |
- // If all file writes haven't cleared in the timeout, leak the WaitableEvent |
- // so that there's no race to reference it in Signal(). |
- if (!done_writing->TimedWait( |
- base::TimeDelta::FromSeconds(kEndSessionTimeoutSeconds))) { |
- ignore_result(done_writing.release()); |
- } |
+ rundown_counter->TimedWait( |
+ base::TimeDelta::FromSeconds(kEndSessionTimeoutSeconds)); |
#else |
NOTIMPLEMENTED(); |
#endif |