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/memory/linked_ptr.h" | |
| 13 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 14 | 15 |
| 16 class Task; | |
| 17 | |
| 15 namespace base { | 18 namespace base { |
| 16 | 19 |
| 17 // 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 |
| 18 // 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 |
| 19 // 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 |
| 20 // mostly used by base::Singleton. | 23 // mostly used by base::Singleton. |
| 21 // | 24 // |
| 22 // 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 |
| 23 // AtExitManager object on the stack: | 26 // AtExitManager object on the stack: |
| 24 // int main(...) { | 27 // int main(...) { |
| 25 // base::AtExitManager exit_manager; | 28 // base::AtExitManager exit_manager; |
| 26 // | 29 // |
| 27 // } | 30 // } |
| 28 // When the exit_manager object goes out of scope, all the registered | 31 // When the exit_manager object goes out of scope, all the registered |
| 29 // callbacks and singleton destructors will be called. | 32 // callbacks and singleton destructors will be called. |
| 30 | 33 |
| 31 class BASE_EXPORT AtExitManager { | 34 class BASE_EXPORT AtExitManager { |
| 32 public: | 35 public: |
| 33 typedef void (*AtExitCallbackType)(void*); | 36 typedef void (*AtExitCallbackType)(void*); |
| 34 | 37 |
| 35 AtExitManager(); | 38 AtExitManager(); |
| 36 | 39 |
| 37 // The dtor calls all the registered callbacks. Do not try to register more | 40 // The dtor calls all the registered callbacks. Do not try to register more |
| 38 // callbacks after this point. | 41 // callbacks after this point. |
| 39 ~AtExitManager(); | 42 ~AtExitManager(); |
| 40 | 43 |
| 41 // Registers the specified function to be called at exit. The prototype of | 44 // Registers the specified function to be called at exit. The prototype of |
| 42 // the callback function is void func(). | 45 // the callback function is void func(void*). |
| 43 static void RegisterCallback(AtExitCallbackType func, void* param); | 46 static void RegisterCallback(AtExitCallbackType func, void* param); |
| 44 | 47 |
| 48 // Registers the specified task to be called at exit. The task is deleted | |
| 49 // after it runs. | |
| 50 static void RegisterTask(Task* task); | |
|
darin (slow to review)
2011/09/01 23:35:44
how about using base::Closure instead?
| |
| 51 | |
| 45 // Calls the functions registered with RegisterCallback in LIFO order. It | 52 // Calls the functions registered with RegisterCallback in LIFO order. It |
| 46 // is possible to register new callbacks after calling this function. | 53 // is possible to register new callbacks after calling this function. |
| 47 static void ProcessCallbacksNow(); | 54 static void ProcessCallbacksNow(); |
| 48 | 55 |
| 49 protected: | 56 protected: |
| 50 // This constructor will allow this instance of AtExitManager to be created | 57 // This constructor will allow this instance of AtExitManager to be created |
| 51 // even if one already exists. This should only be used for testing! | 58 // even if one already exists. This should only be used for testing! |
| 52 // AtExitManagers are kept on a global stack, and it will be removed during | 59 // AtExitManagers are kept on a global stack, and it will be removed during |
| 53 // destruction. This allows you to shadow another AtExitManager. | 60 // destruction. This allows you to shadow another AtExitManager. |
| 54 explicit AtExitManager(bool shadow); | 61 explicit AtExitManager(bool shadow); |
| 55 | 62 |
| 56 private: | 63 private: |
| 57 struct CallbackAndParam { | |
| 58 CallbackAndParam(AtExitCallbackType func, void* param) | |
| 59 : func_(func), param_(param) { } | |
| 60 AtExitCallbackType func_; | |
| 61 void* param_; | |
| 62 }; | |
| 63 | |
| 64 base::Lock lock_; | 64 base::Lock lock_; |
| 65 std::stack<CallbackAndParam> stack_; | 65 std::stack<linked_ptr<Task> > stack_; |
| 66 AtExitManager* next_manager_; // Stack of managers to allow shadowing. | 66 AtExitManager* next_manager_; // Stack of managers to allow shadowing. |
| 67 | 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(AtExitManager); | 68 DISALLOW_COPY_AND_ASSIGN(AtExitManager); |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 #if defined(UNIT_TEST) | 71 #if defined(UNIT_TEST) |
| 72 class ShadowingAtExitManager : public AtExitManager { | 72 class ShadowingAtExitManager : public AtExitManager { |
| 73 public: | 73 public: |
| 74 ShadowingAtExitManager() : AtExitManager(true) {} | 74 ShadowingAtExitManager() : AtExitManager(true) {} |
| 75 }; | 75 }; |
| 76 #endif // defined(UNIT_TEST) | 76 #endif // defined(UNIT_TEST) |
| 77 | 77 |
| 78 } // namespace base | 78 } // namespace base |
| 79 | 79 |
| 80 #endif // BASE_AT_EXIT_H_ | 80 #endif // BASE_AT_EXIT_H_ |
| OLD | NEW |