]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/base/RunnersRegistry.cc
2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
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.
10 #include "base/RunnersRegistry.h"
11 #include "base/TextException.h"
14 /// a collection of unique runners, in no particular order
15 typedef std::set
<RegisteredRunner
*> Runners
;
17 static Runners
*TheRunners
= NULL
;
18 /// used to avoid re-creating deleted TheRunners after shutdown finished.
19 static bool RunnersGone
= false;
21 /// creates the registered runners container if needed
22 /// \return either registered runners (if they should exist) or nil (otherwise)
23 static inline Runners
*
26 if (!TheRunners
&& !RunnersGone
)
27 TheRunners
= new Runners
;
32 GetRidOfRunner(RegisteredRunner
*rr
)
34 if (!dynamic_cast<IndependentRunner
*>(rr
))
36 // else ignore; IndependentRunner
40 RegisterRunner_(RegisteredRunner
*rr
)
42 Runners
*runners
= FindRunners();
48 RegisterRunner(RegisteredRunner
*rr
)
50 Must(!dynamic_cast<IndependentRunner
*>(rr
));
57 // past finishShutdown
63 RunRegistered(const RegisteredRunner::Method
&event
)
65 if (Runners
*runners
= FindRunners()) {
66 // Many things may happen during the loop below. We copy to withstand
67 // runner removal/addition and avoid surprises due to registrations from
68 // parent constructors (with a half-baked "this"!). This copy also
69 // simplifies overall RR logic as it guarantees that registering a
70 // runner during event X loop does not execute runner::X().
71 Runners
oldRunners(*runners
);
72 for (auto runner
: oldRunners
) {
73 if (runners
->find(runner
) != runners
->end()) // still registered
78 if (event
!= &RegisteredRunner::finishShutdown
)
81 // this is the last event; delete registry-dependent runners (and only them)
82 if (Runners
*runners
= FindRunners()) {
85 // from now on, no runners can be registered or unregistered
86 for (auto runner
: *runners
)
87 GetRidOfRunner(runner
); // leaves a dangling pointer in runners
92 /* IndependentRunner */
95 IndependentRunner::unregisterRunner()
97 if (Runners
*runners
= FindRunners())
99 // else it is too late, finishShutdown() has been called
103 IndependentRunner::registerRunner()
106 RegisterRunner_(this);
107 // else do nothing past finishShutdown
111 UseThisStatic(const void *)