17#include <boost/interprocess/containers/vector.hpp>
18#include <boost/interprocess/containers/string.hpp>
19#include <boost/interprocess/containers/map.hpp>
21#include <boost/interprocess/managed_windows_shared_memory.hpp>
23#include <boost/interprocess/managed_shared_memory.hpp>
39#if defined(BOOST_INTERPROCESS_POSIX_BARRIERS) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
55typedef boost::interprocess::managed_windows_shared_memory
managed_shm;
57typedef boost::interprocess::managed_shared_memory
managed_shm;
74template<
class T,
class allocator>
84 ShmDebugInfo(
char type =
'\0',
const std::string &name =
"",
shm_handle_t handle = 0)
85 : handle(handle), deleted(0), type(type)
87 memset(this->name,
'\0',
sizeof(this->name));
88 strncpy(this->name, name.c_str(),
sizeof(this->name) - 1);
97 typedef std::basic_string<T>
string;
98 typedef std::vector<T>
vector;
104 Constructor(
const std::string &name);
106 template<
typename... Args>
107 T *operator()(Args &&...args);
109 static Constructor
construct(
const std::string &name) {
return Constructor(name); }
111 typedef boost::interprocess::allocator<T, managed_shm::segment_manager>
allocator;
113 typedef boost::interprocess::vector<T, allocator>
vector;
115 typedef boost::interprocess::offset_ptr<array>
array_ptr;
116 static typename managed_shm::segment_manager::template construct_proxy<T>::type
construct(
const std::string &name);
118 static T *
find(
const std::string &name);
119 static bool destroy(
const std::string &name);
132 friend class Communicator;
135 static std::string instanceName(
const std::string &host,
unsigned short port);
137 static bool perRank();
138 static bool remove(
const std::string &shmname,
int moduleID,
int rank,
bool perRank);
139 static Shm &create(
const std::string &shmname,
int moduleID,
int rank,
bool perRank);
140 static Shm &attach(
const std::string &shmname,
int moduleID,
int rank,
bool perRank);
141 static bool isAttached();
144 void setRemoveOnDetach();
146 std::string name()
const;
147 const std::string &instanceName()
const;
148 int owningRank()
const;
153 typedef boost::interprocess::allocator<void, managed_shm::segment_manager>
void_allocator;
159 std::string createArrayId(
const std::string &name =
"");
160 std::string createObjectId(
const std::string &name =
"");
162 void lockObjects()
const;
163 void unlockObjects()
const;
164 void lockDictionary()
const;
165 void unlockDictionary()
const;
167 int objectID()
const;
169 void setObjectID(
int id);
170 void setArrayID(
int id);
172 std::shared_ptr<const Object> getObjectFromHandle(
const shm_handle_t &handle)
const;
176 ObjectData *getObjectDataFromName(
const std::string &name)
const;
178 std::shared_ptr<const Object> getObjectFromName(
const std::string &name,
bool onlyComplete =
true)
const;
180 const ShmVector<T> getArrayFromName(
const std::string &name)
const;
182 static std::string shmIdFilename();
183 static bool cleanAll(
int rank);
185 void markAsRemoved(
const std::string &name);
186 void addObject(
const std::string &name,
const shm_handle_t &handle);
187 void addArray(
const std::string &name,
const ShmData *array);
191 static std::recursive_mutex *s_shmdebugMutex;
194 static boost::interprocess::interprocess_recursive_mutex *s_shmdebugMutex;
199 pthread_barrier_t *newBarrier(
const std::string &name,
int count);
200 void deleteBarrier(
const std::string &name);
205 Shm(
const std::string &name,
const int moduleID,
const int rank,
size_t size = 0);
215 int m_owningRank = -1;
216 std::atomic<int> m_objectId, m_arrayId;
217 static Shm *s_singleton;
219 mutable std::recursive_mutex *m_shmDeletionMutex;
220 mutable std::recursive_mutex *m_objectDictionaryMutex;
221 std::map<std::string, shm_handle_t> m_objectDictionary;
223 static bool s_perRank;
224 mutable boost::interprocess::interprocess_recursive_mutex *m_shmDeletionMutex;
225 mutable boost::interprocess::interprocess_recursive_mutex *m_objectDictionaryMutex;
228 mutable std::atomic<int> m_lockCount;
231 std::map<std::string, boost::interprocess::ipcdetail::barrier_initializer> m_barriers;
241 auto &dict =
Shm::the().m_objectDictionary;
242 auto it = dict.find(name);
243 if (it == dict.end()) {
248 return static_cast<T *
>(it->second);
250 return Shm::the().
shm().find<T>(name.c_str()).first;
269 assert(t->refcount() > 0);
281 auto &dict =
Shm::the().m_objectDictionary;
282 auto it = dict.find(name);
284 if (it == dict.end()) {
286 std::cerr <<
"WARNING: shm: did not find object " << name <<
" to be deleted" << std::endl;
289 T *t =
static_cast<T *
>(it->second);
295 const bool ret =
Shm::the().
shm().destroy<T>(name.c_str());
306 if (arr->refcount() > 0) {
315 if (arr->refcount() > 0) {
319 assert(arr->refcount() == 0);
334shm<T>::Constructor::~Constructor()
340template<
typename... Args>
341T *shm<T>::Constructor::operator()(Args &&...args)
343 auto &dict =
Shm::the().m_objectDictionary;
344 auto it = dict.find(name);
345 if (it != dict.end()) {
346 std::cerr <<
"WARNING: shm: already have " << name << std::endl;
347 return reinterpret_cast<T *
>(it->second);
350 T *t =
new T(std::forward<Args>(args)...);
friend struct shm
Definition: shm.h:131
void unlockObjects() const
Definition: shm.cpp:203
static Shm & the()
Definition: shm.cpp:315
void lockObjects() const
Definition: shm.cpp:191
boost::interprocess::allocator< void, managed_shm::segment_manager > void_allocator
Definition: shm.h:153
void unlockDictionary() const
Definition: shm.cpp:222
shm_handle_t getHandleFromObject(std::shared_ptr< const Object > object) const
void markAsRemoved(const std::string &name)
Definition: shm.cpp:481
void lockDictionary() const
Definition: shm.cpp:215
Definition: allocator.h:12
Definition: exception.h:13
Definition: shm_reference.h:15
Definition: shm_array.h:19
shm_exception(const std::string &what="shared memory error")
Definition: shm.h:69
#define V_COREEXPORT
Definition: export.h:9
Definition: allobjects.cpp:30
boost::interprocess::managed_shared_memory managed_shm
Definition: shm.h:57
managed_shm::handle_t shm_handle_t
Definition: object.h:40
default_init_allocator< T > allocator
Definition: allocator.h:36
static T * find(const std::string &name)
Definition: shm.h:237
boost::interprocess::offset_ptr< array > array_ptr
Definition: shm.h:115
static bool destroy_array(const std::string &name, array_ptr arr)
Definition: shm.h:302
static T * find_and_ref(const std::string &name)
Definition: shm.h:255
static bool destroy(const std::string &name)
Definition: shm.h:277
boost::interprocess::basic_string< T, std::char_traits< T >, allocator > string
Definition: shm.h:112
boost::interprocess::allocator< T, managed_shm::segment_manager > allocator
Definition: shm.h:111
boost::interprocess::vector< T, allocator > vector
Definition: shm.h:113
vistle::shm_array< T, allocator > array
Definition: shm.h:114
static managed_shm::segment_manager::template construct_proxy< T >::type construct(const std::string &name)
Definition: shm_impl.h:87