]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Added RunnersRegistry, an API to register and, later, run a group of actions.
authorAlex Rousskov <rousskov@measurement-factory.com>
Thu, 14 Apr 2011 04:22:25 +0000 (22:22 -0600)
committerAlex Rousskov <rousskov@measurement-factory.com>
Thu, 14 Apr 2011 04:22:25 +0000 (22:22 -0600)
Useful for keeping general initialization management code (e.g., main.cc)
independent from specific initialization code (e.g., Ipc::Mem::Init) during
staged initialization and cleaning.

src/base/Makefile.am
src/base/RunnersRegistry.cc [new file with mode: 0644]
src/base/RunnersRegistry.h [new file with mode: 0644]
src/main.cc

index 408d20984bf2807d1057a67cec83990fcb9727dc..cd38376cebb3ce50effa1cedaa11bce6d560ff69 100644 (file)
@@ -15,6 +15,8 @@ libbase_la_SOURCES = \
        TidyPointer.h \
        CbcPointer.h \
        InstanceId.h \
+       RunnersRegistry.cc \
+       RunnersRegistry.h \
        Subscription.h \
        TextException.cc \
        TextException.h
diff --git a/src/base/RunnersRegistry.cc b/src/base/RunnersRegistry.cc
new file mode 100644 (file)
index 0000000..87ac733
--- /dev/null
@@ -0,0 +1,58 @@
+#include "config.h"
+#include "base/RunnersRegistry.h"
+#include <list>
+#include <map>
+
+typedef std::list<RegisteredRunner*> Runners;
+typedef std::map<RunnerRegistry, Runners*> Registries;
+
+/// all known registries
+static Registries *TheRegistries = NULL;
+
+/// returns the requested runners list, initializing structures as needed
+static Runners &
+GetRunners(const RunnerRegistry &registryId)
+{
+    if (!TheRegistries)
+        TheRegistries = new Registries;
+
+    if (TheRegistries->find(registryId) == TheRegistries->end())
+        (*TheRegistries)[registryId] = new Runners;
+
+    return *(*TheRegistries)[registryId];
+}
+
+int
+RegisterRunner(const RunnerRegistry &registryId, RegisteredRunner *rr)
+{
+    Runners &runners = GetRunners(registryId);
+    runners.push_back(rr);
+    return runners.size();
+}
+
+int
+ActivateRegistered(const RunnerRegistry &registryId)
+{
+    Runners &runners = GetRunners(registryId);
+    typedef Runners::iterator RRI;
+    for (RRI i = runners.begin(); i != runners.end(); ++i)
+        (*i)->run(registryId);
+    return runners.size();
+}
+
+void
+DeactivateRegistered(const RunnerRegistry &registryId)
+{
+    Runners &runners = GetRunners(registryId);
+    typedef Runners::iterator RRI;
+    while (!runners.empty()) {
+        delete runners.back();
+        runners.pop_back();
+    }
+}
+
+bool
+UseThisStatic(const void *)
+{
+    return true;
+}
diff --git a/src/base/RunnersRegistry.h b/src/base/RunnersRegistry.h
new file mode 100644 (file)
index 0000000..4203a88
--- /dev/null
@@ -0,0 +1,49 @@
+#ifndef SQUID_BASE_RUNNERSREGISTRY_H
+#define SQUID_BASE_RUNNERSREGISTRY_H
+
+/**
+ * This API allows different modules to register with a well-known registry,
+ * be activated by some central processor at some registry-specific time, and
+ * be deactiveated by some central processor at some registry-specific time.
+ *
+ * For example, main.cc may activate registered I/O modules after parsing
+ * squid.conf and deactivate them before exiting.
+ *
+ */
+
+/// well-known registries (currently, deactivation is not performed for these)
+typedef enum {
+    rrAfterConfig, ///< activated by main.cc after parsing squid.conf
+    rrEnd ///< not a real registry, just a label to mark the end of enum
+} RunnerRegistry;
+
+/// a runnable registrant API
+class RegisteredRunner {
+public:
+    // called when this runner's registry is deactivated
+    virtual ~RegisteredRunner() {}
+
+    // called when this runner's registry is activated
+    virtual void run(const RunnerRegistry &r) = 0;
+};
+
+
+/// registers a given runner with the given registry and returns registry count
+int RegisterRunner(const RunnerRegistry &registry, RegisteredRunner *rr);
+
+/// calls run() methods of all runners in the given registry
+int ActivateRegistered(const RunnerRegistry &registry);
+/// deletes all runners in the given registry
+void DeactivateRegistered(const RunnerRegistry &registry);
+
+
+/// convenience function to "use" an otherwise unreferenced static variable
+bool UseThisStatic(const void *);
+
+/// convenience macro: register one RegisteredRunner kid as early as possible
+#define RunnerRegistrationEntry(Registry, Who) \
+    static const bool Who ## _RegisteredWith_ ## Registry = \
+        RegisterRunner(Registry, new Who) > 0 && \
+        UseThisStatic(& Who ## _RegisteredWith_ ## Registry);
+
+#endif /* SQUID_BASE_RUNNERSREGISTRY_H */
index de345a17905e51a89931614d6541241eb7dee46b..6459439e99cc9989c8bea2efb6162d127ac6764d 100644 (file)
@@ -38,6 +38,7 @@
 #include "adaptation/icap/icap_log.h"
 #endif
 #include "auth/Gadgets.h"
+#include "base/RunnersRegistry.h"
 #include "base/TextException.h"
 #if USE_DELAY_POOLS
 #include "ClientDelayConfig.h"
@@ -64,7 +65,6 @@
 #include "StoreFileSystem.h"
 #include "DiskIO/DiskIOModule.h"
 #include "ipc/Kids.h"
-#include "ipc/mem/Pages.h"
 #include "ipc/Coordinator.h"
 #include "ipc/Strand.h"
 #include "ip/tools.h"
@@ -1412,12 +1412,9 @@ SquidMain(int argc, char **argv)
         /* NOTREACHED */
     }
 
-    // XXX: this logic should probably be moved into Ipc::Mem::Init()
-    if (IamMasterProcess())
-        Ipc::Mem::Init();
-    else
-    if (UsingSmp() && IamWorkerProcess())
-        Ipc::Mem::Attach();
+    debugs(1,2, HERE << "Doing post-config initialization\n");
+    ActivateRegistered(rrAfterConfig);
+    // TODO: find a place to call DeactivateRegistered(rrAfterConfig);
 
     if (!opt_no_daemon && Config.workers > 0)
         watch_child(argv);