Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_AT_EXIT_H_ |
michael@0 | 6 | #define BASE_AT_EXIT_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include <stack> |
michael@0 | 9 | |
michael@0 | 10 | #include "base/basictypes.h" |
michael@0 | 11 | #include "base/lock.h" |
michael@0 | 12 | |
michael@0 | 13 | namespace base { |
michael@0 | 14 | |
michael@0 | 15 | // This class provides a facility similar to the CRT atexit(), except that |
michael@0 | 16 | // we control when the callbacks are executed. Under Windows for a DLL they |
michael@0 | 17 | // happen at a really bad time and under the loader lock. This facility is |
michael@0 | 18 | // mostly used by base::Singleton. |
michael@0 | 19 | // |
michael@0 | 20 | // The usage is simple. Early in the main() or WinMain() scope create an |
michael@0 | 21 | // AtExitManager object on the stack: |
michael@0 | 22 | // int main(...) { |
michael@0 | 23 | // base::AtExitManager exit_manager; |
michael@0 | 24 | // |
michael@0 | 25 | // } |
michael@0 | 26 | // When the exit_manager object goes out of scope, all the registered |
michael@0 | 27 | // callbacks and singleton destructors will be called. |
michael@0 | 28 | |
michael@0 | 29 | class AtExitManager { |
michael@0 | 30 | protected: |
michael@0 | 31 | // This constructor will allow this instance of AtExitManager to be created |
michael@0 | 32 | // even if one already exists. This should only be used for testing! |
michael@0 | 33 | // AtExitManagers are kept on a global stack, and it will be removed during |
michael@0 | 34 | // destruction. This allows you to shadow another AtExitManager. |
michael@0 | 35 | AtExitManager(bool shadow); |
michael@0 | 36 | |
michael@0 | 37 | public: |
michael@0 | 38 | typedef void (*AtExitCallbackType)(void*); |
michael@0 | 39 | |
michael@0 | 40 | AtExitManager(); |
michael@0 | 41 | |
michael@0 | 42 | // The dtor calls all the registered callbacks. Do not try to register more |
michael@0 | 43 | // callbacks after this point. |
michael@0 | 44 | ~AtExitManager(); |
michael@0 | 45 | |
michael@0 | 46 | // Registers the specified function to be called at exit. The prototype of |
michael@0 | 47 | // the callback function is void func(). |
michael@0 | 48 | static void RegisterCallback(AtExitCallbackType func, void* param); |
michael@0 | 49 | |
michael@0 | 50 | // Calls the functions registered with RegisterCallback in LIFO order. It |
michael@0 | 51 | // is possible to register new callbacks after calling this function. |
michael@0 | 52 | static void ProcessCallbacksNow(); |
michael@0 | 53 | |
michael@0 | 54 | static bool AlreadyRegistered(); |
michael@0 | 55 | |
michael@0 | 56 | private: |
michael@0 | 57 | struct CallbackAndParam { |
michael@0 | 58 | CallbackAndParam(AtExitCallbackType func, void* param) |
michael@0 | 59 | : func_(func), param_(param) { } |
michael@0 | 60 | AtExitCallbackType func_; |
michael@0 | 61 | void* param_; |
michael@0 | 62 | }; |
michael@0 | 63 | |
michael@0 | 64 | Lock lock_; |
michael@0 | 65 | std::stack<CallbackAndParam> stack_; |
michael@0 | 66 | AtExitManager* next_manager_; // Stack of managers to allow shadowing. |
michael@0 | 67 | |
michael@0 | 68 | DISALLOW_COPY_AND_ASSIGN(AtExitManager); |
michael@0 | 69 | }; |
michael@0 | 70 | |
michael@0 | 71 | } // namespace base |
michael@0 | 72 | |
michael@0 | 73 | #endif // BASE_AT_EXIT_H_ |