OLD | NEW |
1 // Copyright (c) 2006-2008 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 #ifndef BASE_AT_EXIT_H_ | 5 #ifndef BASE_AT_EXIT_H_ |
6 #define BASE_AT_EXIT_H_ | 6 #define BASE_AT_EXIT_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <stack> | 9 #include <stack> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/lock.h" | 12 #include "base/lock.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 | 15 |
16 // This class provides a facility similar to the CRT atexit(), except that | 16 // This class provides a facility similar to the CRT atexit(), except that |
17 // we control when the callbacks are executed. Under Windows for a DLL they | 17 // we control when the callbacks are executed. Under Windows for a DLL they |
18 // happen at a really bad time and under the loader lock. This facility is | 18 // happen at a really bad time and under the loader lock. This facility is |
19 // mostly used by base::Singleton. | 19 // mostly used by base::Singleton. |
20 // | 20 // |
21 // The usage is simple. Early in the main() or WinMain() scope create an | 21 // The usage is simple. Early in the main() or WinMain() scope create an |
22 // AtExitManager object on the stack: | 22 // AtExitManager object on the stack: |
23 // int main(...) { | 23 // int main(...) { |
24 // base::AtExitManager exit_manager; | 24 // base::AtExitManager exit_manager; |
25 // | 25 // |
26 // } | 26 // } |
27 // When the exit_manager object goes out of scope, all the registered | 27 // When the exit_manager object goes out of scope, all the registered |
28 // callbacks and singleton destructors will be called. | 28 // callbacks and singleton destructors will be called. |
29 | 29 |
30 class AtExitManager { | 30 class AtExitManager { |
31 protected: | |
32 // This constructor will allow this instance of AtExitManager to be created | |
33 // even if one already exists. This should only be used for testing! | |
34 // AtExitManagers are kept on a global stack, and it will be removed during | |
35 // destruction. This allows you to shadow another AtExitManager. | |
36 explicit AtExitManager(bool shadow); | |
37 | |
38 public: | 31 public: |
39 typedef void (*AtExitCallbackType)(void*); | 32 typedef void (*AtExitCallbackType)(void*); |
40 | 33 |
41 AtExitManager(); | 34 AtExitManager(); |
42 | 35 |
43 // The dtor calls all the registered callbacks. Do not try to register more | 36 // The dtor calls all the registered callbacks. Do not try to register more |
44 // callbacks after this point. | 37 // callbacks after this point. |
45 ~AtExitManager(); | 38 ~AtExitManager(); |
46 | 39 |
47 // Registers the specified function to be called at exit. The prototype of | 40 // Registers the specified function to be called at exit. The prototype of |
48 // the callback function is void func(). | 41 // the callback function is void func(). |
49 static void RegisterCallback(AtExitCallbackType func, void* param); | 42 static void RegisterCallback(AtExitCallbackType func, void* param); |
50 | 43 |
51 // Calls the functions registered with RegisterCallback in LIFO order. It | 44 // Calls the functions registered with RegisterCallback in LIFO order. It |
52 // is possible to register new callbacks after calling this function. | 45 // is possible to register new callbacks after calling this function. |
53 static void ProcessCallbacksNow(); | 46 static void ProcessCallbacksNow(); |
54 | 47 |
| 48 protected: |
| 49 // This constructor will allow this instance of AtExitManager to be created |
| 50 // even if one already exists. This should only be used for testing! |
| 51 // AtExitManagers are kept on a global stack, and it will be removed during |
| 52 // destruction. This allows you to shadow another AtExitManager. |
| 53 explicit AtExitManager(bool shadow); |
| 54 |
55 private: | 55 private: |
56 struct CallbackAndParam { | 56 struct CallbackAndParam { |
57 CallbackAndParam(AtExitCallbackType func, void* param) | 57 CallbackAndParam(AtExitCallbackType func, void* param) |
58 : func_(func), param_(param) { } | 58 : func_(func), param_(param) { } |
59 AtExitCallbackType func_; | 59 AtExitCallbackType func_; |
60 void* param_; | 60 void* param_; |
61 }; | 61 }; |
62 | 62 |
63 Lock lock_; | 63 Lock lock_; |
64 std::stack<CallbackAndParam> stack_; | 64 std::stack<CallbackAndParam> stack_; |
65 AtExitManager* next_manager_; // Stack of managers to allow shadowing. | 65 AtExitManager* next_manager_; // Stack of managers to allow shadowing. |
66 | 66 |
67 DISALLOW_COPY_AND_ASSIGN(AtExitManager); | 67 DISALLOW_COPY_AND_ASSIGN(AtExitManager); |
68 }; | 68 }; |
69 | 69 |
70 #if defined(UNIT_TEST) | 70 #if defined(UNIT_TEST) |
71 class ShadowingAtExitManager : public AtExitManager { | 71 class ShadowingAtExitManager : public AtExitManager { |
72 public: | 72 public: |
73 ShadowingAtExitManager() : AtExitManager(true) {} | 73 ShadowingAtExitManager() : AtExitManager(true) {} |
74 }; | 74 }; |
75 #endif // defined(UNIT_TEST) | 75 #endif // defined(UNIT_TEST) |
76 | 76 |
77 } // namespace base | 77 } // namespace base |
78 | 78 |
79 #endif // BASE_AT_EXIT_H_ | 79 #endif // BASE_AT_EXIT_H_ |
OLD | NEW |