OLD | NEW |
1 // Copyright (c) 2012 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 | 7 |
8 #include <stack> | 8 #include <stack> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 | 16 |
17 class TestWatchAtExitManager; | |
18 | |
19 // This class provides a facility similar to the CRT atexit(), except that | 17 // This class provides a facility similar to the CRT atexit(), except that |
20 // we control when the callbacks are executed. Under Windows for a DLL they | 18 // we control when the callbacks are executed. Under Windows for a DLL they |
21 // happen at a really bad time and under the loader lock. This facility is | 19 // happen at a really bad time and under the loader lock. This facility is |
22 // mostly used by base::Singleton. | 20 // mostly used by base::Singleton. |
23 // | 21 // |
24 // The usage is simple. Early in the main() or WinMain() scope create an | 22 // The usage is simple. Early in the main() or WinMain() scope create an |
25 // AtExitManager object on the stack: | 23 // AtExitManager object on the stack: |
26 // int main(...) { | 24 // int main(...) { |
27 // base::AtExitManager exit_manager; | 25 // base::AtExitManager exit_manager; |
28 // | 26 // |
(...skipping 23 matching lines...) Expand all Loading... |
52 static void ProcessCallbacksNow(); | 50 static void ProcessCallbacksNow(); |
53 | 51 |
54 protected: | 52 protected: |
55 // This constructor will allow this instance of AtExitManager to be created | 53 // This constructor will allow this instance of AtExitManager to be created |
56 // even if one already exists. This should only be used for testing! | 54 // even if one already exists. This should only be used for testing! |
57 // AtExitManagers are kept on a global stack, and it will be removed during | 55 // AtExitManagers are kept on a global stack, and it will be removed during |
58 // destruction. This allows you to shadow another AtExitManager. | 56 // destruction. This allows you to shadow another AtExitManager. |
59 explicit AtExitManager(bool shadow); | 57 explicit AtExitManager(bool shadow); |
60 | 58 |
61 private: | 59 private: |
62 friend class TestWatchAtExitManager; | |
63 | |
64 static AtExitManager* current(); | |
65 size_t CallbackStackSize() const; | |
66 | |
67 base::Lock lock_; | 60 base::Lock lock_; |
68 std::stack<base::Closure> stack_; | 61 std::stack<base::Closure> stack_; |
69 AtExitManager* next_manager_; // Stack of managers to allow shadowing. | 62 AtExitManager* next_manager_; // Stack of managers to allow shadowing. |
70 | 63 |
71 DISALLOW_COPY_AND_ASSIGN(AtExitManager); | 64 DISALLOW_COPY_AND_ASSIGN(AtExitManager); |
72 }; | 65 }; |
73 | 66 |
74 #if defined(UNIT_TEST) | 67 #if defined(UNIT_TEST) |
75 class ShadowingAtExitManager : public AtExitManager { | 68 class ShadowingAtExitManager : public AtExitManager { |
76 public: | 69 public: |
77 ShadowingAtExitManager() : AtExitManager(true) {} | 70 ShadowingAtExitManager() : AtExitManager(true) {} |
78 }; | 71 }; |
79 #endif // defined(UNIT_TEST) | 72 #endif // defined(UNIT_TEST) |
80 | 73 |
81 } // namespace base | 74 } // namespace base |
82 | 75 |
83 #endif // BASE_AT_EXIT_H_ | 76 #endif // BASE_AT_EXIT_H_ |
OLD | NEW |