[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 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 | |
| 8 | #include <stack> |
| 9 | |
| 10 | #include "base/basictypes.h" |
| 11 | #include "base/lock.h" |
| 12 | |
| 13 | namespace base { |
| 14 | |
| 15 | // This class provides a facility similar to the CRT atexit(), except that |
| 16 | // we control when the callbacks are executed. Under Windows for a DLL they |
| 17 | // happen at a really bad time and under the loader lock. This facility is |
| 18 | // mostly used by base::Singleton. |
| 19 | // |
| 20 | // The usage is simple. Early in the main() or WinMain() scope create an |
| 21 | // AtExitManager object on the stack: |
| 22 | // int main(...) { |
| 23 | // base::AtExitManager exit_manager; |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 24 | // |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 25 | // } |
| 26 | // When the exit_manager object goes out of scope, all the registered |
| 27 | // callbacks and singleton destructors will be called. |
| 28 | |
| 29 | class AtExitManager { |
| 30 | protected: |
| 31 | // This constructor will allow this instance of AtExitManager to be created |
| 32 | // even if one already exists. This should only be used for testing! |
| 33 | // AtExitManagers are kept on a global stack, and it will be removed during |
| 34 | // destruction. This allows you to shadow another AtExitManager. |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 35 | explicit AtExitManager(bool shadow); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 36 | |
| 37 | public: |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 38 | typedef void (*AtExitCallbackType)(void*); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 39 | |
| 40 | AtExitManager(); |
| 41 | |
| 42 | // The dtor calls all the registered callbacks. Do not try to register more |
| 43 | // callbacks after this point. |
| 44 | ~AtExitManager(); |
| 45 | |
| 46 | // Registers the specified function to be called at exit. The prototype of |
| 47 | // the callback function is void func(). |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 48 | static void RegisterCallback(AtExitCallbackType func, void* param); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 49 | |
| 50 | // Calls the functions registered with RegisterCallback in LIFO order. It |
| 51 | // is possible to register new callbacks after calling this function. |
| 52 | static void ProcessCallbacksNow(); |
| 53 | |
| 54 | private: |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 55 | struct CallbackAndParam { |
| 56 | CallbackAndParam(AtExitCallbackType func, void* param) |
| 57 | : func_(func), param_(param) { } |
| 58 | AtExitCallbackType func_; |
| 59 | void* param_; |
| 60 | }; |
| 61 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 62 | Lock lock_; |
[email protected] | 9795ec1 | 2008-09-08 09:06:51 | [diff] [blame] | 63 | std::stack<CallbackAndParam> stack_; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 64 | AtExitManager* next_manager_; // Stack of managers to allow shadowing. |
| 65 | |
| 66 | DISALLOW_COPY_AND_ASSIGN(AtExitManager); |
| 67 | }; |
| 68 | |
[email protected] | 4ea927b | 2009-11-19 09:11:39 | [diff] [blame] | 69 | #if defined(UNIT_TEST) |
| 70 | class ShadowingAtExitManager : public AtExitManager { |
| 71 | public: |
| 72 | ShadowingAtExitManager() : AtExitManager(true) {} |
| 73 | }; |
| 74 | #endif // defined(UNIT_TEST) |
| 75 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 76 | } // namespace base |
| 77 | |
| 78 | #endif // BASE_AT_EXIT_H_ |