]> git.ipfire.org Git - thirdparty/squid.git/blob - src/base/RunnersRegistry.h
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / base / RunnersRegistry.h
1 /*
2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef SQUID_BASE_RUNNERSREGISTRY_H
10 #define SQUID_BASE_RUNNERSREGISTRY_H
11
12 /**
13 * This API allows virtually any module to register its interest in receiving
14 * notification about initial configuration availability, configuration changes
15 * and other critical events in Squid lifetime without exposing the notifier
16 * to the details of the module.
17 *
18 * For example, main.cc may activate registered I/O modules after parsing
19 * squid.conf and deactivate them before exiting, all without knowing what
20 * those I/O modules really are.
21 *
22 * A module in this context is code providing a functionality or service to the
23 * rest of Squid, such as src/DiskIO/Blocking, src/fs/ufs, or Cache Manager. To
24 * receive notifications, a module must declare a RegisteredRunner child class
25 * and implement the methods corresponding to the events the module is
26 * interested in.
27 *
28 * The order of events is documented in this header (where applicable), but
29 * the order in which runners are notified about a given event is undefined.
30 * If a specific notification order is required, split the event into two or
31 * more related event(s), documenting their relative order here.
32 *
33 */
34
35 /// a runnable registrant API
36 /// kids must override [only] the methods they are interested in
37 class RegisteredRunner
38 {
39 public:
40 /* Related methods below are declared in their calling order */
41
42 /* Configuration events */
43
44 /// Called after parsing squid.conf.
45 /// Meant for setting configuration options that depend on other
46 /// configuration options and were not explicitly configured.
47 virtual void finalizeConfig() {}
48
49 /// Called after finalizeConfig().
50 /// Meant for announcing memory reservations before memory is allocated.
51 virtual void claimMemoryNeeds() {}
52
53 /// Called after claimMemoryNeeds().
54 /// Meant for activating modules and features using a finalized
55 /// configuration with known memory requirements.
56 virtual void useConfig() {}
57
58 /* Reconfiguration events */
59
60 /// Called after receiving a reconfigure request and before parsing squid.conf.
61 /// Meant for modules that need to prepare for their configuration being changed
62 /// [outside their control]. The changes end with the syncConfig() event.
63 virtual void startReconfigure() {}
64
65 /// Called after parsing squid.conf during reconfiguration.
66 /// Meant for adjusting the module state based on configuration changes.
67 virtual void syncConfig() {}
68
69 /* Shutdown events */
70
71 /// Called after receiving a shutdown request and before stopping the main
72 /// loop. At least one main loop iteration is guaranteed after this call.
73 /// Meant for cleanup and state saving that may require other modules.
74 virtual void startShutdown() {}
75
76 /// Called after shutdown_lifetime grace period ends and before stopping
77 /// the main loop. At least one main loop iteration is guaranteed after
78 /// this call.
79 /// Meant for cleanup and state saving that may require other modules.
80 virtual void endingShutdown() {}
81
82 /// Called after stopping the main loop and before releasing memory.
83 /// Meant for quick/basic cleanup that does not require any other modules.
84 virtual ~RegisteredRunner() {}
85
86 /// Meant for cleanup of services needed by the already destroyed objects.
87 virtual void finishShutdown() {}
88
89 /// a pointer to one of the above notification methods
90 typedef void (RegisteredRunner::*Method)();
91
92 };
93
94 /// registers a given runner with the given registry and returns true on success
95 bool RegisterRunner(RegisteredRunner *rr);
96
97 /// Calls a given method of all runners.
98 /// All runners are destroyed after the finishShutdown() call.
99 void RunRegistered(const RegisteredRunner::Method &m);
100
101 /// A RegisteredRunner with lifetime determined by forces outside the Registry.
102 class IndependentRunner: public RegisteredRunner
103 {
104 public:
105 virtual ~IndependentRunner() { unregisterRunner(); }
106
107 protected:
108 void registerRunner();
109 void unregisterRunner(); ///< unregisters self; safe to call multiple times
110 };
111
112 /// convenience macro to describe/debug the caller and the method being called
113 #define RunRegisteredHere(m) \
114 debugs(1, 2, "running " # m); \
115 RunRegistered(&m)
116
117 /// convenience function to "use" an otherwise unreferenced static variable
118 bool UseThisStatic(const void *);
119
120 /// convenience macro: register one RegisteredRunner kid as early as possible
121 #define RunnerRegistrationEntry(Who) \
122 static const bool Who ## _Registered_ = \
123 RegisterRunner(new Who) > 0 && \
124 UseThisStatic(& Who ## _Registered_);
125
126 #endif /* SQUID_BASE_RUNNERSREGISTRY_H */
127