From a08443372149256a5980ea66ffbc510c9a6b9a76 Mon Sep 17 00:00:00 2001 From: randomdan Date: Wed, 25 Aug 2010 18:45:03 +0000 Subject: Don't create the SHM session mapping until something tries to use it --- project2/sessionShm.cpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/project2/sessionShm.cpp b/project2/sessionShm.cpp index e4c9d16..8064142 100644 --- a/project2/sessionShm.cpp +++ b/project2/sessionShm.cpp @@ -24,13 +24,14 @@ class SessionShmData { SessionValues svs; time_t expiryTime; - static boost::interprocess::managed_mapped_file shm; + static boost::interprocess::managed_mapped_file & shm(); + static boost::shared_ptr _shm; friend class SessionContainerShm; }; const char * shmFile = "/tmp/project2.sessions"; -boost::interprocess::managed_mapped_file SessionShmData::shm(open_or_create, shmFile, 1024 * 1024); +boost::shared_ptr SessionShmData::_shm; SessionContainerShm::SessionContainerShm() { @@ -44,8 +45,8 @@ void SessionContainerShm::CleanUp() { std::set toDelete; - for (managed_mapped_file::const_named_iterator i = SessionShmData::shm.named_begin(); - i != SessionShmData::shm.named_end(); i++) { + for (managed_mapped_file::const_named_iterator i = SessionShmData::shm().named_begin(); + i != SessionShmData::shm().named_end(); i++) { const SessionShmData * s = static_cast(i->value()); if (s) { if (s->expiryTime < time(NULL)) { @@ -54,7 +55,7 @@ SessionContainerShm::CleanUp() } } for(std::set::const_iterator s = toDelete.begin(); s != toDelete.end(); s++) { - SessionShmData::shm.destroy(s->c_str()); + SessionShmData::shm().destroy(s->c_str()); } } @@ -65,7 +66,7 @@ SessionContainerShm::getSession(boost::uuids::uuid & sid) sid = boost::uuids::random_generator()(); } return SessionPtr(new SessionShm( - SessionShmData::shm.find_or_construct(boost::lexical_cast(sid).c_str())())); + SessionShmData::shm().find_or_construct(boost::lexical_cast(sid).c_str())())); } SessionShm::SessionShm(SessionShmData * d) : data(d) @@ -89,7 +90,7 @@ SessionShm::ExpiryTime() const } SessionShmData::SessionShmData() : - svs(std::less(), AllocatorValueType(shm.get_segment_manager())) + svs(std::less(), AllocatorValueType(shm().get_segment_manager())) { } @@ -97,6 +98,16 @@ SessionShmData::~SessionShmData() { } +boost::interprocess::managed_mapped_file & +SessionShmData::shm() +{ + if (!_shm) { + _shm = boost::shared_ptr( + new boost::interprocess::managed_mapped_file(open_or_create, shmFile, 1024 * 1024)); + } + return *_shm; +} + Glib::ustring SessionShm::GetValue(const Glib::ustring & name) const { -- cgit v1.2.3