[email protected] | fe992bf0 | 2012-07-25 20:36:33 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
5 | #ifndef BASE_AT_EXIT_H_ | ||||
6 | #define BASE_AT_EXIT_H_ | ||||
7 | |||||
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 8 | #include "base/base_export.h" |
[email protected] | 762de91 | 2011-09-06 23:14:47 | [diff] [blame] | 9 | #include "base/callback.h" |
Brett Wilson | 1f07f20e | 2017-10-02 18:55:28 | [diff] [blame] | 10 | #include "base/containers/stack.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 11 | #include "base/macros.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 12 | #include "base/synchronization/lock.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 13 | |
14 | namespace base { | ||||
15 | |||||
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 | ||||
18 | // happen at a really bad time and under the loader lock. This facility is | ||||
19 | // mostly used by base::Singleton. | ||||
20 | // | ||||
21 | // The usage is simple. Early in the main() or WinMain() scope create an | ||||
22 | // AtExitManager object on the stack: | ||||
23 | // int main(...) { | ||||
24 | // base::AtExitManager exit_manager; | ||||
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 25 | // |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 26 | // } |
27 | // When the exit_manager object goes out of scope, all the registered | ||||
28 | // callbacks and singleton destructors will be called. | ||||
29 | |||||
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 30 | class BASE_EXPORT AtExitManager { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 31 | public: |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 32 | typedef void (*AtExitCallbackType)(void*); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 33 | |
34 | AtExitManager(); | ||||
35 | |||||
36 | // The dtor calls all the registered callbacks. Do not try to register more | ||||
37 | // callbacks after this point. | ||||
38 | ~AtExitManager(); | ||||
39 | |||||
40 | // Registers the specified function to be called at exit. The prototype of | ||||
[email protected] | 762de91 | 2011-09-06 23:14:47 | [diff] [blame] | 41 | // the callback function is void func(void*). |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 42 | static void RegisterCallback(AtExitCallbackType func, void* param); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 43 | |
[email protected] | 762de91 | 2011-09-06 23:14:47 | [diff] [blame] | 44 | // Registers the specified task to be called at exit. |
45 | static void RegisterTask(base::Closure task); | ||||
46 | |||||
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 47 | // Calls the functions registered with RegisterCallback in LIFO order. It |
48 | // is possible to register new callbacks after calling this function. | ||||
49 | static void ProcessCallbacksNow(); | ||||
50 | |||||
haraken | bbfdd9f0 | 2017-01-12 07:14:04 | [diff] [blame] | 51 | // Disable all registered at-exit callbacks. This is used only in a single- |
52 | // process mode. | ||||
53 | static void DisableAllAtExitManagers(); | ||||
54 | |||||
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 55 | protected: |
56 | // This constructor will allow this instance of AtExitManager to be created | ||||
57 | // even if one already exists. This should only be used for testing! | ||||
58 | // AtExitManagers are kept on a global stack, and it will be removed during | ||||
59 | // destruction. This allows you to shadow another AtExitManager. | ||||
60 | explicit AtExitManager(bool shadow); | ||||
61 | |||||
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 62 | private: |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 63 | base::Lock lock_; |
Brett Wilson | 1f07f20e | 2017-10-02 18:55:28 | [diff] [blame] | 64 | base::stack<base::Closure> stack_; |
amistry | f35088d | 2016-02-10 02:18:33 | [diff] [blame] | 65 | bool processing_callbacks_; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 66 | AtExitManager* next_manager_; // Stack of managers to allow shadowing. |
67 | |||||
68 | DISALLOW_COPY_AND_ASSIGN(AtExitManager); | ||||
69 | }; | ||||
70 | |||||
[email protected] | 4ea927b | 2009-11-19 09:11:39 | [diff] [blame] | 71 | #if defined(UNIT_TEST) |
72 | class ShadowingAtExitManager : public AtExitManager { | ||||
73 | public: | ||||
74 | ShadowingAtExitManager() : AtExitManager(true) {} | ||||
75 | }; | ||||
76 | #endif // defined(UNIT_TEST) | ||||
77 | |||||
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 78 | } // namespace base |
79 | |||||
80 | #endif // BASE_AT_EXIT_H_ |