michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_AT_EXIT_H_ michael@0: #define BASE_AT_EXIT_H_ michael@0: michael@0: #include michael@0: michael@0: #include "base/basictypes.h" michael@0: #include "base/lock.h" michael@0: michael@0: namespace base { michael@0: michael@0: // This class provides a facility similar to the CRT atexit(), except that michael@0: // we control when the callbacks are executed. Under Windows for a DLL they michael@0: // happen at a really bad time and under the loader lock. This facility is michael@0: // mostly used by base::Singleton. michael@0: // michael@0: // The usage is simple. Early in the main() or WinMain() scope create an michael@0: // AtExitManager object on the stack: michael@0: // int main(...) { michael@0: // base::AtExitManager exit_manager; michael@0: // michael@0: // } michael@0: // When the exit_manager object goes out of scope, all the registered michael@0: // callbacks and singleton destructors will be called. michael@0: michael@0: class AtExitManager { michael@0: protected: michael@0: // This constructor will allow this instance of AtExitManager to be created michael@0: // even if one already exists. This should only be used for testing! michael@0: // AtExitManagers are kept on a global stack, and it will be removed during michael@0: // destruction. This allows you to shadow another AtExitManager. michael@0: AtExitManager(bool shadow); michael@0: michael@0: public: michael@0: typedef void (*AtExitCallbackType)(void*); michael@0: michael@0: AtExitManager(); michael@0: michael@0: // The dtor calls all the registered callbacks. Do not try to register more michael@0: // callbacks after this point. michael@0: ~AtExitManager(); michael@0: michael@0: // Registers the specified function to be called at exit. The prototype of michael@0: // the callback function is void func(). michael@0: static void RegisterCallback(AtExitCallbackType func, void* param); michael@0: michael@0: // Calls the functions registered with RegisterCallback in LIFO order. It michael@0: // is possible to register new callbacks after calling this function. michael@0: static void ProcessCallbacksNow(); michael@0: michael@0: static bool AlreadyRegistered(); michael@0: michael@0: private: michael@0: struct CallbackAndParam { michael@0: CallbackAndParam(AtExitCallbackType func, void* param) michael@0: : func_(func), param_(param) { } michael@0: AtExitCallbackType func_; michael@0: void* param_; michael@0: }; michael@0: michael@0: Lock lock_; michael@0: std::stack stack_; michael@0: AtExitManager* next_manager_; // Stack of managers to allow shadowing. michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(AtExitManager); michael@0: }; michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // BASE_AT_EXIT_H_