OLD | NEW |
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/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/task.h" |
11 | 12 |
12 namespace base { | 13 namespace base { |
13 | 14 |
14 // Keep a stack of registered AtExitManagers. We always operate on the most | 15 // 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 | 16 // 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 | 17 // 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 | 18 // 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 | 19 // 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. | 20 // this for thread-safe access, since it will only be modified in testing. |
20 static AtExitManager* g_top_manager = NULL; | 21 static AtExitManager* g_top_manager = NULL; |
(...skipping 13 matching lines...) Expand all Loading... |
34 return; | 35 return; |
35 } | 36 } |
36 DCHECK_EQ(this, g_top_manager); | 37 DCHECK_EQ(this, g_top_manager); |
37 | 38 |
38 ProcessCallbacksNow(); | 39 ProcessCallbacksNow(); |
39 g_top_manager = next_manager_; | 40 g_top_manager = next_manager_; |
40 } | 41 } |
41 | 42 |
42 // static | 43 // static |
43 void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) { | 44 void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) { |
| 45 DCHECK(func); |
| 46 RegisterTask(NewRunnableFunction(func, param)); |
| 47 } |
| 48 |
| 49 // static |
| 50 void AtExitManager::RegisterTask(Task* task) { |
44 if (!g_top_manager) { | 51 if (!g_top_manager) { |
45 NOTREACHED() << "Tried to RegisterCallback without an AtExitManager"; | 52 NOTREACHED() << "Tried to RegisterCallback without an AtExitManager"; |
46 return; | 53 return; |
47 } | 54 } |
48 | 55 |
49 DCHECK(func); | 56 DCHECK(task); |
50 | 57 |
51 AutoLock lock(g_top_manager->lock_); | 58 AutoLock lock(g_top_manager->lock_); |
52 g_top_manager->stack_.push(CallbackAndParam(func, param)); | 59 g_top_manager->stack_.push(linked_ptr<Task>(task)); |
53 } | 60 } |
54 | 61 |
55 // static | 62 // static |
56 void AtExitManager::ProcessCallbacksNow() { | 63 void AtExitManager::ProcessCallbacksNow() { |
57 if (!g_top_manager) { | 64 if (!g_top_manager) { |
58 NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager"; | 65 NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager"; |
59 return; | 66 return; |
60 } | 67 } |
61 | 68 |
62 AutoLock lock(g_top_manager->lock_); | 69 AutoLock lock(g_top_manager->lock_); |
63 | 70 |
64 while (!g_top_manager->stack_.empty()) { | 71 while (!g_top_manager->stack_.empty()) { |
65 CallbackAndParam callback_and_param = g_top_manager->stack_.top(); | 72 Task* task = g_top_manager->stack_.top().get(); |
| 73 task->Run(); |
66 g_top_manager->stack_.pop(); | 74 g_top_manager->stack_.pop(); |
67 | |
68 callback_and_param.func_(callback_and_param.param_); | |
69 } | 75 } |
70 } | 76 } |
71 | 77 |
72 AtExitManager::AtExitManager(bool shadow) : next_manager_(g_top_manager) { | 78 AtExitManager::AtExitManager(bool shadow) : next_manager_(g_top_manager) { |
73 DCHECK(shadow || !g_top_manager); | 79 DCHECK(shadow || !g_top_manager); |
74 g_top_manager = this; | 80 g_top_manager = this; |
75 } | 81 } |
76 | 82 |
77 } // namespace base | 83 } // namespace base |
OLD | NEW |