-
-
Notifications
You must be signed in to change notification settings - Fork 423
/
simple_component_base.hpp
323 lines (276 loc) · 10.8 KB
/
simple_component_base.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright (c) 2007-2017 Hartmut Kaiser
// Copyright (c) 2011 Bryce Lelbach
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#if !defined(HPX_COMPONENTS_SIMPLE_COMPONENT_BASE_JUL_18_2008_0948PM)
#define HPX_COMPONENTS_SIMPLE_COMPONENT_BASE_JUL_18_2008_0948PM
#include <hpx/config.hpp>
#include <hpx/runtime/agas/interface.hpp>
#include <hpx/runtime/applier/applier.hpp>
#include <hpx/runtime/applier/bind_naming_wrappers.hpp>
#include <hpx/runtime/applier_fwd.hpp>
#include <hpx/runtime/components/component_type.hpp>
#include <hpx/runtime/components/server/create_component_fwd.hpp>
#include <hpx/runtime/naming/address.hpp>
#include <hpx/runtime/naming/name.hpp>
#include <hpx/runtime_fwd.hpp>
#include <hpx/throw_exception.hpp>
#include <hpx/traits/is_component.hpp>
#include <hpx/util/unique_function.hpp>
#include <cstddef>
#include <cstdint>
#include <mutex>
#include <sstream>
#include <type_traits>
#include <utility>
namespace hpx { namespace detail
{
HPX_API_EXPORT naming::gid_type get_next_id(std::size_t count = 1);
}}
namespace hpx { namespace components
{
template <typename Component>
class simple_component;
///////////////////////////////////////////////////////////////////////////
template <typename Component>
class simple_component_base : public traits::detail::simple_component_tag
{
protected:
typedef typename std::conditional<
std::is_same<Component, detail::this_type>::value,
simple_component_base, Component
>::type this_component_type;
Component& derived()
{
return static_cast<Component&>(*this);
}
Component const& derived() const
{
return static_cast<Component const&>(*this);
}
public:
typedef this_component_type wrapped_type;
typedef this_component_type base_type_holder;
typedef simple_component<this_component_type> wrapping_type;
/// \brief Construct an empty simple_component
simple_component_base()
{}
/// \brief Destruct a simple_component
~simple_component_base()
{
if (gid_)
{
error_code ec;
agas::unbind(launch::sync, gid_, 1, ec);
}
}
// Copy construction and copy assignment should not copy the gid_.
simple_component_base(simple_component_base const&) {}
simple_component_base& operator=(simple_component_base const&)
{
return *this;
}
// just move our gid_
simple_component_base(simple_component_base && rhs)
: gid_(std::move(rhs.gid_))
{}
simple_component_base& operator=(simple_component_base && rhs)
{
if (this != &rhs)
gid_ = std::move(rhs.gid_);
return *this;
}
/// \brief finalize() will be called just before the instance gets
/// destructed
void finalize() {}
// This exposes the component type.
static component_type get_component_type()
{
return components::get_component_type<this_component_type>();
}
static void set_component_type(component_type type)
{
components::set_component_type<this_component_type>(type);
}
naming::address get_current_address() const
{
return naming::address(get_locality(),
components::get_component_type<wrapped_type>(),
std::uint64_t(static_cast<this_component_type const*>(this)));
}
#if defined(HPX_HAVE_CXX11_EXTENDED_FRIEND_DECLARATIONS) && !defined(__NVCC__) && \
!defined(__CUDACC__)
protected:
// declare friends which are allowed to access get_base_gid()
template <typename Component_>
friend naming::gid_type server::create(std::size_t count);
template <typename Component_>
friend naming::gid_type server::create(
util::unique_function_nonser<void(void*)> const& ctor);
template <typename Component_>
friend naming::gid_type server::create(naming::gid_type const& gid,
util::unique_function_nonser<void(void*)> const& ctor, void** p);
template <typename Component_, typename ...Ts>
friend naming::gid_type server::create_with_args(Ts&&... ts);
#endif
// Create a new GID (if called for the first time), assign this
// GID to this instance of a component and register this gid
// with the AGAS service
//
// Returns he global id (GID) assigned to this instance of a component
naming::gid_type get_base_gid(
naming::gid_type const& assign_gid = naming::invalid_gid) const
{
if (!gid_)
{
naming::address addr(get_current_address());
if (!assign_gid)
{
gid_ = hpx::detail::get_next_id();
if (!applier::bind_gid_local(gid_, addr))
{
std::ostringstream strm;
strm << "failed to bind id " << gid_
<< "to locality: " << hpx::get_locality();
gid_ = naming::invalid_gid; // invalidate GID
HPX_THROW_EXCEPTION(duplicate_component_address,
"simple_component_base<Component>::get_base_gid",
strm.str());
}
}
else
{
applier::applier& appl = hpx::applier::get_applier();
gid_ = assign_gid;
naming::detail::strip_credits_from_gid(gid_);
if (!agas::bind(launch::sync, gid_, addr,
appl.get_locality_id()))
{
std::ostringstream strm;
strm << "failed to rebind id " << gid_
<< "to locality: " << hpx::get_locality();
gid_ = naming::invalid_gid; // invalidate GID
HPX_THROW_EXCEPTION(duplicate_component_address,
"simple_component_base<Component>::get_base_gid",
strm.str());
}
}
}
std::unique_lock<naming::gid_type::mutex_type> l(gid_.get_mutex());
if (!naming::detail::has_credits(gid_))
{
naming::gid_type gid = gid_;
return gid;
}
// on first invocation take all credits to avoid a self reference
naming::gid_type gid = gid_;
naming::detail::strip_credits_from_gid(
const_cast<naming::gid_type&>(gid_));
HPX_ASSERT(naming::detail::has_credits(gid));
// We have to assume this credit was split as otherwise the gid
// returned at this point will control the lifetime of the
// component.
naming::detail::set_credit_split_mask_for_gid(gid);
return gid;
}
public:
naming::id_type get_id() const
{
// all credits should have been taken already
naming::gid_type gid = derived().get_base_gid();
HPX_ASSERT(!naming::detail::has_credits(gid));
// any (subsequent) invocation causes the credits to be replenished
naming::detail::replenish_credits(gid);
return naming::id_type(gid, naming::id_type::managed);
}
naming::id_type get_unmanaged_id() const
{
return naming::id_type(derived().get_base_gid(),
naming::id_type::managed);
}
#if defined(HPX_HAVE_COMPONENT_GET_GID_COMPATIBILITY)
HPX_DEPRECATED(HPX_DEPRECATED_MSG)
naming::id_type get_gid() const
{
return get_id();
}
#endif
// Pinning functionality
void pin() {}
void unpin() {}
std::uint32_t pin_count() const { return 0; }
void mark_as_migrated()
{
// If this assertion is triggered then this component instance is
// being migrated even if the component type has not been enabled
// to support migration.
HPX_ASSERT(false);
}
void on_migrated()
{
// If this assertion is triggered then this component instance is being
// migrated even if the component type has not been enabled to support
// migration.
HPX_ASSERT(false);
}
protected:
mutable naming::gid_type gid_;
};
namespace detail
{
///////////////////////////////////////////////////////////////////////
template <typename Component>
struct simple_heap_factory
{
static void* alloc(std::size_t count)
{
HPX_ASSERT(1 == count);
return ::operator new(sizeof(Component));
}
static void free(void* p, std::size_t count)
{
HPX_ASSERT(1 == count);
::operator delete(p);
}
};
}
///////////////////////////////////////////////////////////////////////////
template <typename Component>
class simple_component : public Component
{
public:
typedef Component type_holder;
typedef simple_component<Component> component_type;
typedef component_type derived_type;
typedef detail::simple_heap_factory<component_type> heap_type;
/// \brief Construct a simple_component instance holding a new wrapped
/// instance
template <typename ...Ts>
simple_component(Ts&&... vs)
: Component(std::forward<Ts>(vs)...)
{}
/// \brief The function \a create is used for allocation and
/// initialization of instances of the derived components.
template <typename T = Component>
static typename std::enable_if<
std::is_default_constructible<T>::value, component_type*
>::type
create(std::size_t count)
{
// simple components can be created individually only
HPX_ASSERT(1 == count);
return static_cast<component_type*>(new Component()); //-V572
}
/// \brief The function \a destroy is used for destruction and
/// de-allocation of instances of the derived components.
static void destroy(Component* p, std::size_t count = 1)
{
// simple components can be deleted individually only
HPX_ASSERT(1 == count);
p->finalize();
delete p;
}
};
}}
#endif