From: Dmitry Kurochkin Date: Tue, 4 Oct 2011 17:26:58 +0000 (+0400) Subject: Add base class for registered runners that work with shared memory. X-Git-Tag: BumpSslServerFirst.take01~122^2~7 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f5480a9e690773150236ab3f001d9a5c6d946b74;p=thirdparty%2Fsquid.git Add base class for registered runners that work with shared memory. 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. --- diff --git a/src/ipc/mem/Segment.cc b/src/ipc/mem/Segment.cc index 5702fd0f6a..ca31f3ca0b 100644 --- a/src/ipc/mem/Segment.cc +++ b/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 a/src/ipc/mem/Segment.h b/src/ipc/mem/Segment.h index b82ac4fab7..28966f298f 100644 --- a/src/ipc/mem/Segment.h +++ b/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 a/src/tests/stub_tools.cc b/src/tests/stub_tools.cc index 014b561ba6..f86d0d7605 100644 --- a/src/tests/stub_tools.cc +++ b/src/tests/stub_tools.cc @@ -75,6 +75,13 @@ IamMasterProcess() return false; } +bool +InDaemonMode() +{ + fprintf(stderr, "Not implemented"); + return false; +} + bool UsingSmp() {