at_exit.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (c) 2011 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. #ifndef BASE_AT_EXIT_H_
  5. #define BASE_AT_EXIT_H_
  6. #include "base/base_export.h"
  7. #include "base/callback.h"
  8. #include "base/containers/stack.h"
  9. #include "base/synchronization/lock.h"
  10. #include "base/thread_annotations.h"
  11. namespace base {
  12. // This class provides a facility similar to the CRT atexit(), except that
  13. // we control when the callbacks are executed. Under Windows for a DLL they
  14. // happen at a really bad time and under the loader lock. This facility is
  15. // mostly used by base::Singleton.
  16. //
  17. // The usage is simple. Early in the main() or WinMain() scope create an
  18. // AtExitManager object on the stack:
  19. // int main(...) {
  20. // base::AtExitManager exit_manager;
  21. //
  22. // }
  23. // When the exit_manager object goes out of scope, all the registered
  24. // callbacks and singleton destructors will be called.
  25. class BASE_EXPORT AtExitManager {
  26. public:
  27. typedef void (*AtExitCallbackType)(void*);
  28. AtExitManager();
  29. AtExitManager(const AtExitManager&) = delete;
  30. AtExitManager& operator=(const AtExitManager&) = delete;
  31. // The dtor calls all the registered callbacks. Do not try to register more
  32. // callbacks after this point.
  33. ~AtExitManager();
  34. // Registers the specified function to be called at exit. The prototype of
  35. // the callback function is void func(void*).
  36. static void RegisterCallback(AtExitCallbackType func, void* param);
  37. // Registers the specified task to be called at exit.
  38. static void RegisterTask(base::OnceClosure task);
  39. // Calls the functions registered with RegisterCallback in LIFO order. It
  40. // is possible to register new callbacks after calling this function.
  41. static void ProcessCallbacksNow();
  42. // Disable all registered at-exit callbacks. This is used only in a single-
  43. // process mode.
  44. static void DisableAllAtExitManagers();
  45. protected:
  46. // This constructor will allow this instance of AtExitManager to be created
  47. // even if one already exists. This should only be used for testing!
  48. // AtExitManagers are kept on a global stack, and it will be removed during
  49. // destruction. This allows you to shadow another AtExitManager.
  50. explicit AtExitManager(bool shadow);
  51. private:
  52. base::Lock lock_;
  53. base::stack<base::OnceClosure> stack_ GUARDED_BY(lock_);
  54. #if DCHECK_IS_ON()
  55. bool processing_callbacks_ GUARDED_BY(lock_) = false;
  56. #endif
  57. // Stack of managers to allow shadowing.
  58. AtExitManager* const next_manager_;
  59. };
  60. #if defined(UNIT_TEST)
  61. class ShadowingAtExitManager : public AtExitManager {
  62. public:
  63. ShadowingAtExitManager() : AtExitManager(true) {}
  64. };
  65. #endif // defined(UNIT_TEST)
  66. } // namespace base
  67. #endif // BASE_AT_EXIT_H_