Skip to content

Commit

Permalink
Try #704:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored Jun 21, 2023
2 parents b38f3b6 + 9bc71e8 commit 6363874
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ namespace pika {
init_params const& params = init_params());
PIKA_EXPORT int finalize(error_code& ec = throws);
PIKA_EXPORT int stop(error_code& ec = throws);
PIKA_EXPORT int wait(error_code& ec = throws);
PIKA_EXPORT int suspend(error_code& ec = throws);
PIKA_EXPORT int resume(error_code& ec = throws);
} // namespace pika
15 changes: 15 additions & 0 deletions libs/pika/init_runtime/src/init_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,21 @@ namespace pika {
return result;
}

int wait(error_code& ec)
{
runtime* rt = get_runtime_ptr();
if (nullptr == rt)
{
PIKA_THROWS_IF(ec, pika::error::invalid_status, "pika::wait",
"the runtime system is not active (did you already call pika::stop?)");
return -1;
}

rt->get_thread_manager().wait();

return 0;
}

int suspend(error_code& ec)
{
if (threads::detail::get_self_ptr())
Expand Down
3 changes: 2 additions & 1 deletion libs/pika/init_runtime/tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

set(tests
config_entry const_args_init finalize_non_pika_thread scoped_finalize
config_entry const_args_init finalize_non_pika_thread pika_wait
scoped_finalize
# shutdown_suspended_thread # Disabled due to unavailable timed suspension
)

Expand Down
58 changes: 58 additions & 0 deletions libs/pika/init_runtime/tests/unit/pika_wait.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2023 ETH Zurich
//
// 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)
//
// This test checks that the runtime takes into account suspended threads before
// initiating full shutdown.

#include <pika/execution.hpp>
#include <pika/init.hpp>
#include <pika/runtime/thread_pool_helpers.hpp>
#include <pika/testing.hpp>

#include <cstddef>

namespace ex = pika::execution::experimental;
namespace tt = pika::this_thread::experimental;

void test_wait()
{
for (std::size_t i = 0; i < 1000; ++i)
{
ex::execute(ex::thread_pool_scheduler{}, [] {});
}

pika::wait();

if (pika::threads::detail::get_self_ptr())
{
PIKA_TEST_EQ(pika::threads::get_thread_count(), 1);
}
else
{
PIKA_TEST_EQ(pika::threads::get_thread_count(), 0);
}
}

int main(int argc, char** argv)
{
pika::start(nullptr, argc, argv);

// Test outside the runtime
test_wait();

// Test in a pika thread
tt::sync_wait(ex::schedule(ex::thread_pool_scheduler()) | ex::then(test_wait));

// Test in a stackless pika thread
tt::sync_wait(ex::schedule(ex::with_stacksize(
ex::thread_pool_scheduler(), pika::execution::thread_stacksize::nostack)) |
ex::then(test_wait));

pika::finalize();
pika::stop();

return 0;
}

0 comments on commit 6363874

Please sign in to comment.