Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:47:55

0001 // -*- C++ -*-
0002 //
0003 // Package:     FWCore/SharedMemory
0004 // Class  :     WriteBuffer
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  21/01/2020
0011 //
0012 
0013 // system include files
0014 
0015 // user include files
0016 #include "FWCore/SharedMemory/interface/WriteBuffer.h"
0017 #include "FWCore/Utilities/interface/Exception.h"
0018 //
0019 // constants, enums and typedefs
0020 //
0021 using namespace edm::shared_memory;
0022 
0023 //
0024 // static data member definitions
0025 //
0026 
0027 //
0028 // constructors and destructor
0029 //
0030 WriteBuffer::SMOwner::SMOwner(std::string const& iName, std::size_t iLength) : name_(iName) {
0031   //do a remove first just in case a previous job had a failure and left a same named
0032   // memory object
0033   boost::interprocess::shared_memory_object::remove(iName.c_str());
0034   sm_ = std::make_unique<boost::interprocess::managed_shared_memory>(
0035       boost::interprocess::create_only, iName.c_str(), iLength);
0036 }
0037 
0038 WriteBuffer::SMOwner::~SMOwner() {
0039   if (sm_) {
0040     boost::interprocess::shared_memory_object::remove(name_.c_str());
0041   }
0042 }
0043 
0044 WriteBuffer::SMOwner& WriteBuffer::SMOwner::operator=(WriteBuffer::SMOwner&& other) {
0045   if (sm_) {
0046     boost::interprocess::shared_memory_object::remove(name_.c_str());
0047   }
0048   name_ = std::move(other.name_);
0049   sm_ = std::move(other.sm_);
0050   return *this;
0051 }
0052 
0053 void WriteBuffer::SMOwner::reset() { *this = SMOwner(); }
0054 
0055 WriteBuffer::~WriteBuffer() {
0056   if (sm_) {
0057     sm_->destroy<char>(buffer_names::kBuffer);
0058   }
0059 }
0060 
0061 //
0062 // member functions
0063 //
0064 void WriteBuffer::growBuffer(std::size_t iLength) {
0065   int newBuffer = (bufferInfo_->index_ + 1) % 2;
0066   bool destroyedBuffer = false;
0067   auto oldIndex = bufferInfo_->index_;
0068   if (sm_) {
0069     destroyedBuffer = true;
0070     try {
0071       sm_->destroy<char>(buffer_names::kBuffer);
0072     } catch (boost::interprocess::interprocess_exception const& iExcept) {
0073       throw cms::Exception("SharedMemory")
0074           << "in growBuffer while destroying the shared memory object the following exception was caught\n"
0075           << iExcept.what();
0076     }
0077     try {
0078       sm_.reset();
0079     } catch (boost::interprocess::interprocess_exception const& iExcept) {
0080       throw cms::Exception("SharedMemory")
0081           << "in growBuffer while removing the shared memory object named '" << bufferNames_[bufferInfo_->index_]
0082           << "' the following exception was caught\n"
0083           << iExcept.what();
0084     }
0085   }
0086   try {
0087     sm_ = SMOwner(bufferNames_[newBuffer], iLength + 1024);
0088   } catch (boost::interprocess::interprocess_exception const& iExcept) {
0089     throw cms::Exception("SharedMemory") << "in growBuffer while creating the shared memory object '"
0090                                          << bufferNames_[newBuffer] << "' of length " << iLength + 1024
0091                                          << " the following exception was caught\n"
0092                                          << iExcept.what();
0093   }
0094   assert(sm_.get());
0095   bufferSize_ = iLength;
0096   bufferInfo_->index_ = newBuffer;
0097   bufferInfo_->identifier_ = bufferInfo_->identifier_ + 1;
0098   try {
0099     buffer_ = sm_->construct<char>(buffer_names::kBuffer)[iLength](0);
0100   } catch (boost::interprocess::interprocess_exception const& iExcept) {
0101     cms::Exception except("SharedMemory");
0102     except << "boost::interprocess exception caught: " << iExcept.what();
0103     {
0104       std::ostringstream os;
0105       os << "in growBuffer while creating the buffer within the shared memory object '" << bufferNames_[newBuffer]
0106          << "' with index " << newBuffer << " where the buffer is of length " << iLength;
0107 
0108       if (destroyedBuffer) {
0109         os << " after destroying the previous shared memory object '" << bufferNames_[oldIndex] << "' with index "
0110            << oldIndex;
0111       }
0112       except.addContext(os.str());
0113     }
0114     throw except;
0115   }
0116   assert(buffer_);
0117 }
0118 
0119 //
0120 // const member functions
0121 //
0122 
0123 //
0124 // static member functions
0125 //