FairMQ  1.2.3
C++ Message Passing Framework
ShmChunk.h
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
15 #ifndef SHMCHUNK_H_
16 #define SHMCHUNK_H_
17 
18 #include <thread>
19 #include <chrono>
20 
21 #include <boost/interprocess/managed_shared_memory.hpp>
22 #include <boost/interprocess/smart_ptr/shared_ptr.hpp>
23 
24 #include "FairMQLogger.h"
25 
26 namespace bipc = boost::interprocess;
27 
29 {
30  public:
31  static SegmentManager& Instance()
32  {
33  static SegmentManager man;
34  return man;
35  }
36 
37  void InitializeSegment(const std::string& op, const std::string& name, const size_t size = 0)
38  {
39  if (!fSegment)
40  {
41  try
42  {
43  if (op == "open_or_create")
44  {
45  fSegment = new bipc::managed_shared_memory(bipc::open_or_create, name.c_str(), size);
46  }
47  else if (op == "create_only")
48  {
49  fSegment = new bipc::managed_shared_memory(bipc::create_only, name.c_str(), size);
50  }
51  else if (op == "open_only")
52  {
53  int numTries = 0;
54  bool success = false;
55 
56  do
57  {
58  try
59  {
60  fSegment = new bipc::managed_shared_memory(bipc::open_only, name.c_str());
61  success = true;
62  }
63  catch (bipc::interprocess_exception& ie)
64  {
65  if (++numTries == 5)
66  {
67  LOG(error) << "Could not open shared memory after " << numTries << " attempts, exiting!";
68  exit(EXIT_FAILURE);
69  }
70  else
71  {
72  LOG(debug) << "Could not open shared memory segment on try " << numTries << ". Retrying in 1 second...";
73  LOG(debug) << ie.what();
74 
75  std::this_thread::sleep_for(std::chrono::milliseconds(1000));
76  }
77  }
78  }
79  while (!success);
80  }
81  else
82  {
83  LOG(error) << "Unknown operation when initializing shared memory segment: " << op;
84  }
85  }
86  catch (std::exception& e)
87  {
88  LOG(error) << "Exception during shared memory segment initialization: " << e.what() << ", application will now exit";
89  exit(EXIT_FAILURE);
90  }
91  }
92  else
93  {
94  LOG(info) << "Segment already initialized";
95  }
96  }
97 
98  bipc::managed_shared_memory* Segment() const
99  {
100  if (fSegment)
101  {
102  return fSegment;
103  }
104  else
105  {
106  LOG(error) << "Segment not initialized";
107  exit(EXIT_FAILURE);
108  }
109  }
110 
111  private:
113  : fSegment(nullptr)
114  {}
115 
116  bipc::managed_shared_memory* fSegment;
117 };
118 
119 struct alignas(16) ExMetaHeader
120 {
121  uint64_t fSize;
122  bipc::managed_shared_memory::handle_t fHandle;
123 };
124 
125 // class ShmChunk
126 // {
127 // public:
128 // ShmChunk()
129 // : fHandle()
130 // , fSize(0)
131 // {
132 // }
133 
134 // ShmChunk(const size_t size)
135 // : fHandle()
136 // , fSize(size)
137 // {
138 // void* ptr = SegmentManager::Instance().Segment()->allocate(size);
139 // fHandle = SegmentManager::Instance().Segment()->get_handle_from_address(ptr);
140 // }
141 
142 // ~ShmChunk()
143 // {
144 // SegmentManager::Instance().Segment()->deallocate(SegmentManager::Instance().Segment()->get_address_from_handle(fHandle));
145 // }
146 
147 // bipc::managed_shared_memory::handle_t GetHandle() const
148 // {
149 // return fHandle;
150 // }
151 
152 // void* GetData() const
153 // {
154 // return SegmentManager::Instance().Segment()->get_address_from_handle(fHandle);
155 // }
156 
157 // size_t GetSize() const
158 // {
159 // return fSize;
160 // }
161 
162 // private:
163 // bipc::managed_shared_memory::handle_t fHandle;
164 // size_t fSize;
165 // };
166 
167 // typedef bipc::managed_shared_ptr<ShmChunk, bipc::managed_shared_memory>::type ShPtrType;
168 
169 // struct ShPtrOwner
170 // {
171 // ShPtrOwner(const ShPtrType& other)
172 // : fPtr(other)
173 // {}
174 
175 // ShPtrOwner(const ShPtrOwner& other)
176 // : fPtr(other.fPtr)
177 // {}
178 
179 // ShPtrType fPtr;
180 // };
181 
182 #endif /* SHMCHUNK_H_ */
Definition: ShmChunk.h:28
Definition: ShmChunk.h:119