Skip to content

Commit

Permalink
Adding user supplied on_finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
hkaiser committed Jun 28, 2023
1 parent 2208f1e commit bad3b6f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,7 @@ namespace hpx {
HPX_CORE_EXPORT int suspend(error_code& ec = throws);
HPX_CORE_EXPORT int resume(error_code& ec = throws);
} // namespace local

// Allow applications to add a finalizer if HPX_MAIN is set
HPX_CORE_EXPORT extern void (*on_finalize)();
} // namespace hpx
9 changes: 9 additions & 0 deletions libs/core/init_runtime_local/src/init_runtime_local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@

///////////////////////////////////////////////////////////////////////////////
namespace hpx {

void (*on_finalize)() = nullptr;

namespace detail {

int init_helper(hpx::program_options::variables_map& /*vm*/,
Expand Down Expand Up @@ -151,6 +154,12 @@ namespace hpx {

rt->finalize(0);

// invoke user supplied finalizer
if (hpx::on_finalize != nullptr)
{
(*hpx::on_finalize)();
}

return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions libs/core/init_runtime_local/tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Copyright (c) 2020 The STE||AR-Group
# Copyright (c) 2020-2023 The STE||AR-Group
#
# SPDX-License-Identifier: BSL-1.0
# 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)

set(tests shutdown_suspended_thread_local)
set(tests on_finalize shutdown_suspended_thread_local)

foreach(test ${tests})
set(sources ${test}.cpp)
Expand Down
40 changes: 40 additions & 0 deletions libs/core/init_runtime_local/tests/unit/on_finalize.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2023 STE||AR Group
//
// SPDX-License-Identifier: BSL-1.0
// 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)

#include <hpx/init.hpp>
#include <hpx/modules/testing.hpp>

int hpx_main()
{
return hpx::local::finalize();
}

bool called_on_finalize = false;

void finalize_callback()
{
called_on_finalize = true;
}

struct set_finalizer
{
set_finalizer()
{
hpx::on_finalize = &finalize_callback;
}
};

set_finalizer init;

int main(int argc, char** argv)
{
HPX_TEST_EQ(hpx::local::init(hpx_main, argc, argv), 0);

// on_finalize should have been called at this point
HPX_TEST(called_on_finalize);

return hpx::util::report_errors();
}
2 changes: 1 addition & 1 deletion libs/full/init_runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2020 The STE||AR-Group
# Copyright (c) 2019-2023 The STE||AR-Group
#
# SPDX-License-Identifier: BSL-1.0
# Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand Down
42 changes: 23 additions & 19 deletions libs/full/init_runtime/src/hpx_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
#include <hpx/performance_counters/counters.hpp>
#include <hpx/performance_counters/query_counters.hpp>
#include <hpx/runtime_distributed.hpp>
#include <hpx/runtime_distributed/find_localities.hpp>
#include <hpx/runtime_distributed/runtime_fwd.hpp>
#include <hpx/runtime_distributed/runtime_support.hpp>
#endif
Expand All @@ -86,7 +85,6 @@
#include <iostream>
#include <map>
#include <memory>
#include <new>
#include <sstream>
#include <string>
#include <utility>
Expand All @@ -100,7 +98,7 @@
namespace hpx_startup {
std::vector<std::string> (*user_main_config_function)(
std::vector<std::string> const&) = nullptr;
} // namespace hpx_startup
}

///////////////////////////////////////////////////////////////////////////////
namespace hpx::detail {
Expand Down Expand Up @@ -135,7 +133,7 @@ namespace hpx::detail {
std::signal(SIGABRT, detail::on_abort);
std::atexit(detail::on_exit);
#if defined(HPX_HAVE_CXX11_STD_QUICK_EXIT)
[[maybe_unused]] int ret = std::at_quick_exit(detail::on_exit);
[[maybe_unused]] int const ret = std::at_quick_exit(detail::on_exit);
HPX_ASSERT(ret == 0);
#endif
return detail::run_or_start(f, argc, argv, params, true);
Expand Down Expand Up @@ -168,7 +166,7 @@ namespace hpx::detail {
std::signal(SIGABRT, detail::on_abort);
std::atexit(detail::on_exit);
#if defined(HPX_HAVE_CXX11_STD_QUICK_EXIT)
[[maybe_unused]] int ret = std::at_quick_exit(detail::on_exit);
[[maybe_unused]] int const ret = std::at_quick_exit(detail::on_exit);
HPX_ASSERT(ret == 0);
#endif
return 0 == detail::run_or_start(f, argc, argv, params, false);
Expand Down Expand Up @@ -319,7 +317,7 @@ namespace hpx::detail {
///////////////////////////////////////////////////////////////////////////
void list_symbolic_name(std::string const& name, hpx::id_type const& id)
{
std::string str = hpx::util::format("{}, {}, {}", name, id,
std::string const str = hpx::util::format("{}, {}, {}", name, id,
(id.get_management_type() == id_type::management_type::managed ?
"management_type::managed" :
"management_type::unmanaged"));
Expand All @@ -331,7 +329,7 @@ namespace hpx::detail {
print(std::string("List of all registered symbolic names:"));
print(std::string("--------------------------------------"));

std::map<std::string, hpx::id_type> entries =
std::map<std::string, hpx::id_type> const entries =
agas::find_symbols(hpx::launch::sync);

for (auto const& e : entries)
Expand Down Expand Up @@ -644,7 +642,8 @@ namespace hpx {
{
if (vm.count("hpx:app-config"))
{
std::string config(vm["hpx:app-config"].as<std::string>());
std::string const config(
vm["hpx:app-config"].as<std::string>());
rt.get_config().load_application_configuration(config.c_str());
}

Expand All @@ -657,7 +656,7 @@ namespace hpx {
#if defined(HPX_HAVE_DISTRIBUTED_RUNTIME)
// Add startup function related to listing counter names or counter
// infos (on console only).
bool print_counters_locally =
bool const print_counters_locally =
vm.count("hpx:print-counters-locally") != 0;
if (mode == runtime_mode::console || print_counters_locally)
handle_list_and_print_options(rt, vm, print_counters_locally);
Expand Down Expand Up @@ -735,8 +734,8 @@ namespace hpx {
HPX_MOVE(startup), HPX_MOVE(shutdown));

// pointer to runtime is stored in TLS
hpx::runtime* p = rt.release();
(void) p;
[[maybe_unused]] hpx::runtime const* p = rt.release();
HPX_ASSERT(p != nullptr);

return 0;
}
Expand Down Expand Up @@ -851,7 +850,7 @@ namespace hpx {
{
init_environment();

int result = 0;
int result;
try
{
// make sure the runtime system is not active yet
Expand Down Expand Up @@ -898,7 +897,7 @@ namespace hpx {
cmdline.rtcfg_.get_entry("hpx.affinity", ""),
cmdline.rtcfg_.get_entry("hpx.bind", ""),
hpx::util::get_entry_as<bool>(
cmdline.rtcfg_, "hpx.use_process_mask", 0));
cmdline.rtcfg_, "hpx.use_process_mask", false));

hpx::resource::partitioner rp =
hpx::resource::detail::make_partitioner(
Expand Down Expand Up @@ -1041,9 +1040,9 @@ namespace hpx {
localwait = detail::get_option("hpx.finalize_wait_time", -1.0);

{
hpx::chrono::high_resolution_timer t;
double start_time = t.elapsed();
double current = 0.0;
hpx::chrono::high_resolution_timer const t;
double const start_time = t.elapsed();
double current;
do
{
current = t.elapsed();
Expand All @@ -1064,6 +1063,11 @@ namespace hpx {

rt->finalize(shutdown_timeout);

// invoke user supplied finalizer
if (hpx::on_finalize != nullptr)
{
(*hpx::on_finalize)();
}
return 0;
}

Expand Down Expand Up @@ -1093,9 +1097,9 @@ namespace hpx {
localwait = detail::get_option("hpx.finalize_wait_time", -1.0);

{
hpx::chrono::high_resolution_timer t;
double start_time = t.elapsed();
double current = 0.0;
hpx::chrono::high_resolution_timer const t;
double const start_time = t.elapsed();
double current;
do
{
current = t.elapsed();
Expand Down

0 comments on commit bad3b6f

Please sign in to comment.