Skip to content

Commit

Permalink
Adding scope_xxx from library fundamentals TS v3
Browse files Browse the repository at this point in the history
-flyby: adding feature test for constexpr destructors
  • Loading branch information
hkaiser committed Jan 11, 2024
1 parent 7b35448 commit 50bf437
Show file tree
Hide file tree
Showing 34 changed files with 788 additions and 409 deletions.
18 changes: 18 additions & 0 deletions cmake/HPX_AddConfigTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,15 @@ function(hpx_check_for_cxx20_std_bit_cast)
)
endfunction()

# ##############################################################################
function(hpx_check_for_cxx20_constexpr_destructor)
add_hpx_config_test(
HPX_WITH_CXX20_CONSTEXPR_DESTRUCTOR
SOURCE cmake/tests/cxx20_constexpr_destructor.cpp
FILE ${ARGN}
)
endfunction()

# ##############################################################################
function(hpx_check_for_cxx23_std_generator)
add_hpx_config_test(
Expand All @@ -642,6 +651,15 @@ function(hpx_check_for_cxx23_std_generator)
)
endfunction()

# ##############################################################################
function(hpx_check_for_cxx26_experimental_scope)
add_hpx_config_test(
HPX_WITH_CXX26_EXPERIMENTAL_SCOPE
SOURCE cmake/tests/cxx26_experimental_scope.cpp
FILE ${ARGN}
)
endfunction()

# ##############################################################################
function(hpx_check_for_cxx_lambda_capture_decltype)
add_hpx_config_test(
Expand Down
8 changes: 8 additions & 0 deletions cmake/HPX_PerformCxxFeatureTests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,20 @@ function(hpx_perform_cxx_feature_tests)
)

hpx_check_for_cxx20_std_bit_cast(DEFINITIONS HPX_HAVE_CXX20_STD_BIT_CAST)

hpx_check_for_cxx20_constexpr_destructor(
DEFINITIONS HPX_HAVE_CXX20_CONSTEXPR_DESTRUCTOR
)
endif()

if(HPX_WITH_CXX_STANDARD GREATER_EQUAL 23)
hpx_check_for_cxx23_std_generator(DEFINITIONS HPX_HAVE_CXX23_STD_GENERATOR)
endif()

hpx_check_for_cxx26_experimental_scope(
DEFINITIONS HPX_HAVE_CXX26_EXPERIMENTAL_SCOPE
)

hpx_check_for_cxx_lambda_capture_decltype(
DEFINITIONS HPX_HAVE_CXX_LAMBDA_CAPTURE_DECLTYPE
)
Expand Down
19 changes: 19 additions & 0 deletions cmake/tests/cxx20_constexpr_destructor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2024 Hartmut Kaiser
//
// 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 <iterator>

struct A
{
constexpr A() noexcept {}
constexpr ~A() {}
};

int main()
{
[[maybe_unused]] A a;
return 0;
}
22 changes: 22 additions & 0 deletions cmake/tests/cxx26_experimental_scope.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2024 Hartmut Kaiser
//
// 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)

// test for availability of std::experimental::scope_xxx

#include <experimental/scope>

#if !defined(__cpp_lib_experimental_scope)
# error "__cpp_lib_experimental_scope not defined, assume scope_exit etc. is not supported"
#endif

int main()
{
std::experimental::scope_exit se([] {});
std::experimental::scope_failure sf([] {});
std::experimental::scope_success ss([] {});

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <hpx/components_base/server/locking_hook.hpp>
#include <hpx/modules/collectives.hpp>
#include <hpx/modules/errors.hpp>
#include <hpx/modules/functional.hpp>
#include <hpx/preprocessor/cat.hpp>
#include <hpx/preprocessor/expand.hpp>
#include <hpx/preprocessor/nargs.hpp>
Expand Down Expand Up @@ -203,22 +204,6 @@ namespace hpx { namespace server {
/// \return Return the value of the element at position represented
/// by \a pos.
///
struct erase_on_exit
{
erase_on_exit(data_type& m, typename data_type::iterator& it)
: m_(m)
, it_(it)
{
}
~erase_on_exit()
{
m_.erase(it_);
}

data_type& m_;
typename data_type::iterator& it_;
};

T get_value(Key const& key, bool erase)
{
typename data_type::iterator it =
Expand All @@ -234,7 +219,9 @@ namespace hpx { namespace server {
if (!erase)
return it->second;

erase_on_exit t(partition_unordered_map_, it);
auto on_exit = hpx::experimental::scope_exit(
[this, &it] { partition_unordered_map_.erase(it); });

return it->second;
}

Expand Down
30 changes: 11 additions & 19 deletions libs/core/algorithms/include/hpx/parallel/task_group.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2023 Hartmut Kaiser
// Copyright (c) 2021-2024 Hartmut Kaiser
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand All @@ -16,6 +16,7 @@
#include <hpx/execution_base/execution.hpp>
#include <hpx/execution_base/traits/is_executor.hpp>
#include <hpx/executors/parallel_executor.hpp>
#include <hpx/functional/experimental/scope_exit.hpp>
#include <hpx/functional/invoke_fused.hpp>
#include <hpx/futures/detail/future_data.hpp>
#include <hpx/modules/memory.hpp>
Expand Down Expand Up @@ -44,21 +45,6 @@ namespace hpx::experimental {
task_group& operator=(task_group const&) = delete;
task_group& operator=(task_group&&) = delete;

private:
struct on_exit
{
HPX_CORE_EXPORT explicit on_exit(task_group& tg);
HPX_CORE_EXPORT ~on_exit();

on_exit(on_exit const& rhs) = delete;
on_exit& operator=(on_exit const& rhs) = delete;

HPX_CORE_EXPORT on_exit(on_exit&& rhs) noexcept;
HPX_CORE_EXPORT on_exit& operator=(on_exit&& rhs) noexcept;

hpx::lcos::local::latch* latch_;
};

public:
/// \brief Adds a task to compute \c f() and returns immediately.
///
Expand All @@ -82,13 +68,19 @@ namespace hpx::experimental {
void run(Executor&& exec, F&& f, Ts&&... ts)
{
// make sure exceptions don't leave the latch in the wrong state
on_exit l(*this);
if (latch_.reset_if_needed_and_count_up(1, 1))
{
has_arrived_.store(false, std::memory_order_release);
}

auto on_exit =
hpx::experimental::scope_exit([this] { latch_.count_down(1); });

hpx::parallel::execution::post(HPX_FORWARD(Executor, exec),
[this, l = HPX_MOVE(l), f = HPX_FORWARD(F, f),
[this, on_exit = HPX_MOVE(on_exit), f = HPX_FORWARD(F, f),
t = hpx::make_tuple(HPX_FORWARD(Ts, ts)...)]() mutable {
// latch needs to be released before the lambda exits
on_exit _(HPX_MOVE(l));
auto _(HPX_MOVE(on_exit));

hpx::detail::try_catch_exception_ptr(
[&]() { hpx::invoke_fused(HPX_MOVE(f), HPX_MOVE(t)); },
Expand Down
33 changes: 1 addition & 32 deletions libs/core/algorithms/src/task_group.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2023 Hartmut Kaiser
// Copyright (c) 2021-2024 Hartmut Kaiser
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand All @@ -16,37 +16,6 @@

namespace hpx::experimental {

///////////////////////////////////////////////////////////////////////////
task_group::on_exit::on_exit(task_group& tg)
: latch_(&tg.latch_)
{
if (latch_->reset_if_needed_and_count_up(1, 1))
{
tg.has_arrived_.store(false, std::memory_order_release);
}
}

task_group::on_exit::~on_exit()
{
if (latch_ != nullptr)
{
latch_->count_down(1);
}
}

task_group::on_exit::on_exit(on_exit&& rhs) noexcept
: latch_(rhs.latch_)
{
rhs.latch_ = nullptr;
}

task_group::on_exit& task_group::on_exit::operator=(on_exit&& rhs) noexcept
{
latch_ = rhs.latch_;
rhs.latch_ = nullptr;
return *this;
}

///////////////////////////////////////////////////////////////////////////
task_group::task_group()
: latch_(1)
Expand Down
1 change: 1 addition & 0 deletions libs/core/async_cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ add_hpx_module(
hpx_debugging
hpx_errors
hpx_execution_base
hpx_functional
hpx_futures
hpx_memory
hpx_runtime_local
Expand Down
28 changes: 5 additions & 23 deletions libs/core/async_cuda/include/hpx/async_cuda/cuda_future.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2023 Gregor Daiß
// Copyright (c) 2023 Gregor Daiß
// Copyright (c) 2020 John Biddiscombe
// Copyright (c) 2016 Thomas Heller
// Copyright (c) 2016 Hartmut Kaiser
// Copyright (c) 2016-2024 Hartmut Kaiser
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
Expand All @@ -18,6 +18,7 @@
#include <hpx/async_cuda/custom_gpu_api.hpp>
#include <hpx/async_cuda/detail/cuda_debug.hpp>
#include <hpx/async_cuda/detail/cuda_event_callback.hpp>
#include <hpx/functional/experimental/scope_exit.hpp>
#include <hpx/futures/future.hpp>
#include <hpx/modules/concurrency.hpp>
#include <hpx/modules/execution_base.hpp>
Expand Down Expand Up @@ -47,26 +48,6 @@ namespace hpx { namespace cuda { namespace experimental {
template <typename Allocator, typename Mode>
struct future_data;

// -------------------------------------------------------------
// helper struct to delete future data in destructor
template <typename Allocator>
struct release_on_exit
{
explicit release_on_exit(
future_data<Allocator, callback_mode>* data)
: data_(data)
{
}

~release_on_exit()
{
// release the shared state
lcos::detail::intrusive_ptr_release(data_);
}

future_data<Allocator, callback_mode>* data_;
};

template <typename Allocator>
struct future_data<Allocator, event_mode>
: lcos::detail::future_data_allocator<void, Allocator,
Expand Down Expand Up @@ -162,7 +143,8 @@ namespace hpx { namespace cuda { namespace experimental {
{
future_data* this_ = static_cast<future_data*>(user_data);

release_on_exit<Allocator> on_exit(this_);
auto on_exit = hpx::experimental::scope_exit(
[&] { hpx::lcos::detail::intrusive_ptr_release(this_); });

if (error != cudaSuccess)
{
Expand Down
6 changes: 6 additions & 0 deletions libs/core/config/include/hpx/config/constexpr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@
#else
#define HPX_HOST_DEVICE_CONSTEXPR constexpr
#endif

#if defined(HPX_HAVE_CXX20_CONSTEXPR_DESTRUCTOR)
#define HPX_CONSTEXPR_DESTRUCTOR constexpr
#else
#define HPX_CONSTEXPR_DESTRUCTOR
#endif
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2006, Giovanni P. Deretta
// Copyright (c) 2007-2022 Hartmut Kaiser
// Copyright (c) 2007-2024 Hartmut Kaiser
//
// This code may be used under either of the following two licences:
//
Expand Down
Loading

0 comments on commit 50bf437

Please sign in to comment.