Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1372)

Side by Side Diff: base/at_exit.cc

Issue 1661673003: Fix AtExitManager recursive lock acquisition. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix ShadowingAtExitManager. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/at_exit.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/at_exit.h" 5 #include "base/at_exit.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <ostream> 8 #include <ostream>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 13
14 namespace base { 14 namespace base {
15 15
16 // Keep a stack of registered AtExitManagers. We always operate on the most 16 // Keep a stack of registered AtExitManagers. We always operate on the most
17 // recent, and we should never have more than one outside of testing (for a 17 // recent, and we should never have more than one outside of testing (for a
18 // statically linked version of this library). Testing may use the shadow 18 // statically linked version of this library). Testing may use the shadow
19 // version of the constructor, and if we are building a dynamic library we may 19 // version of the constructor, and if we are building a dynamic library we may
20 // end up with multiple AtExitManagers on the same process. We don't protect 20 // end up with multiple AtExitManagers on the same process. We don't protect
21 // this for thread-safe access, since it will only be modified in testing. 21 // this for thread-safe access, since it will only be modified in testing.
22 static AtExitManager* g_top_manager = NULL; 22 static AtExitManager* g_top_manager = NULL;
23 23
24 AtExitManager::AtExitManager() : next_manager_(g_top_manager) { 24 AtExitManager::AtExitManager()
25 : processing_callbacks_(false), next_manager_(g_top_manager) {
25 // If multiple modules instantiate AtExitManagers they'll end up living in this 26 // If multiple modules instantiate AtExitManagers they'll end up living in this
26 // module... they have to coexist. 27 // module... they have to coexist.
27 #if !defined(COMPONENT_BUILD) 28 #if !defined(COMPONENT_BUILD)
28 DCHECK(!g_top_manager); 29 DCHECK(!g_top_manager);
29 #endif 30 #endif
30 g_top_manager = this; 31 g_top_manager = this;
31 } 32 }
32 33
33 AtExitManager::~AtExitManager() { 34 AtExitManager::~AtExitManager() {
34 if (!g_top_manager) { 35 if (!g_top_manager) {
(...skipping 13 matching lines...) Expand all
48 } 49 }
49 50
50 // static 51 // static
51 void AtExitManager::RegisterTask(base::Closure task) { 52 void AtExitManager::RegisterTask(base::Closure task) {
52 if (!g_top_manager) { 53 if (!g_top_manager) {
53 NOTREACHED() << "Tried to RegisterCallback without an AtExitManager"; 54 NOTREACHED() << "Tried to RegisterCallback without an AtExitManager";
54 return; 55 return;
55 } 56 }
56 57
57 AutoLock lock(g_top_manager->lock_); 58 AutoLock lock(g_top_manager->lock_);
59 DCHECK(!g_top_manager->processing_callbacks_);
Mark Mentovai 2016/02/08 15:15:45 Maybe this should be a CHECK.
Anand Mistry (off Chromium) 2016/02/08 23:12:25 I didn't want to break release builds, only catch
Mark Mentovai 2016/02/09 15:37:46 Anand Mistry wrote:
58 g_top_manager->stack_.push(task); 60 g_top_manager->stack_.push(task);
59 } 61 }
60 62
61 // static 63 // static
62 void AtExitManager::ProcessCallbacksNow() { 64 void AtExitManager::ProcessCallbacksNow() {
63 if (!g_top_manager) { 65 if (!g_top_manager) {
64 NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager"; 66 NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager";
65 return; 67 return;
66 } 68 }
67 69
68 AutoLock lock(g_top_manager->lock_); 70 // Callbacks may add new callbacks, so run them without holding |lock_|. This
Mark Mentovai 2016/02/08 15:15:45 may add→may try to add
Anand Mistry (off Chromium) 2016/02/08 23:12:25 Done.
71 // is an error and caught by the DCHECK in RegisterTask(), but handle it
72 // gracefully in release builds so we don't deadlock.
73 std::stack<base::Closure> tasks;
74 {
75 AutoLock lock(g_top_manager->lock_);
76 tasks.swap(g_top_manager->stack_);
77 g_top_manager->processing_callbacks_ = true;
78 }
69 79
70 while (!g_top_manager->stack_.empty()) { 80 while (!tasks.empty()) {
71 base::Closure task = g_top_manager->stack_.top(); 81 base::Closure task = tasks.top();
72 task.Run(); 82 task.Run();
73 g_top_manager->stack_.pop(); 83 tasks.pop();
74 } 84 }
85
86 // Expect that all callbacks have been run.
87 DCHECK(g_top_manager->stack_.empty());
75 } 88 }
76 89
77 AtExitManager::AtExitManager(bool shadow) : next_manager_(g_top_manager) { 90 AtExitManager::AtExitManager(bool shadow)
91 : processing_callbacks_(false), next_manager_(g_top_manager) {
78 DCHECK(shadow || !g_top_manager); 92 DCHECK(shadow || !g_top_manager);
79 g_top_manager = this; 93 g_top_manager = this;
80 } 94 }
81 95
82 } // namespace base 96 } // namespace base
OLDNEW
« no previous file with comments | « base/at_exit.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698