xref: /aosp_15_r20/external/libchrome/base/at_exit.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_AT_EXIT_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_AT_EXIT_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
9*635a8641SAndroid Build Coastguard Worker #include "base/callback.h"
10*635a8641SAndroid Build Coastguard Worker #include "base/containers/stack.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/synchronization/lock.h"
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker namespace base {
15*635a8641SAndroid Build Coastguard Worker 
16*635a8641SAndroid Build Coastguard Worker // This class provides a facility similar to the CRT atexit(), except that
17*635a8641SAndroid Build Coastguard Worker // we control when the callbacks are executed. Under Windows for a DLL they
18*635a8641SAndroid Build Coastguard Worker // happen at a really bad time and under the loader lock. This facility is
19*635a8641SAndroid Build Coastguard Worker // mostly used by base::Singleton.
20*635a8641SAndroid Build Coastguard Worker //
21*635a8641SAndroid Build Coastguard Worker // The usage is simple. Early in the main() or WinMain() scope create an
22*635a8641SAndroid Build Coastguard Worker // AtExitManager object on the stack:
23*635a8641SAndroid Build Coastguard Worker // int main(...) {
24*635a8641SAndroid Build Coastguard Worker //    base::AtExitManager exit_manager;
25*635a8641SAndroid Build Coastguard Worker //
26*635a8641SAndroid Build Coastguard Worker // }
27*635a8641SAndroid Build Coastguard Worker // When the exit_manager object goes out of scope, all the registered
28*635a8641SAndroid Build Coastguard Worker // callbacks and singleton destructors will be called.
29*635a8641SAndroid Build Coastguard Worker 
30*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT AtExitManager {
31*635a8641SAndroid Build Coastguard Worker  public:
32*635a8641SAndroid Build Coastguard Worker   typedef void (*AtExitCallbackType)(void*);
33*635a8641SAndroid Build Coastguard Worker 
34*635a8641SAndroid Build Coastguard Worker   AtExitManager();
35*635a8641SAndroid Build Coastguard Worker 
36*635a8641SAndroid Build Coastguard Worker   // The dtor calls all the registered callbacks. Do not try to register more
37*635a8641SAndroid Build Coastguard Worker   // callbacks after this point.
38*635a8641SAndroid Build Coastguard Worker   ~AtExitManager();
39*635a8641SAndroid Build Coastguard Worker 
40*635a8641SAndroid Build Coastguard Worker   // Registers the specified function to be called at exit. The prototype of
41*635a8641SAndroid Build Coastguard Worker   // the callback function is void func(void*).
42*635a8641SAndroid Build Coastguard Worker   static void RegisterCallback(AtExitCallbackType func, void* param);
43*635a8641SAndroid Build Coastguard Worker 
44*635a8641SAndroid Build Coastguard Worker   // Registers the specified task to be called at exit.
45*635a8641SAndroid Build Coastguard Worker   static void RegisterTask(base::Closure task);
46*635a8641SAndroid Build Coastguard Worker 
47*635a8641SAndroid Build Coastguard Worker   // Calls the functions registered with RegisterCallback in LIFO order. It
48*635a8641SAndroid Build Coastguard Worker   // is possible to register new callbacks after calling this function.
49*635a8641SAndroid Build Coastguard Worker   static void ProcessCallbacksNow();
50*635a8641SAndroid Build Coastguard Worker 
51*635a8641SAndroid Build Coastguard Worker   // Disable all registered at-exit callbacks. This is used only in a single-
52*635a8641SAndroid Build Coastguard Worker   // process mode.
53*635a8641SAndroid Build Coastguard Worker   static void DisableAllAtExitManagers();
54*635a8641SAndroid Build Coastguard Worker 
55*635a8641SAndroid Build Coastguard Worker  protected:
56*635a8641SAndroid Build Coastguard Worker   // This constructor will allow this instance of AtExitManager to be created
57*635a8641SAndroid Build Coastguard Worker   // even if one already exists.  This should only be used for testing!
58*635a8641SAndroid Build Coastguard Worker   // AtExitManagers are kept on a global stack, and it will be removed during
59*635a8641SAndroid Build Coastguard Worker   // destruction.  This allows you to shadow another AtExitManager.
60*635a8641SAndroid Build Coastguard Worker   explicit AtExitManager(bool shadow);
61*635a8641SAndroid Build Coastguard Worker 
62*635a8641SAndroid Build Coastguard Worker  private:
63*635a8641SAndroid Build Coastguard Worker   base::Lock lock_;
64*635a8641SAndroid Build Coastguard Worker   base::stack<base::Closure> stack_;
65*635a8641SAndroid Build Coastguard Worker   bool processing_callbacks_;
66*635a8641SAndroid Build Coastguard Worker   AtExitManager* next_manager_;  // Stack of managers to allow shadowing.
67*635a8641SAndroid Build Coastguard Worker 
68*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(AtExitManager);
69*635a8641SAndroid Build Coastguard Worker };
70*635a8641SAndroid Build Coastguard Worker 
71*635a8641SAndroid Build Coastguard Worker #if defined(UNIT_TEST)
72*635a8641SAndroid Build Coastguard Worker class ShadowingAtExitManager : public AtExitManager {
73*635a8641SAndroid Build Coastguard Worker  public:
ShadowingAtExitManager()74*635a8641SAndroid Build Coastguard Worker   ShadowingAtExitManager() : AtExitManager(true) {}
75*635a8641SAndroid Build Coastguard Worker };
76*635a8641SAndroid Build Coastguard Worker #endif  // defined(UNIT_TEST)
77*635a8641SAndroid Build Coastguard Worker 
78*635a8641SAndroid Build Coastguard Worker }  // namespace base
79*635a8641SAndroid Build Coastguard Worker 
80*635a8641SAndroid Build Coastguard Worker #endif  // BASE_AT_EXIT_H_
81