Skip to content

Commit

Permalink
Converted lcos::channel to be a simple_component
Browse files Browse the repository at this point in the history
- added tests verifying that lcos::channel can be used as an LCO
  • Loading branch information
hkaiser committed Jul 11, 2017
1 parent ae34321 commit 4fad8c7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 24 deletions.
42 changes: 26 additions & 16 deletions hpx/lcos/server/channel.hpp
@@ -1,4 +1,4 @@
// Copyright (c) 2016 Hartmut Kaiser
// Copyright (c) 2016-2017 Hartmut Kaiser
//
// 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)
Expand All @@ -11,15 +11,18 @@
#include <hpx/lcos/local/channel.hpp>
#include <hpx/runtime/actions/component_action.hpp>
#include <hpx/runtime/components/component_type.hpp>
#include <hpx/runtime/components/server/managed_component_base.hpp>
#include <hpx/runtime/components/server/component_base.hpp>
#include <hpx/traits/get_remote_result.hpp>
#include <hpx/traits/is_component.hpp>
#include <hpx/traits/promise_remote_result.hpp>

#include <cstddef>
#include <exception>
#include <type_traits>
#include <utility>

#include <boost/preprocessor/cat.hpp>

///////////////////////////////////////////////////////////////////////////////
namespace hpx { namespace lcos { namespace server
{
Expand All @@ -31,14 +34,17 @@ namespace hpx { namespace lcos { namespace server
///////////////////////////////////////////////////////////////////////////
template <typename T, typename RemoteType>
class channel
: public lcos::base_lco_with_value<T, RemoteType>
, public components::managed_component_base<channel<T, RemoteType> >
: public lcos::base_lco_with_value<
T, RemoteType, traits::detail::component_tag>
, public components::component_base<channel<T, RemoteType> >
{
public:
typedef lcos::base_lco_with_value<T, RemoteType> base_type_holder;
typedef lcos::base_lco_with_value<
T, RemoteType, traits::detail::component_tag
> base_type_holder;

private:
typedef components::managed_component_base<channel> base_type;
typedef components::component_base<channel> base_type;
typedef typename std::conditional<
std::is_void<T>::value, util::unused_type, T
>::type result_type;
Expand Down Expand Up @@ -120,20 +126,22 @@ namespace hpx { namespace lcos { namespace server
HPX_REGISTER_CHANNEL_DECLARATION_2(type, type) \
/**/
#define HPX_REGISTER_CHANNEL_DECLARATION_2(type, name) \
typedef ::hpx::lcos::server::channel<type> \
typedef ::hpx::lcos::server::channel< type> \
BOOST_PP_CAT(__channel_, BOOST_PP_CAT(type, name)); \
HPX_REGISTER_ACTION_DECLARATION( \
hpx::lcos::server::channel<type>::get_generation_action, \
hpx::lcos::server::channel< type>::get_generation_action, \
BOOST_PP_CAT(__channel_get_generation_action, \
BOOST_PP_CAT(type, name))); \
HPX_REGISTER_ACTION_DECLARATION( \
hpx::lcos::server::channel<type>::set_generation_action, \
hpx::lcos::server::channel< type>::set_generation_action, \
BOOST_PP_CAT(__channel_set_generation_action, \
BOOST_PP_CAT(type, name))); \
HPX_REGISTER_ACTION_DECLARATION( \
hpx::lcos::server::channel<type>::close_action, \
hpx::lcos::server::channel< type>::close_action, \
BOOST_PP_CAT(__channel_close_action, \
BOOST_PP_CAT(type, name))) \
BOOST_PP_CAT(type, name))); \
HPX_REGISTER_BASE_LCO_WITH_VALUE_DECLARATION( \
type, type, name, hpx::traits::detail::component_tag) \
/**/

#define HPX_REGISTER_CHANNEL(...) \
Expand All @@ -149,27 +157,29 @@ namespace hpx { namespace lcos { namespace server
HPX_REGISTER_CHANNEL_2(type, type) \
/**/
#define HPX_REGISTER_CHANNEL_2(type, name) \
typedef ::hpx::lcos::server::channel<type> \
typedef ::hpx::lcos::server::channel< type> \
BOOST_PP_CAT(__channel_, BOOST_PP_CAT(type, name)); \
typedef ::hpx::components::managed_component< \
typedef ::hpx::components::component< \
BOOST_PP_CAT(__channel_, BOOST_PP_CAT(type, name)) \
> BOOST_PP_CAT(__channel_component_, name); \
HPX_REGISTER_DERIVED_COMPONENT_FACTORY( \
BOOST_PP_CAT(__channel_component_, name), \
BOOST_PP_CAT(__channel_component_, name), \
BOOST_PP_STRINGIZE(BOOST_PP_CAT(__base_lco_with_value_channel_, name)));\
HPX_REGISTER_ACTION( \
hpx::lcos::server::channel<type>::get_generation_action, \
hpx::lcos::server::channel< type>::get_generation_action, \
BOOST_PP_CAT(__channel_get_generation_action, \
BOOST_PP_CAT(type, name))); \
HPX_REGISTER_ACTION( \
hpx::lcos::server::channel<type>::set_generation_action, \
hpx::lcos::server::channel< type>::set_generation_action, \
BOOST_PP_CAT(__channel_set_generation_action, \
BOOST_PP_CAT(type, name))); \
HPX_REGISTER_ACTION( \
hpx::lcos::server::channel<type>::close_action, \
hpx::lcos::server::channel< type>::close_action, \
BOOST_PP_CAT(__channel_close_action, \
BOOST_PP_CAT(type, name))) \
HPX_REGISTER_BASE_LCO_WITH_VALUE( \
type, type, name, hpx::traits::detail::component_tag) \
/**/

#endif
46 changes: 38 additions & 8 deletions tests/unit/lcos/channel.cpp
Expand Up @@ -63,10 +63,10 @@ void pong(
pongs.set(msg);
}

void pingpong(hpx::id_type const& loc)
void pingpong(hpx::id_type const& here, hpx::id_type const& there)
{
hpx::lcos::channel<std::string> pings(loc);
hpx::lcos::channel<std::string> pongs(loc);
hpx::lcos::channel<std::string> pings(here);
hpx::lcos::channel<std::string> pongs(there);

ping(pings, "passed message");
pong(pings, pongs);
Expand All @@ -91,10 +91,10 @@ void pong_void(
pingponged = true;
}

void pingpong_void(hpx::id_type const& loc)
void pingpong_void(hpx::id_type const& here, hpx::id_type const& there)
{
hpx::lcos::channel<> pings(loc);
hpx::lcos::channel<> pongs(loc);
hpx::lcos::channel<> pings(here);
hpx::lcos::channel<> pongs(there);

bool pingponged = false;

Expand Down Expand Up @@ -263,14 +263,30 @@ void closed_channel_set(hpx::id_type const& loc)
HPX_TEST(caught_exception);
}

///////////////////////////////////////////////////////////////////////////////
int return42()
{
return 42;
}
HPX_PLAIN_ACTION(return42);

void channel_as_lco(hpx::id_type const& here, hpx::id_type const& there)
{
hpx::lcos::channel<int> lco(here);

hpx::apply_c(return42_action(), lco.get_id(), there);

HPX_TEST_EQ(lco.get(hpx::launch::sync), 42);
}

///////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
hpx::id_type here = hpx::find_here();

calculate_sum(here);
pingpong(here);
pingpong_void(here);
pingpong(here, here);
pingpong_void(here, here);
dispatch_work(here);
channel_range(here);
channel_range_void(here);
Expand All @@ -280,5 +296,19 @@ int main(int argc, char* argv[])
closed_channel_get_generation(here);
closed_channel_set(here);

channel_as_lco(here, here);

std::vector<hpx::id_type> remote_localities = hpx::find_remote_localities();
for (hpx::id_type id : remote_localities)
{
pingpong(id, here);
pingpong(here, id);
pingpong_void(id, here);
pingpong_void(here, id);

channel_as_lco(id, here);
channel_as_lco(here, id);
}

return hpx::util::report_errors();
}

0 comments on commit 4fad8c7

Please sign in to comment.