View on GitHub

Vistle

Distributed Data-parallel Scientific Visualization in VR

shm_reference.h
Go to the documentation of this file.
1#ifndef SHM_REFRENCE_H
2#define SHM_REFRENCE_H
3
4#include "archives_config.h"
5#include <string>
6#include <cassert>
7#include "shmname.h"
8#include "shm_array.h"
10#include <boost/interprocess/offset_ptr.hpp>
11
12namespace vistle {
13
14template<class T>
16public:
18 //: m_name(Shm::the().createArrayId())
19 : m_name("")
20 //, m_p(shm<T>::construct(m_name)(Shm::the().allocator()))
21 , m_p(nullptr)
22 {
23 ref();
24 }
25
26 shm_array_ref(const std::string &name, T *p): m_name(name), m_p(p) { ref(); }
27
28 shm_array_ref(const shm_array_ref &other): m_name(other.m_name), m_p(other.m_p)
29 {
30 if (m_p) {
31 assert(other->refcount() > 0);
32 }
33 ref();
34 }
35
37 : m_name(name), m_p(name.empty() ? nullptr : shm<T>::find_and_ref(name))
38 {
39 assert(!m_p || refcount() > 0);
40 }
41
42 explicit shm_array_ref(const std::vector<typename T::value_type> &data);
43 explicit shm_array_ref(
44 const std::vector<typename T::value_type, vistle::default_init_allocator<typename T::value_type>> &data);
45 shm_array_ref(const typename T::value_type *data, size_t size);
46
48 {
49 if (m_p) {
50 assert(refcount() > 0);
51 }
52 unref();
53 }
54
55 void reset()
56 {
57 if (m_p) {
58 assert(refcount() > 0);
59 }
60 unref();
61 m_p = nullptr;
62 m_name = std::string();
63 }
64
65 template<typename... Args>
66 static shm_array_ref create(const Args &...args)
67 {
68 shm_array_ref result;
69 result.construct(args...);
70 return result;
71 }
72
73 bool find()
74 {
75 assert(!m_name.empty());
76 if (!m_p) {
77 m_p = shm<T>::find_and_ref(m_name);
78 }
79 if (m_p) {
80 assert(refcount() > 0);
81 }
82 return valid();
83 }
84
85 template<typename... Args>
86 void construct(const Args &...args)
87 {
88 assert(!valid());
89 unref();
90 if (m_name.empty())
91 m_name = Shm::the().createArrayId();
92 assert(!m_name.empty());
93 m_p = shm<T>::construct(m_name)(args..., Shm::the().allocator());
94 ref();
95 assert(m_p->refcount() == 1);
96 Shm::the().addArray(m_name, &*m_p);
97 }
98
100 {
101 if (&rhs != this) {
102 unref();
103 m_name = rhs.m_name;
104 m_p = rhs.m_p;
105 ref();
106 }
107 return *this;
108 }
109
110 bool valid() const { return !!m_p; }
111
112 operator bool() const { return valid(); }
113
114 T &operator*();
115 const T &operator*() const;
116
117 T *operator->();
118 const T *operator->() const;
119
120 const shm_name_t &name() const;
121 int refcount() const;
122
123 void ref()
124 {
125 if (m_p) {
126 assert(!m_name.empty());
127 assert(m_p->refcount() >= 0);
128 m_p->ref();
129 }
130 }
131
132 void unref()
133 {
134 if (m_p) {
135 assert(!m_name.empty());
136 assert(m_p->refcount() > 0);
137 //std::cerr << "shm_array_ref: giving up reference to " << m_name << ", refcount=" << m_p->refcount() << std::endl;
138 if (m_p->unref() == 0) {
140 m_p = nullptr;
141 }
142 }
143 }
144
145private:
146 shm_name_t m_name;
147#ifdef NO_SHMEM
148 T *m_p;
149#else
150 boost::interprocess::offset_ptr<T> m_p;
151#endif
153
154 template<class Archive>
155 void save(Archive &ar) const;
156 template<class Archive>
157 void load(Archive &ar);
158};
159
160
161#ifdef USE_BOOST_ARCHIVE
162#ifdef USE_YAS
163#define V_DECLARE_SHMREF(T) \
164 extern template class V_COREEXPORT shm_array_ref<shm_array<T, typename shm<T>::allocator>>; \
165 extern template void V_COREEXPORT \
166 shm_array_ref<shm_array<T, typename shm<T>::allocator>>::load<vistle::yas_iarchive>(vistle::yas_iarchive & \
167 ar); \
168 extern template void V_COREEXPORT \
169 shm_array_ref<shm_array<T, typename shm<T>::allocator>>::load<vistle::boost_iarchive>(vistle::boost_iarchive & \
170 ar); \
171 extern template void V_COREEXPORT \
172 shm_array_ref<shm_array<T, typename shm<T>::allocator>>::save<vistle::yas_oarchive>(vistle::yas_oarchive & ar) \
173 const; \
174 extern template void V_COREEXPORT \
175 shm_array_ref<shm_array<T, typename shm<T>::allocator>>::save<vistle::boost_oarchive>(vistle::boost_oarchive & \
176 ar) const;
177
178#define V_DEFINE_SHMREF(T) \
179 template class shm_array_ref<shm_array<T, typename shm<T>::allocator>>; \
180 template void shm_array_ref<shm_array<T, typename shm<T>::allocator>>::load<vistle::yas_iarchive>( \
181 vistle::yas_iarchive & ar); \
182 template void shm_array_ref<shm_array<T, typename shm<T>::allocator>>::load<vistle::boost_iarchive>( \
183 vistle::boost_iarchive & ar); \
184 template void shm_array_ref<shm_array<T, typename shm<T>::allocator>>::save<vistle::yas_oarchive>( \
185 vistle::yas_oarchive & ar) const; \
186 template void shm_array_ref<shm_array<T, typename shm<T>::allocator>>::save<vistle::boost_oarchive>( \
187 vistle::boost_oarchive & ar) const;
188#else
189#define V_DECLARE_SHMREF(T) \
190 extern template class V_COREEXPORT shm_array_ref<shm_array<T, typename shm<T>::allocator>>; \
191 extern template void V_COREEXPORT \
192 shm_array_ref<shm_array<T, typename shm<T>::allocator>>::load<vistle::boost_iarchive>(vistle::boost_iarchive & \
193 ar); \
194 extern template void V_COREEXPORT \
195 shm_array_ref<shm_array<T, typename shm<T>::allocator>>::save<vistle::boost_oarchive>(vistle::boost_oarchive & \
196 ar) const;
197
198#define V_DEFINE_SHMREF(T) \
199 template class shm_array_ref<shm_array<T, typename shm<T>::allocator>>; \
200 template void shm_array_ref<shm_array<T, typename shm<T>::allocator>>::load<vistle::boost_iarchive>( \
201 vistle::boost_iarchive & ar); \
202 template void shm_array_ref<shm_array<T, typename shm<T>::allocator>>::save<vistle::boost_oarchive>( \
203 vistle::boost_oarchive & ar) const;
204#endif
205#else
206#define V_DECLARE_SHMREF(T) \
207 extern template class V_COREEXPORT shm_array_ref<shm_array<T, typename shm<T>::allocator>>; \
208 extern template void V_COREEXPORT \
209 shm_array_ref<shm_array<T, typename shm<T>::allocator>>::load<vistle::yas_iarchive>(vistle::yas_iarchive & \
210 ar); \
211 extern template void V_COREEXPORT \
212 shm_array_ref<shm_array<T, typename shm<T>::allocator>>::save<vistle::yas_oarchive>(vistle::yas_oarchive & ar) \
213 const;
214
215#define V_DEFINE_SHMREF(T) \
216 template class shm_array_ref<shm_array<T, typename shm<T>::allocator>>; \
217 template void shm_array_ref<shm_array<T, typename shm<T>::allocator>>::load<vistle::yas_iarchive>( \
218 vistle::yas_iarchive & ar); \
219 template void shm_array_ref<shm_array<T, typename shm<T>::allocator>>::save<vistle::yas_oarchive>( \
220 vistle::yas_oarchive & ar) const;
221#endif
222
223V_DECLARE_SHMREF(char) // for MessagePayload
224V_DECLARE_SHMREF(signed char)
225V_DECLARE_SHMREF(unsigned char)
226V_DECLARE_SHMREF(int32_t)
227V_DECLARE_SHMREF(uint32_t)
228V_DECLARE_SHMREF(int64_t)
229V_DECLARE_SHMREF(uint64_t)
230V_DECLARE_SHMREF(float)
231V_DECLARE_SHMREF(double)
232
233
234} // namespace vistle
235#endif
#define ARCHIVE_ACCESS_SPLIT
Definition: archives_config.h:502
std::string createArrayId(const std::string &name="")
Definition: shm.cpp:460
const void_allocator & allocator() const
Definition: shm.cpp:310
void addArray(const std::string &name, const ShmData *array)
Definition: shm.cpp:510
static Shm & the()
Definition: shm.cpp:315
Definition: allocator.h:12
Definition: shm_reference.h:15
bool valid() const
Definition: shm_reference.h:110
T * operator->()
Definition: shm_reference_impl.h:101
T & operator*()
Definition: shm_reference_impl.h:89
shm_array_ref(const shm_array_ref &other)
Definition: shm_reference.h:28
const shm_name_t & name() const
Definition: shm_reference_impl.h:129
bool find()
Definition: shm_reference.h:73
~shm_array_ref()
Definition: shm_reference.h:47
shm_array_ref()
Definition: shm_reference.h:17
const shm_array_ref & operator=(const shm_array_ref &rhs)
Definition: shm_reference.h:99
void ref()
Definition: shm_reference.h:123
shm_array_ref(const shm_name_t name)
Definition: shm_reference.h:36
void construct(const Args &...args)
Definition: shm_reference.h:86
void unref()
Definition: shm_reference.h:132
int refcount() const
Definition: shm_reference_impl.h:121
static shm_array_ref create(const Args &...args)
Definition: shm_reference.h:66
void reset()
Definition: shm_reference.h:55
shm_array_ref(const std::string &name, T *p)
Definition: shm_reference.h:26
Definition: allobjects.cpp:30
#define V_DECLARE_SHMREF(T)
Definition: shm_reference.h:206
Definition: shmname.h:11
bool empty() const
Definition: shmname.cpp:48
Definition: shm.h:94
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 managed_shm::segment_manager::template construct_proxy< T >::type construct(const std::string &name)
Definition: shm_impl.h:87