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