Skip to content

Commit

Permalink
Replace finally with scope_exit
Browse files Browse the repository at this point in the history
Signed-off-by: Joseph Schuchart <[email protected]>
  • Loading branch information
devreal committed Oct 30, 2024
1 parent 81abc56 commit 899ba18
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 61 deletions.
1 change: 1 addition & 0 deletions ttg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions ttg/ttg/madness/ttg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 16 additions & 4 deletions ttg/ttg/make_tt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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();
}
}
Expand All @@ -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();
}
}
Expand Down
1 change: 1 addition & 0 deletions ttg/ttg/parsec/ttg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 0 additions & 57 deletions ttg/ttg/util/finally.h

This file was deleted.

58 changes: 58 additions & 0 deletions ttg/ttg/util/scope_exit.h
Original file line number Diff line number Diff line change
@@ -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 <type_traits>

namespace ttg::detail {
template <typename EF>
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 <typename EF>
auto make_scope_exit(EF &&exit_function)
{
return scope_exit<std::remove_reference_t<EF>>(std::forward<EF>(exit_function));
}

} // namespace ttg::detail

#endif // TTG_UTIL_SCOPE_EXIT_H

0 comments on commit 899ba18

Please sign in to comment.