| 6 | #define BASE_AT_EXIT_H_ |
| 7 |
| 8 | #include "base/base_export.h" |
| 9 | #include "base/callback.h" |
| 10 | #include "base/containers/stack.h" |
| 11 | #include "base/macros.h" |
| 12 | #include "base/synchronization/lock.h" |
| 13 | #include "base/thread_annotations.h" |
| 14 |
| 15 | namespace base { |
| 16 |
| 17 | // 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 |
| 19 | // happen at a really bad time and under the loader lock. This facility is |
| 20 | // mostly used by base::Singleton. |
| 21 | // |
| 22 | // The usage is simple. Early in the main() or WinMain() scope create an |
| 23 | // AtExitManager object on the stack: |
| 24 | // int main(...) { |
| 25 | // base::AtExitManager exit_manager; |
| 26 | // |
| 27 | // } |
| 28 | // When the exit_manager object goes out of scope, all the registered |
| 29 | // callbacks and singleton destructors will be called. |
| 30 |
| 31 | class BASE_EXPORT AtExitManager { |
| 32 | public: |
| 33 | typedef void (*AtExitCallbackType)(void*); |
| 34 |
| 35 | AtExitManager(); |
| 36 |
| 37 | // The dtor calls all the registered callbacks. Do not try to register more |
| 38 | // callbacks after this point. |
| 39 | ~AtExitManager(); |
| 40 |
| 41 | // Registers the specified function to be called at exit. The prototype of |
| 42 | // the callback function is void func(void*). |
| 43 | static void RegisterCallback(AtExitCallbackType func, void* param); |
| 44 |
| 45 | // Registers the specified task to be called at exit. |
| 46 | static void RegisterTask(base::OnceClosure task); |
| 47 |
| 48 | // Calls the functions registered with RegisterCallback in LIFO order. It |
| 49 | // is possible to register new callbacks after calling this function. |
| 50 | static void ProcessCallbacksNow(); |
| 51 |
| 52 | // Disable all registered at-exit callbacks. This is used only in a single- |
| 53 | // process mode. |
| 54 | static void DisableAllAtExitManagers(); |
| 55 |
| 56 | protected: |
| 57 | // This constructor will allow this instance of AtExitManager to be created |
| 58 | // even if one already exists. This should only be used for testing! |
| 59 | // AtExitManagers are kept on a global stack, and it will be removed during |
| 60 | // destruction. This allows you to shadow another AtExitManager. |
| 61 | explicit AtExitManager(bool shadow); |
| 62 |
| 63 | private: |
| 64 | base::Lock lock_; |
| 65 |
| 66 | base::stack<base::OnceClosure> stack_ GUARDED_BY(lock_); |
| 67 |
| 68 | #if DCHECK_IS_ON() |
| 69 | bool processing_callbacks_ GUARDED_BY(lock_) = false; |
| 70 | #endif |
| 71 |
| 72 | // Stack of managers to allow shadowing. |
| 73 | AtExitManager* const next_manager_; |
| 74 |
| 75 | DISALLOW_COPY_AND_ASSIGN(AtExitManager); |
| 76 | }; |
| 77 |
| 78 | #if defined(UNIT_TEST) |
| 79 | class ShadowingAtExitManager : public AtExitManager { |
| 80 | public: |
| 81 | ShadowingAtExitManager() : AtExitManager(true) {} |
| 82 | }; |
| 83 | #endif // defined(UNIT_TEST) |
| 84 |
| 85 | } // namespace base |
| 86 |
| 87 | #endif // BASE_AT_EXIT_H_ |
| 88 |