blob: 6bf3f50350ec7f76d2f0193aeafb3ee84975517a [file] [log] [blame]
[email protected]fe992bf02012-07-25 20:36:331// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]b2e97292008-09-02 18:20:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_AT_EXIT_H_
6#define BASE_AT_EXIT_H_
7
8#include <stack>
9
[email protected]0bea7252011-08-05 15:34:0010#include "base/base_export.h"
[email protected]762de912011-09-06 23:14:4711#include "base/callback.h"
avi9b6f42932015-12-26 22:15:1412#include "base/macros.h"
[email protected]20305ec2011-01-21 04:55:5213#include "base/synchronization/lock.h"
[email protected]b2e97292008-09-02 18:20:3414
15namespace base {
16
17// This class provides a facility similar to the CRT atexit(), except that
18// we control when the callbacks are executed. Under Windows for a DLL they
19// happen at a really bad time and under the loader lock. This facility is
20// mostly used by base::Singleton.
21//
22// The usage is simple. Early in the main() or WinMain() scope create an
23// AtExitManager object on the stack:
24// int main(...) {
25// base::AtExitManager exit_manager;
[email protected]52a261f2009-03-03 15:01:1226//
[email protected]b2e97292008-09-02 18:20:3427// }
28// When the exit_manager object goes out of scope, all the registered
29// callbacks and singleton destructors will be called.
30
[email protected]0bea7252011-08-05 15:34:0031class BASE_EXPORT AtExitManager {
[email protected]b2e97292008-09-02 18:20:3432 public:
[email protected]9795ec12008-09-08 09:06:5133 typedef void (*AtExitCallbackType)(void*);
[email protected]b2e97292008-09-02 18:20:3434
35 AtExitManager();
36
37 // The dtor calls all the registered callbacks. Do not try to register more
38 // callbacks after this point.
39 ~AtExitManager();
40
41 // Registers the specified function to be called at exit. The prototype of
[email protected]762de912011-09-06 23:14:4742 // the callback function is void func(void*).
[email protected]9795ec12008-09-08 09:06:5143 static void RegisterCallback(AtExitCallbackType func, void* param);
[email protected]b2e97292008-09-02 18:20:3444
[email protected]762de912011-09-06 23:14:4745 // Registers the specified task to be called at exit.
46 static void RegisterTask(base::Closure task);
47
[email protected]b2e97292008-09-02 18:20:3448 // Calls the functions registered with RegisterCallback in LIFO order. It
49 // is possible to register new callbacks after calling this function.
50 static void ProcessCallbacksNow();
51
harakenbbfdd9f02017-01-12 07:14:0452 // Disable all registered at-exit callbacks. This is used only in a single-
53 // process mode.
54 static void DisableAllAtExitManagers();
55
[email protected]a502bbe72011-01-07 18:06:4556 protected:
57 // This constructor will allow this instance of AtExitManager to be created
58 // even if one already exists. This should only be used for testing!
59 // AtExitManagers are kept on a global stack, and it will be removed during
60 // destruction. This allows you to shadow another AtExitManager.
61 explicit AtExitManager(bool shadow);
62
[email protected]b2e97292008-09-02 18:20:3463 private:
[email protected]20305ec2011-01-21 04:55:5264 base::Lock lock_;
[email protected]762de912011-09-06 23:14:4765 std::stack<base::Closure> stack_;
amistryf35088d2016-02-10 02:18:3366 bool processing_callbacks_;
[email protected]b2e97292008-09-02 18:20:3467 AtExitManager* next_manager_; // Stack of managers to allow shadowing.
68
69 DISALLOW_COPY_AND_ASSIGN(AtExitManager);
70};
71
[email protected]4ea927b2009-11-19 09:11:3972#if defined(UNIT_TEST)
73class ShadowingAtExitManager : public AtExitManager {
74 public:
75 ShadowingAtExitManager() : AtExitManager(true) {}
76};
77#endif // defined(UNIT_TEST)
78
[email protected]b2e97292008-09-02 18:20:3479} // namespace base
80
81#endif // BASE_AT_EXIT_H_