CMS 3D CMS Logo

WriteBuffer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FWCore/SharedMemory
4 // Class : WriteBuffer
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Chris Jones
10 // Created: 21/01/2020
11 //
12 
13 // system include files
14 
15 // user include files
18 //
19 // constants, enums and typedefs
20 //
21 using namespace edm::shared_memory;
22 
23 //
24 // static data member definitions
25 //
26 
27 //
28 // constructors and destructor
29 //
30 WriteBuffer::SMOwner::SMOwner(std::string const& iName, std::size_t iLength) : name_(iName) {
31  //do a remove first just in case a previous job had a failure and left a same named
32  // memory object
34  sm_ = std::make_unique<boost::interprocess::managed_shared_memory>(
35  boost::interprocess::create_only, iName.c_str(), iLength);
36 }
37 
39  if (sm_) {
41  }
42 }
43 
45  if (sm_) {
47  }
48  name_ = std::move(other.name_);
49  sm_ = std::move(other.sm_);
50  return *this;
51 }
52 
53 void WriteBuffer::SMOwner::reset() { *this = SMOwner(); }
54 
56  if (sm_) {
57  sm_->destroy<char>(buffer_names::kBuffer);
58  }
59 }
60 
61 //
62 // member functions
63 //
64 void WriteBuffer::growBuffer(std::size_t iLength) {
65  int newBuffer = (bufferInfo_->index_ + 1) % 2;
66  bool destroyedBuffer = false;
67  auto oldIndex = bufferInfo_->index_;
68  if (sm_) {
69  destroyedBuffer = true;
70  try {
71  sm_->destroy<char>(buffer_names::kBuffer);
72  } catch (boost::interprocess::interprocess_exception const& iExcept) {
73  throw cms::Exception("SharedMemory")
74  << "in growBuffer while destroying the shared memory object the following exception was caught\n"
75  << iExcept.what();
76  }
77  try {
78  sm_.reset();
79  } catch (boost::interprocess::interprocess_exception const& iExcept) {
80  throw cms::Exception("SharedMemory")
81  << "in growBuffer while removing the shared memory object named '" << bufferNames_[bufferInfo_->index_]
82  << "' the following exception was caught\n"
83  << iExcept.what();
84  }
85  }
86  try {
87  sm_ = SMOwner(bufferNames_[newBuffer], iLength + 1024);
88  } catch (boost::interprocess::interprocess_exception const& iExcept) {
89  throw cms::Exception("SharedMemory") << "in growBuffer while creating the shared memory object '"
90  << bufferNames_[newBuffer] << "' of length " << iLength + 1024
91  << " the following exception was caught\n"
92  << iExcept.what();
93  }
94  assert(sm_.get());
95  bufferSize_ = iLength;
96  bufferInfo_->index_ = newBuffer;
98  try {
99  buffer_ = sm_->construct<char>(buffer_names::kBuffer)[iLength](0);
100  } catch (boost::interprocess::interprocess_exception const& iExcept) {
101  cms::Exception except("SharedMemory");
102  except << "boost::interprocess exception caught: " << iExcept.what();
103  {
104  std::ostringstream os;
105  os << "in growBuffer while creating the buffer within the shared memory object '" << bufferNames_[newBuffer]
106  << "' with index " << newBuffer << " where the buffer is of length " << iLength;
107 
108  if (destroyedBuffer) {
109  os << " after destroying the previous shared memory object '" << bufferNames_[oldIndex] << "' with index "
110  << oldIndex;
111  }
112  except.addContext(os.str());
113  }
114  throw except;
115  }
116  assert(buffer_);
117 }
118 
119 //
120 // const member functions
121 //
122 
123 //
124 // static member functions
125 //
constexpr char const *const kBuffer
Definition: buffer_names.h:29
struct edm::shared_memory::WriteBuffer::SMOwner sm_
assert(be >=bs)
std::unique_ptr< boost::interprocess::managed_shared_memory > sm_
Definition: WriteBuffer.h:84
void growBuffer(std::size_t iLength)
Definition: WriteBuffer.cc:64
std::array< std::string, 2 > bufferNames_
Definition: WriteBuffer.h:71
void addContext(std::string const &context)
Definition: Exception.cc:165
def remove(d, key, TELL=False)
Definition: MatrixUtil.py:223
boost::interprocess::managed_shared_memory * get()
Definition: WriteBuffer.h:78
char const * what() const noexcept override
Definition: Exception.cc:103
def move(src, dest)
Definition: eostools.py:511