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

Side by Side Diff: base/at_exit.cc

Issue 7831021: Support for registering arbitrary Tasks with AtExitManager. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 3 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 | Annotate | Revision Log
« no previous file with comments | « base/at_exit.h ('k') | base/at_exit_unittest.cc » ('j') | 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/logging.h" 11 #include "base/logging.h"
12 #include "base/task.h"
11 13
12 namespace base { 14 namespace base {
13 15
14 // 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
15 // 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
16 // statically linked version of this library). Testing may use the shadow 18 // statically linked version of this library). Testing may use the shadow
17 // 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
18 // 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
19 // 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.
20 static AtExitManager* g_top_manager = NULL; 22 static AtExitManager* g_top_manager = NULL;
(...skipping 13 matching lines...) Expand all
34 return; 36 return;
35 } 37 }
36 DCHECK_EQ(this, g_top_manager); 38 DCHECK_EQ(this, g_top_manager);
37 39
38 ProcessCallbacksNow(); 40 ProcessCallbacksNow();
39 g_top_manager = next_manager_; 41 g_top_manager = next_manager_;
40 } 42 }
41 43
42 // static 44 // static
43 void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) { 45 void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) {
46 DCHECK(func);
47 RegisterTask(base::Bind(func, param));
48 }
49
50 // static
51 void AtExitManager::RegisterTask(base::Closure task) {
44 if (!g_top_manager) { 52 if (!g_top_manager) {
45 NOTREACHED() << "Tried to RegisterCallback without an AtExitManager"; 53 NOTREACHED() << "Tried to RegisterCallback without an AtExitManager";
46 return; 54 return;
47 } 55 }
48 56
49 DCHECK(func);
50
51 AutoLock lock(g_top_manager->lock_); 57 AutoLock lock(g_top_manager->lock_);
52 g_top_manager->stack_.push(CallbackAndParam(func, param)); 58 g_top_manager->stack_.push(task);
53 } 59 }
54 60
55 // static 61 // static
56 void AtExitManager::ProcessCallbacksNow() { 62 void AtExitManager::ProcessCallbacksNow() {
57 if (!g_top_manager) { 63 if (!g_top_manager) {
58 NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager"; 64 NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager";
59 return; 65 return;
60 } 66 }
61 67
62 AutoLock lock(g_top_manager->lock_); 68 AutoLock lock(g_top_manager->lock_);
63 69
64 while (!g_top_manager->stack_.empty()) { 70 while (!g_top_manager->stack_.empty()) {
65 CallbackAndParam callback_and_param = g_top_manager->stack_.top(); 71 base::Closure task = g_top_manager->stack_.top();
72 task.Run();
66 g_top_manager->stack_.pop(); 73 g_top_manager->stack_.pop();
67
68 callback_and_param.func_(callback_and_param.param_);
69 } 74 }
70 } 75 }
71 76
72 AtExitManager::AtExitManager(bool shadow) : next_manager_(g_top_manager) { 77 AtExitManager::AtExitManager(bool shadow) : next_manager_(g_top_manager) {
73 DCHECK(shadow || !g_top_manager); 78 DCHECK(shadow || !g_top_manager);
74 g_top_manager = this; 79 g_top_manager = this;
75 } 80 }
76 81
77 } // namespace base 82 } // namespace base
OLDNEW
« no previous file with comments | « base/at_exit.h ('k') | base/at_exit_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698