Skip to content

Commit

Permalink
Making sure resource partitioner is not accessed if its not valid
Browse files Browse the repository at this point in the history
  • Loading branch information
hkaiser committed Feb 26, 2018
1 parent a3faea3 commit 549be5a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
1 change: 1 addition & 0 deletions hpx/runtime/resource/partitioner_fwd.hpp
Expand Up @@ -34,6 +34,7 @@ namespace hpx
// May be used anywhere in code and returns a reference to the single,
// global resource partitioner.
HPX_EXPORT detail::partitioner& get_partitioner();
HPX_EXPORT bool is_partitioner_valid();

// resource_partitioner mode
enum partitioner_mode
Expand Down
39 changes: 28 additions & 11 deletions src/runtime.cpp
Expand Up @@ -777,6 +777,10 @@ namespace hpx
{
return get_runtime().get_config().get_entry(key, dflt);
}
if (!resource::is_partitioner_valid())
{
return dflt;
}
return resource::get_partitioner()
.get_command_line_switches().rtcfg_.get_entry(key, dflt);
}
Expand All @@ -787,6 +791,10 @@ namespace hpx
{
return get_runtime().get_config().get_entry(key, dflt);
}
if (!resource::is_partitioner_valid())
{
return std::to_string(dflt);
}
return resource::get_partitioner()
.get_command_line_switches().rtcfg_.get_entry(key, dflt);
}
Expand All @@ -796,22 +804,28 @@ namespace hpx
{
if (get_runtime_ptr() != nullptr)
{
return get_runtime_ptr()->get_config().add_entry(key, value);
get_runtime_ptr()->get_config().add_entry(key, value);
}
if (resource::is_partitioner_valid())
{
resource::get_partitioner()
.get_command_line_switches().rtcfg_.add_entry(key, value);
}
return resource::get_partitioner()
.get_command_line_switches().rtcfg_.add_entry(key, value);
}

void set_config_entry(std::string const& key, std::size_t value)
{
if (get_runtime_ptr() != nullptr)
{
return get_runtime_ptr()->get_config().add_entry(
get_runtime_ptr()->get_config().add_entry(
key, std::to_string(value));
}
return resource::get_partitioner()
.get_command_line_switches().rtcfg_.
add_entry(key, std::to_string(value));
if (resource::is_partitioner_valid())
{
resource::get_partitioner()
.get_command_line_switches().rtcfg_.
add_entry(key, std::to_string(value));
}
}

void set_config_entry_callback(std::string const& key,
Expand All @@ -820,12 +834,15 @@ namespace hpx
{
if (get_runtime_ptr() != nullptr)
{
return get_runtime_ptr()->get_config().add_notification_callback(
get_runtime_ptr()->get_config().add_notification_callback(
key, callback);
}
return resource::get_partitioner()
.get_command_line_switches()
.rtcfg_.add_notification_callback(key, callback);
if (resource::is_partitioner_valid())
{
return resource::get_partitioner()
.get_command_line_switches()
.rtcfg_.add_notification_callback(key, callback);
}
}

///////////////////////////////////////////////////////////////////////////
Expand Down
15 changes: 15 additions & 0 deletions src/runtime/resource/partitioner.cpp
Expand Up @@ -154,6 +154,16 @@ namespace hpx { namespace resource
{
std::unique_ptr<detail::partitioner>& rp = detail::get_partitioner();

if (!rp)
{
// if the resource partitioner is not accessed for the first time
// if the command-line parsing has not yet been done
throw std::invalid_argument(
"hpx::resource::get_partitioner() can be called only after "
"the resource partitioner has been initialized and before it "
"has been deleted");
}

if (!rp->cmd_line_parsed())
{
if (get_runtime_ptr() != nullptr)
Expand All @@ -177,6 +187,11 @@ namespace hpx { namespace resource
return *rp;
}

bool is_partitioner_valid()
{
return bool(detail::get_partitioner());
}

namespace detail
{
detail::partitioner &create_partitioner(
Expand Down

0 comments on commit 549be5a

Please sign in to comment.