RunnersRegistry.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 #include "squid.h"
10 #include "base/RunnersRegistry.h"
11 #include "base/TextException.h"
12 #include <set>
13 
15 typedef std::set<RegisteredRunner*> Runners;
19 static bool RunnersGone = false;
20 
23 static inline Runners *
25 {
26  if (!TheRunners && !RunnersGone)
27  TheRunners = new Runners;
28  return TheRunners;
29 }
30 
31 static inline void
33 {
34  if (!dynamic_cast<IndependentRunner*>(rr))
35  delete rr;
36  // else ignore; IndependentRunner
37 }
38 
39 static inline void
41 {
42  Runners *runners = FindRunners();
43  Must(runners);
44  runners->insert(rr);
45 }
46 
47 bool
49 {
50  Must(!dynamic_cast<IndependentRunner*>(rr));
51 
52  if (FindRunners()) {
53  RegisterRunner_(rr);
54  return true;
55  }
56 
57  // past finishShutdown
58  GetRidOfRunner(rr);
59  return false;
60 }
61 
62 void
64 {
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
74  (runner->*event)();
75  }
76  }
77 
79  return;
80 
81  // this is the last event; delete registry-dependent runners (and only them)
82  if (Runners *runners = FindRunners()) {
83  RunnersGone = true;
84  TheRunners = nullptr;
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
88  delete runners;
89  }
90 }
91 
92 /* IndependentRunner */
93 
94 void
96 {
97  if (Runners *runners = FindRunners())
98  runners->erase(this);
99  // else it is too late, finishShutdown() has been called
100 }
101 
102 void
104 {
105  if (FindRunners())
106  RegisterRunner_(this);
107  // else do nothing past finishShutdown
108 }
109 
110 bool
111 UseThisStatic(const void *)
112 {
113  return true;
114 }
115 
void RunRegistered(const RegisteredRunner::Method &event)
static Runners * TheRunners
all known runners
static void RegisterRunner_(RegisteredRunner *rr)
virtual void finishShutdown()
Meant for cleanup of services needed by the already destroyed objects.
bool UseThisStatic(const void *)
convenience function to "use" an otherwise unreferenced static variable
bool RegisterRunner(RegisteredRunner *rr)
registers a given runner with the given registry and returns true on success
static void GetRidOfRunner(RegisteredRunner *rr)
static Runners * FindRunners()
void unregisterRunner()
unregisters self; safe to call multiple times
#define Must(cond)
Definition: TextException.h:89
void(RegisteredRunner::* Method)()
a pointer to one of the above notification methods
#define NULL
Definition: types.h:166
std::set< RegisteredRunner * > Runners
a collection of unique runners, in no particular order
static bool RunnersGone
used to avoid re-creating deleted TheRunners after shutdown finished.

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors