1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/at_exit.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,76 @@ 1.4 +// Copyright (c) 2011 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef BASE_AT_EXIT_H_ 1.9 +#define BASE_AT_EXIT_H_ 1.10 + 1.11 +#include <stack> 1.12 + 1.13 +#include "base/base_export.h" 1.14 +#include "base/basictypes.h" 1.15 +#include "base/callback.h" 1.16 +#include "base/synchronization/lock.h" 1.17 + 1.18 +namespace base { 1.19 + 1.20 +// This class provides a facility similar to the CRT atexit(), except that 1.21 +// we control when the callbacks are executed. Under Windows for a DLL they 1.22 +// happen at a really bad time and under the loader lock. This facility is 1.23 +// mostly used by base::Singleton. 1.24 +// 1.25 +// The usage is simple. Early in the main() or WinMain() scope create an 1.26 +// AtExitManager object on the stack: 1.27 +// int main(...) { 1.28 +// base::AtExitManager exit_manager; 1.29 +// 1.30 +// } 1.31 +// When the exit_manager object goes out of scope, all the registered 1.32 +// callbacks and singleton destructors will be called. 1.33 + 1.34 +class BASE_EXPORT AtExitManager { 1.35 + public: 1.36 + typedef void (*AtExitCallbackType)(void*); 1.37 + 1.38 + AtExitManager(); 1.39 + 1.40 + // The dtor calls all the registered callbacks. Do not try to register more 1.41 + // callbacks after this point. 1.42 + ~AtExitManager(); 1.43 + 1.44 + // Registers the specified function to be called at exit. The prototype of 1.45 + // the callback function is void func(void*). 1.46 + static void RegisterCallback(AtExitCallbackType func, void* param); 1.47 + 1.48 + // Registers the specified task to be called at exit. 1.49 + static void RegisterTask(base::Closure task); 1.50 + 1.51 + // Calls the functions registered with RegisterCallback in LIFO order. It 1.52 + // is possible to register new callbacks after calling this function. 1.53 + static void ProcessCallbacksNow(); 1.54 + 1.55 + protected: 1.56 + // This constructor will allow this instance of AtExitManager to be created 1.57 + // even if one already exists. This should only be used for testing! 1.58 + // AtExitManagers are kept on a global stack, and it will be removed during 1.59 + // destruction. This allows you to shadow another AtExitManager. 1.60 + explicit AtExitManager(bool shadow); 1.61 + 1.62 + private: 1.63 + base::Lock lock_; 1.64 + std::stack<base::Closure> stack_; 1.65 + AtExitManager* next_manager_; // Stack of managers to allow shadowing. 1.66 + 1.67 + DISALLOW_COPY_AND_ASSIGN(AtExitManager); 1.68 +}; 1.69 + 1.70 +#if defined(UNIT_TEST) 1.71 +class ShadowingAtExitManager : public AtExitManager { 1.72 + public: 1.73 + ShadowingAtExitManager() : AtExitManager(true) {} 1.74 +}; 1.75 +#endif // defined(UNIT_TEST) 1.76 + 1.77 +} // namespace base 1.78 + 1.79 +#endif // BASE_AT_EXIT_H_