at_exit.h 2.7 KB

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