| Line | % of fetches | Source |
|---|---|---|
| 1 | //////////////////////////////////////////////////////////////////////////////// | |
| 2 | // Copyright (c) 2016 Thomas Heller | |
| 3 | // | |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
| 6 | //////////////////////////////////////////////////////////////////////////////// | |
| 7 | ||
| 8 | #include <hpx/config.hpp> | |
| 9 | #include <hpx/apply.hpp> | |
| 10 | #include <hpx/throw_exception.hpp> | |
| 11 | #include <hpx/lcos/base_lco_with_value.hpp> | |
| 12 | #include <hpx/runtime/actions/continuation.hpp> | |
| 13 | #include <hpx/runtime/agas/primary_namespace.hpp> | |
| 14 | #include <hpx/runtime/agas/server/primary_namespace.hpp> | |
| 15 | #include <hpx/runtime/applier/apply_callback.hpp> | |
| 16 | #include <hpx/runtime/components/component_factory.hpp> | |
| 17 | #include <hpx/runtime/serialization/vector.hpp> | |
| 18 | ||
| 19 | #include <boost/format.hpp> | |
| 20 | ||
| 21 | #include <cstdint> | |
| 22 | #include <string> | |
| 23 | #include <utility> | |
| 24 | ||
| 25 | using hpx::components::component_agas_primary_namespace; | |
| 26 | ||
| 27 | using hpx::agas::server::primary_namespace; | |
| 28 | ||
| 29 | HPX_REGISTER_COMPONENT( | |
| 30 | hpx::components::fixed_component<primary_namespace>, | |
| 31 | primary_namespace, hpx::components::factory_enabled) | |
| 32 | HPX_DEFINE_GET_COMPONENT_TYPE_STATIC( | |
| 33 | primary_namespace, component_agas_primary_namespace) | |
| 34 | ||
| 35 | HPX_REGISTER_ACTION_ID( | |
| 36 | primary_namespace::allocate_action, | |
| 37 | primary_namespace_allocate_action, | |
| 38 | hpx::actions::primary_namespace_allocate_action_id) | |
| 39 | ||
| 40 | HPX_REGISTER_ACTION_ID( | |
| 41 | primary_namespace::bind_gid_action, | |
| 42 | primary_namespace_bind_gid_action, | |
| 43 | hpx::actions::primary_namespace_bind_gid_action_id) | |
| 44 | ||
| 45 | HPX_REGISTER_ACTION_ID( | |
| 46 | primary_namespace::begin_migration_action, | |
| 47 | primary_namespace_begin_migration_action, | |
| 48 | hpx::actions::primary_namespace_begin_migration_action_id) | |
| 49 | ||
| 50 | HPX_REGISTER_ACTION_ID( | |
| 51 | primary_namespace::end_migration_action, | |
| 52 | primary_namespace_end_migration_action, | |
| 53 | hpx::actions::primary_namespace_end_migration_action_id) | |
| 54 | ||
| 55 | HPX_REGISTER_ACTION_ID( | |
| 56 | primary_namespace::decrement_credit_action, | |
| 57 | primary_namespace_decrement_credit_action, | |
| 58 | hpx::actions::primary_namespace_decrement_credit_action_id) | |
| 59 | ||
| 60 | HPX_REGISTER_ACTION_ID( | |
| 61 | primary_namespace::increment_credit_action, | |
| 62 | primary_namespace_increment_credit_action, | |
| 63 | hpx::actions::primary_namespace_increment_credit_action_id) | |
| 64 | ||
| 65 | HPX_REGISTER_ACTION_ID( | |
| 66 | primary_namespace::resolve_gid_action, | |
| 67 | primary_namespace_resolve_gid_action, | |
| 68 | hpx::actions::primary_namespace_resolve_gid_action_id) | |
| 69 | ||
| 70 | HPX_REGISTER_ACTION_ID( | |
| 71 | primary_namespace::colocate_action, | |
| 72 | primary_namespace_colocate_action, | |
| 73 | hpx::actions::primary_namespace_colocate_action_id) | |
| 74 | ||
| 75 | HPX_REGISTER_ACTION_ID( | |
| 76 | primary_namespace::unbind_gid_action, | |
| 77 | primary_namespace_unbind_gid_action, | |
| 78 | hpx::actions::primary_namespace_unbind_gid_action_id) | |
| 79 | ||
| 80 | HPX_REGISTER_ACTION_ID( | |
| 81 | primary_namespace::route_action, | |
| 82 | primary_namespace_route_action, | |
| 83 | hpx::actions::primary_namespace_route_action_id) | |
| 84 | ||
| 85 | HPX_REGISTER_ACTION_ID( | |
| 86 | primary_namespace::statistics_counter_action, | |
| 87 | primary_namespace_statistics_counter_action, | |
| 88 | hpx::actions::primary_namespace_statistics_counter_action_id) | |
| 89 | ||
| 90 | namespace hpx { namespace agas { | |
| 91 | ||
| 92 | naming::gid_type primary_namespace::get_service_instance( | |
| 93 | std::uint32_t service_locality_id) | |
| 94 | { | |
| 95 | naming::gid_type service(HPX_AGAS_PRIMARY_NS_MSB, HPX_AGAS_PRIMARY_NS_LSB); | |
| 96 | return naming::replace_locality_id(service, service_locality_id); | |
| 97 | } | |
| 98 | ||
| 99 | naming::gid_type primary_namespace::get_service_instance(naming::gid_type const& dest, | |
| 100 | error_code& ec) | |
| 101 | { | |
| 102 | std::uint32_t service_locality_id = naming::get_locality_id_from_gid(dest); | |
| 103 | if (service_locality_id == naming::invalid_locality_id) | |
| 104 | { | |
| 105 | HPX_THROWS_IF(ec, bad_parameter, | |
| 106 | "primary_namespace::get_service_instance", | |
| 107 | boost::str(boost::format( | |
| 108 | "can't retrieve a valid locality id from global address (%1%): " | |
| 109 | ) % dest)); | |
| 110 | return naming::gid_type(); | |
| 111 | } | |
| 112 | return get_service_instance(service_locality_id); | |
| 113 | } | |
| 114 | ||
| 115 | bool primary_namespace::is_service_instance(naming::gid_type const& gid) | |
| 116 | { | |
| 117 | return gid.get_lsb() == HPX_AGAS_PRIMARY_NS_LSB && | |
| 118 | (gid.get_msb() & ~naming::gid_type::locality_id_mask) | |
| 119 | == HPX_AGAS_PRIMARY_NS_MSB; | |
| 120 | } | |
| 121 | ||
| 122 | primary_namespace::primary_namespace() | |
| 123 | : server_(new server::primary_namespace()) | |
| 124 | {} | |
| 125 | ||
| 126 | primary_namespace::~primary_namespace() | |
| 127 | {} | |
| 128 | ||
| 129 | naming::address::address_type primary_namespace::ptr() const | |
| 130 | { | |
| 131 | return reinterpret_cast<naming::address::address_type>(server_.get()); | |
| 132 | } | |
| 133 | ||
| 134 | naming::address primary_namespace::addr() const | |
| 135 | { | |
| 136 | return naming::address( | |
| 137 | hpx::get_locality(), | |
| 138 | server::primary_namespace::get_component_type(), | |
| 139 | this->ptr() | |
| 140 | ); | |
| 141 | } | |
| 142 | ||
| 143 | naming::id_type primary_namespace::gid() const | |
| 144 | { | |
| 145 | return naming::id_type( | |
| 146 | get_service_instance(hpx::get_locality()), | |
| 147 | naming::id_type::unmanaged); | |
| 148 | } | |
| 149 | ||
| 150 | future<std::pair<naming::id_type, naming::address>> | |
| 151 | primary_namespace::begin_migration(naming::gid_type id) | |
| 152 | { | |
| 153 | naming::id_type dest = naming::id_type(get_service_instance(id), | |
| 154 | naming::id_type::unmanaged); | |
| 155 | if (naming::get_locality_from_gid(dest.get_gid()) == hpx::get_locality()) | |
| 156 | { | |
| 157 | return hpx::make_ready_future(server_->begin_migration(id)); | |
| 158 | } | |
| 159 | server::primary_namespace::begin_migration_action action; | |
| 160 | return hpx::async(action, std::move(dest), id); | |
| 161 | } | |
| 162 | future<bool> primary_namespace::end_migration(naming::gid_type id) | |
| 163 | { | |
| 164 | naming::id_type dest = naming::id_type(get_service_instance(id), | |
| 165 | naming::id_type::unmanaged); | |
| 166 | if (naming::get_locality_from_gid(dest.get_gid()) == hpx::get_locality()) | |
| 167 | { | |
| 168 | return hpx::make_ready_future(server_->end_migration(id)); | |
| 169 | } | |
| 170 | server::primary_namespace::end_migration_action action; | |
| 171 | return hpx::async(action, std::move(dest), id); | |
| 172 | } | |
| 173 | ||
| 174 | bool primary_namespace::bind_gid( | |
| 175 | gva g, naming::gid_type id, naming::gid_type locality) | |
| 176 | { | |
| 177 | return server_->bind_gid(g, id, locality); | |
| 178 | } | |
| 179 | ||
| 180 | future<bool> primary_namespace::bind_gid_async( | |
| 181 | gva g, naming::gid_type id, naming::gid_type locality) | |
| 182 | { | |
| 183 | naming::id_type dest = naming::id_type(get_service_instance(id), | |
| 184 | naming::id_type::unmanaged); | |
| 185 | if (naming::get_locality_from_gid(dest.get_gid()) == hpx::get_locality()) | |
| 186 | { | |
| 187 | return hpx::make_ready_future(server_->bind_gid(g, id, locality)); | |
| 188 | } | |
| 189 | server::primary_namespace::bind_gid_action action; | |
| 190 | return hpx::async(action, std::move(dest), g, id, locality); | |
| 191 | } | |
| 192 | ||
| 193 | void primary_namespace::route(parcelset::parcel && p, | |
| 194 | util::function_nonser<void(boost::system::error_code const&, | |
| 195 | parcelset::parcel const&)> && f) | |
| 196 | { | |
| 197 | // compose request | |
| 198 | naming::gid_type const& id = p.destination(); | |
| 199 | naming::id_type dest = naming::id_type(get_service_instance(id), | |
| 200 | naming::id_type::unmanaged); | |
| 201 | if (naming::get_locality_from_gid(dest.get_gid()) == hpx::get_locality()) | |
| 202 | { | |
| 203 | hpx::apply( | |
| 204 | &server::primary_namespace::route, | |
| 205 | server_.get(), | |
| 206 | std::move(p) | |
| 207 | ); | |
| 208 | f(boost::system::error_code(), parcelset::parcel()); | |
| 209 | return; | |
| 210 | } | |
| 211 | ||
| 212 | server::primary_namespace::route_action action; | |
| 213 | hpx::apply_cb(action, std::move(dest), std::move(f), std::move(p)); | |
| 214 | } | |
| 215 | ||
| 216 | primary_namespace::resolved_type | |
| 217 | primary_namespace::resolve_gid(naming::gid_type id) | |
| 218 | { | |
| 219 | return server_->resolve_gid(id); | |
| 220 | } | |
| 221 | ||
| 222 | future<primary_namespace::resolved_type> | |
| 223 | primary_namespace::resolve_full(naming::gid_type id) | |
| 224 | { | |
| 225 | naming::id_type dest = naming::id_type(get_service_instance(id), | |
| 226 | naming::id_type::unmanaged); | |
| 227 | if (naming::get_locality_from_gid(dest.get_gid()) == hpx::get_locality()) | |
| 228 | { | |
| 229 | return hpx::make_ready_future(server_->resolve_gid(id)); | |
| 230 | } | |
| 231 | server::primary_namespace::resolve_gid_action action; | |
| 232 | return hpx::async(action, std::move(dest), id); | |
| 233 | } | |
| 234 | ||
| 235 | hpx::future<id_type> primary_namespace::colocate(naming::gid_type id) | |
| 236 | { | |
| 237 | naming::id_type dest = naming::id_type(get_service_instance(id), | |
| 238 | naming::id_type::unmanaged); | |
| 239 | if (naming::get_locality_from_gid(dest.get_gid()) == hpx::get_locality()) | |
| 240 | { | |
| 241 | return hpx::make_ready_future(server_->colocate(id)); | |
| 242 | } | |
| 243 | server::primary_namespace::colocate_action action; | |
| 244 | return hpx::async(action, std::move(dest), id); | |
| 245 | } | |
| 246 | ||
| 247 | future<naming::address> | |
| 248 | primary_namespace::unbind_gid_async(std::uint64_t count, naming::gid_type id) | |
| 249 | { | |
| 250 | naming::id_type dest = naming::id_type(get_service_instance(id), | |
| 251 | naming::id_type::unmanaged); | |
| 252 | naming::gid_type stripped_id = naming::detail::get_stripped_gid(id); | |
| 253 | if (naming::get_locality_from_gid(dest.get_gid()) == hpx::get_locality()) | |
| 254 | { | |
| 255 | return hpx::make_ready_future(server_->unbind_gid(count, stripped_id)); | |
| 256 | } | |
| 257 | server::primary_namespace::unbind_gid_action action; | |
| 258 | return hpx::async(action, std::move(dest), count, stripped_id); | |
| 259 | } | |
| 260 | ||
| 261 | naming::address | |
| 262 | primary_namespace::unbind_gid(std::uint64_t count, naming::gid_type id) | |
| 263 | { | |
| 264 | naming::id_type dest = naming::id_type(get_service_instance(id), | |
| 265 | naming::id_type::unmanaged); | |
| 266 | naming::gid_type stripped_id = naming::detail::get_stripped_gid(id); | |
| 267 | if (naming::get_locality_from_gid(dest.get_gid()) == hpx::get_locality()) | |
| 268 | { | |
| 269 | return server_->unbind_gid(count, stripped_id); | |
| 270 | } | |
| 271 | server::primary_namespace::unbind_gid_action action; | |
| 272 | return action(std::move(dest), count, stripped_id); | |
| 273 | } | |
| 274 | ||
| 275 | future<std::int64_t> primary_namespace::increment_credit( | |
| 276 | std::int64_t credits | |
| 277 | , naming::gid_type lower | |
| 278 | , naming::gid_type upper | |
| 279 | ) | |
| 280 | { | |
| 281 | naming::id_type dest = naming::id_type(get_service_instance(lower), | |
| 282 | naming::id_type::unmanaged); | |
| 283 | if (naming::get_locality_from_gid(dest.get_gid()) == hpx::get_locality()) | |
| 284 | { | |
| 285 | return hpx::make_ready_future( | |
| 286 | server_->increment_credit(credits, lower, upper)); | |
| 287 | } | |
| 288 | server::primary_namespace::increment_credit_action action; | |
| 289 | return hpx::async(action, std::move(dest), credits, lower, upper); | |
| 290 | } | |
| 291 | ||
| 292 | std::pair<naming::gid_type, naming::gid_type> | |
| 293 | primary_namespace::allocate(std::uint64_t count) | |
| 294 | { | |
| 295 | return server_->allocate(count); | |
| 296 | } | |
| 297 | ||
| 298 | void primary_namespace::set_local_locality(naming::gid_type const& g) | |
| 299 | { | |
| 300 | server_->set_local_locality(g); | |
| 301 | } | |
| 302 | ||
| 303 | void primary_namespace::register_counter_types() | |
| 304 | { | |
| 305 | server::primary_namespace::register_counter_types(); | |
| 306 | server::primary_namespace::register_global_counter_types(); | |
| 307 | } | |
| 308 | ||
| 309 | void primary_namespace::register_server_instance(std::uint32_t locality_id) | |
| 310 | { | |
| 311 | std::string str("locality#" + | |
| 312 | std::to_string(locality_id) + "/"); | |
| 313 | server_->register_server_instance(str.c_str(), locality_id); | |
| 314 | } | |
| 315 | ||
| 316 | void primary_namespace::unregister_server_instance(error_code& ec) | |
| 317 | { | |
| 318 | server_->unregister_server_instance(ec); | |
| 319 | } | |
| 320 | }} | |
| 321 |
Copyright (c) 2006-2012 Rogue Wave Software, Inc. All Rights Reserved.
Patents pending.