Skip to content

Commit

Permalink
Fixed #952: register_[pre_]shutdown_function never throw
Browse files Browse the repository at this point in the history
  • Loading branch information
hkaiser committed Oct 21, 2013
1 parent 077db90 commit 38d1972
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 11 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ else()
##############################################################################
if(HPX_HAVE_CXX11)
# Turn on C++11 support
hpx_use_flag_if_available(-std=c++0x)
hpx_use_flag_if_available(-std=c++0x CXX)

hpx_check_for_cxx11_rvalue_references(HPX_HAVE_CXX11_RVALUE_REFERENCES
DEFINITIONS HPX_HAVE_CXX11_RVALUE_REFERENCES)
Expand Down Expand Up @@ -1259,7 +1259,7 @@ else()
# These come from within Boost. Detection for this flag fails with GCC 4.4,
# 4.5 and 4.6.
if(040700 LESS ${GCC_VERSION} OR 040700 EQUAL ${GCC_VERSION})
hpx_use_flag_if_available(-Wno-delete-non-virtual-dtor)
hpx_use_flag_if_available(-Wno-delete-non-virtual-dtor CXX C)
endif()

# Check if our libraries have unresolved symbols
Expand Down
6 changes: 2 additions & 4 deletions hpx/hpx_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1075,8 +1075,7 @@ namespace hpx
/// \param f [in] The function to be registered to run by an HPX thread as
/// a pre-shutdown function.
///
/// \note If this function is called before the runtime system is
/// initialized, or while the pre-shutdown functions are
/// \note If this function is called while the pre-shutdown functions are
/// being executed, or after that point, it will raise a invalid_status
/// exception.
///
Expand All @@ -1095,8 +1094,7 @@ namespace hpx
/// \param f [in] The function to be registered to run by an HPX thread as
/// a shutdown function.
///
/// \note If this function is called before the runtime system is
/// initialized, or while the shutdown functions are
/// \note If this function is called while the shutdown functions are
/// being executed, or after that point, it will raise a invalid_status
/// exception.
///
Expand Down
6 changes: 4 additions & 2 deletions hpx/runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ namespace hpx
state_startup = 2,
state_pre_main = 3,
state_running = 4,
state_stopped = 5
state_pre_shutdown = 5,
state_shutdown = 6,
state_stopped = 7
};

state get_state() const { return state_; }
Expand Down Expand Up @@ -346,7 +348,7 @@ namespace hpx
void init_tss();
void deinit_tss();

friend bool hpx::pre_main(runtime_mode);
public:
void set_state(state s) { state_ = s; }

protected:
Expand Down
1 change: 1 addition & 0 deletions src/hpx_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,7 @@ namespace hpx

p->call_shutdown_functions(true);
p->call_shutdown_functions(false);

p->stop(shutdown_timeout, naming::invalid_id, true);

return 0;
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/components/server/runtime_support_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,12 +683,14 @@ namespace hpx { namespace components { namespace server
void runtime_support::call_shutdown_functions(bool pre_shutdown)
{
if (pre_shutdown) {
get_runtime().set_state(runtime::state_pre_shutdown);
BOOST_FOREACH(HPX_STD_FUNCTION<void()> const& f, pre_shutdown_functions_)
{
f();
}
}
else {
get_runtime().set_state(runtime::state_shutdown);
BOOST_FOREACH(HPX_STD_FUNCTION<void()> const& f, shutdown_functions_)
{
f();
Expand Down
41 changes: 38 additions & 3 deletions src/runtime_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ namespace hpx {
std::list<HPX_STD_FUNCTION<void()> > global_pre_startup_functions;
std::list<HPX_STD_FUNCTION<void()> > global_startup_functions;

std::list<HPX_STD_FUNCTION<void()> > global_pre_shutdown_functions;
std::list<HPX_STD_FUNCTION<void()> > global_shutdown_functions;

///////////////////////////////////////////////////////////////////////////
void register_startup_function(startup_function_type const& f)
{
Expand All @@ -72,7 +75,7 @@ namespace hpx {
if (NULL != rt) {
if (rt->get_state() >= runtime::state_pre_startup) {
HPX_THROW_EXCEPTION(invalid_status,
"register_startup_function",
"register_pre_startup_function",
"Too late to register a new pre-startup function.");
return;
}
Expand All @@ -86,15 +89,35 @@ namespace hpx {
void register_pre_shutdown_function(shutdown_function_type const& f)
{
runtime* rt = get_runtime_ptr();
if (NULL != rt)
if (NULL != rt) {
if (rt->get_state() >= runtime::state_pre_shutdown) {
HPX_THROW_EXCEPTION(invalid_status,
"register_pre_shutdown_function",
"Too late to register a new pre-shutdown function.");
return;
}
rt->add_pre_shutdown_function(f);
}
else {
global_pre_shutdown_functions.push_back(f);
}
}

void register_shutdown_function(shutdown_function_type const& f)
{
runtime* rt = get_runtime_ptr();
if (NULL != rt)
if (NULL != rt) {
if (rt->get_state() >= runtime::state_shutdown) {
HPX_THROW_EXCEPTION(invalid_status,
"register_shutdown_function",
"Too late to register a new shutdown function.");
return;
}
rt->add_shutdown_function(f);
}
else {
global_shutdown_functions.push_back(f);
}
}

///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -167,6 +190,18 @@ namespace hpx {
}
global_startup_functions.clear();

BOOST_FOREACH(HPX_STD_FUNCTION<void()> const& f, global_pre_shutdown_functions)
{
add_pre_shutdown_function(f);
}
global_startup_functions.clear();

BOOST_FOREACH(HPX_STD_FUNCTION<void()> const& f, global_shutdown_functions)
{
add_shutdown_function(f);
}
global_startup_functions.clear();

// set state to initialized
set_state(state_initialized);
}
Expand Down

0 comments on commit 38d1972

Please sign in to comment.