From 899ba186ee37b4195e6f89c22bd8db523da8d09d Mon Sep 17 00:00:00 2001 From: Joseph Schuchart Date: Wed, 30 Oct 2024 10:22:47 -0400 Subject: [PATCH] Replace finally with scope_exit Signed-off-by: Joseph Schuchart --- ttg/CMakeLists.txt | 1 + ttg/ttg/madness/ttg.h | 1 + ttg/ttg/make_tt.h | 20 +++++++++++--- ttg/ttg/parsec/ttg.h | 1 + ttg/ttg/util/finally.h | 57 -------------------------------------- ttg/ttg/util/scope_exit.h | 58 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 77 insertions(+), 61 deletions(-) delete mode 100644 ttg/ttg/util/finally.h create mode 100644 ttg/ttg/util/scope_exit.h diff --git a/ttg/CMakeLists.txt b/ttg/CMakeLists.txt index b1fa72947..272f005ab 100644 --- a/ttg/CMakeLists.txt +++ b/ttg/CMakeLists.txt @@ -22,6 +22,7 @@ set(ttg-util-headers ${CMAKE_CURRENT_SOURCE_DIR}/ttg/util/meta.h ${CMAKE_CURRENT_SOURCE_DIR}/ttg/util/meta/callable.h ${CMAKE_CURRENT_SOURCE_DIR}/ttg/util/print.h + ${CMAKE_CURRENT_SOURCE_DIR}/ttg/util/scope_exit.h ${CMAKE_CURRENT_SOURCE_DIR}/ttg/util/span.h ${CMAKE_CURRENT_SOURCE_DIR}/ttg/util/trace.h ${CMAKE_CURRENT_SOURCE_DIR}/ttg/util/tree.h diff --git a/ttg/ttg/madness/ttg.h b/ttg/ttg/madness/ttg.h index 5d2360cfb..85dee437f 100644 --- a/ttg/ttg/madness/ttg.h +++ b/ttg/ttg/madness/ttg.h @@ -22,6 +22,7 @@ #include "ttg/util/macro.h" #include "ttg/util/meta.h" #include "ttg/util/meta/callable.h" +#include "ttg/util/scope_exit.h" #include "ttg/util/void.h" #include "ttg/world.h" #include "ttg/coroutine.h" diff --git a/ttg/ttg/make_tt.h b/ttg/ttg/make_tt.h index 7ba01b56d..97e6dfccc 100644 --- a/ttg/ttg/make_tt.h +++ b/ttg/ttg/make_tt.h @@ -127,7 +127,10 @@ class CallableWrapTTArgs auto old_output_tls_ptr = this->outputs_tls_ptr_accessor(); this->set_outputs_tls_ptr(); // make sure the output tls is reset - auto _ = ttg::detail::finally([this, old_output_tls_ptr](){ this->set_outputs_tls_ptr(old_output_tls_ptr); }); + auto _ = ttg::detail::scope_exit( + [this, old_output_tls_ptr](){ + this->set_outputs_tls_ptr(old_output_tls_ptr); + }); return unpack_input_tuple_if_needed(); } } @@ -158,7 +161,10 @@ class CallableWrapTTArgs auto old_output_tls_ptr = this->outputs_tls_ptr_accessor(); this->set_outputs_tls_ptr(); // make sure the output tls is reset - auto _ = ttg::detail::finally([this, old_output_tls_ptr](){ this->set_outputs_tls_ptr(old_output_tls_ptr); }); + auto _ = ttg::detail::scope_exit( + [this, old_output_tls_ptr](){ + this->set_outputs_tls_ptr(old_output_tls_ptr); + }); return unpack_input_tuple_if_needed(invoke_func_handle_ret); } } @@ -179,7 +185,10 @@ class CallableWrapTTArgs auto old_output_tls_ptr = this->outputs_tls_ptr_accessor(); this->set_outputs_tls_ptr(); // make sure the output tls is reset - auto _ = ttg::detail::finally([this, old_output_tls_ptr](){ this->set_outputs_tls_ptr(old_output_tls_ptr); }); + auto _ = ttg::detail::scope_exit( + [this, old_output_tls_ptr](){ + this->set_outputs_tls_ptr(old_output_tls_ptr); + }); return invoke_func_handle_ret(); } } @@ -201,7 +210,10 @@ class CallableWrapTTArgs auto old_output_tls_ptr = this->outputs_tls_ptr_accessor(); this->set_outputs_tls_ptr(); // make sure the output tls is reset - auto _ = ttg::detail::finally([this, old_output_tls_ptr](){ this->set_outputs_tls_ptr(old_output_tls_ptr); }); + auto _ = ttg::detail::scope_exit( + [this, old_output_tls_ptr](){ + this->set_outputs_tls_ptr(old_output_tls_ptr); + }); return invoke_func_handle_ret(); } } diff --git a/ttg/ttg/parsec/ttg.h b/ttg/ttg/parsec/ttg.h index 0a0ddefcb..2fc62bd91 100644 --- a/ttg/ttg/parsec/ttg.h +++ b/ttg/ttg/parsec/ttg.h @@ -33,6 +33,7 @@ #include "ttg/util/meta.h" #include "ttg/util/meta/callable.h" #include "ttg/util/print.h" +#include "ttg/util/scope_exit.h" #include "ttg/util/trace.h" #include "ttg/util/typelist.h" #ifdef TTG_HAVE_DEVICE diff --git a/ttg/ttg/util/finally.h b/ttg/ttg/util/finally.h deleted file mode 100644 index d37a94dc3..000000000 --- a/ttg/ttg/util/finally.h +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef TTG_UTIL_FINALLY_H -#define TTG_UTIL_FINALLY_H - -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) 2015 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// -/////////////////////////////////////////////////////////////////////////////// - -namespace ttg::detail { - - /* finally, taken from GSL: https://github.com/microsoft/GSL */ - - // final_action allows you to ensure something gets run at the end of a scope - template - class final_action - { - public: - explicit final_action(const F& ff) noexcept : f{ff} { } - explicit final_action(F&& ff) noexcept : f{std::move(ff)} { } - - ~final_action() noexcept { if (invoke) f(); } - - final_action(final_action&& other) noexcept - : f(std::move(other.f)), invoke(std::exchange(other.invoke, false)) - { } - - final_action(const final_action&) = delete; - void operator=(const final_action&) = delete; - void operator=(final_action&&) = delete; - - private: - F f; - bool invoke = true; - }; - - - // finally() - convenience function to generate a final_action - template - [[nodiscard]] auto finally(F&& f) noexcept - { - return final_action>{std::forward(f)}; - } - -} // namespace ttg::detail - -#endif // TTG_UTIL_FINALLY_H diff --git a/ttg/ttg/util/scope_exit.h b/ttg/ttg/util/scope_exit.h new file mode 100644 index 000000000..5e5fe6722 --- /dev/null +++ b/ttg/ttg/util/scope_exit.h @@ -0,0 +1,58 @@ +#ifndef TTG_UTIL_SCOPE_EXIT_H +#define TTG_UTIL_SCOPE_EXIT_H + +// +// N4189: Scoped Resource - Generic RAII Wrapper for the Standard Library +// Peter Sommerlad and Andrew L. Sandoval +// Adopted from https://github.com/tandasat/ScopedResource/tree/master +// + +#include + +namespace ttg::detail { + template + struct scope_exit + { + // construction + explicit + scope_exit(EF &&f) + : exit_function(std::move(f)) + , execute_on_destruction{ true } + { } + + // move + scope_exit(scope_exit &&rhs) + : exit_function(std::move(rhs.exit_function)) + , execute_on_destruction{ rhs.execute_on_destruction } + { + rhs.release(); + } + + // release + ~scope_exit() + { + if (execute_on_destruction) this->exit_function(); + } + + void release() + { + this->execute_on_destruction = false; + } + + private: + scope_exit(scope_exit const &) = delete; + void operator=(scope_exit const &) = delete; + scope_exit& operator=(scope_exit &&) = delete; + EF exit_function; + bool execute_on_destruction; // exposition only + }; + + template + auto make_scope_exit(EF &&exit_function) + { + return scope_exit>(std::forward(exit_function)); + } + +} // namespace ttg::detail + +#endif // TTG_UTIL_SCOPE_EXIT_H \ No newline at end of file