at_exit.h 2.8 KB

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