[PATCH 5/9] Add base class for registered runners that work with shared memory.

From: Dmitry Kurochkin <dmitry.kurochkin_at_measurement-factory.com>
Date: Thu, 29 Sep 2011 03:06:03 +0400

Shared memory runners do two main things:

* create shared memory segments
* open shared memory segments created earlier

Each runner has to decide whether it needs to create and/or open
shared memory segments. Common runners share (some) logic in making
this decisions. The purpose of this patch is to implement this logic
in a single place to allow easy reuse and avoid duplication.

---
 src/ipc/mem/Segment.cc  |   20 ++++++++++++++++++++
 src/ipc/mem/Segment.h   |   17 +++++++++++++++++
 src/tests/stub_tools.cc |    7 +++++++
 3 files changed, 44 insertions(+), 0 deletions(-)
diff --git src/ipc/mem/Segment.cc src/ipc/mem/Segment.cc
index 5702fd0..ca31f3c 100644
--- src/ipc/mem/Segment.cc
+++ src/ipc/mem/Segment.cc
@@ -256,3 +256,23 @@ Ipc::Mem::Segment::checkSupport(const char *const context)
 }
 
 #endif // HAVE_SHM
+
+void
+Ipc::Mem::RegisteredRunner::run(const RunnerRegistry &r)
+{
+    // If Squid is built with real segments, we create() real segments
+    // in the master process only.  Otherwise, we create() fake
+    // segments in each worker process.  We assume that only workers
+    // need and can work with fake segments.
+#if HAVE_SHM
+    if (IamMasterProcess())
+#else
+    if (IamWorker())
+#endif
+        create(r);
+
+    // we assume that master process does not need shared segments
+    // unless it is also a worker
+    if (!InDaemonMode() || !IamMasterProcess())
+        open(r);
+}
diff --git src/ipc/mem/Segment.h src/ipc/mem/Segment.h
index b82ac4f..28966f2 100644
--- src/ipc/mem/Segment.h
+++ src/ipc/mem/Segment.h
@@ -6,6 +6,7 @@
 #ifndef SQUID_IPC_MEM_SEGMENT_H
 #define SQUID_IPC_MEM_SEGMENT_H
 
+#include "base/RunnersRegistry.h"
 #include "SquidString.h"
 
 namespace Ipc
@@ -65,6 +66,22 @@ private:
     bool doUnlink; ///< whether the segment should be unlinked on destruction
 };
 
+/// Base class for runners that create and open shared memory segments.
+/// First may run create() method and then open().
+class RegisteredRunner: public ::RegisteredRunner
+{
+public:
+    /* RegisteredRunner API */
+    virtual void run(const RunnerRegistry &r);
+
+protected:
+    /// called when the runner should create a new memory segment
+    virtual void create(const RunnerRegistry &) = 0;
+    /// called when the runner should open a previously created segment,
+    /// not needed if segments are opened in constructor or init methods
+    virtual void open(const RunnerRegistry &) {}
+};
+
 } // namespace Mem
 
 } // namespace Ipc
diff --git src/tests/stub_tools.cc src/tests/stub_tools.cc
index 014b561..f86d0d7 100644
--- src/tests/stub_tools.cc
+++ src/tests/stub_tools.cc
@@ -76,6 +76,13 @@ IamMasterProcess()
 }
 
 bool
+InDaemonMode()
+{
+    fprintf(stderr, "Not implemented");
+    return false;
+}
+
+bool
 UsingSmp()
 {
     fprintf(stderr, "Not implemented");
-- 
1.7.6.3
Received on Wed Sep 28 2011 - 23:07:13 MDT

This archive was generated by hypermail 2.2.0 : Thu Sep 29 2011 - 12:00:03 MDT