Coverage Report

Created: 2025-06-13 06:23

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