Skip to content

Commit 0ff3b68

Browse files
authored
improve the STDEXEC_TRY and STDEXEC_CATCH portability macros (#1549)
1 parent 0f1b477 commit 0ff3b68

5 files changed

Lines changed: 37 additions & 18 deletions

File tree

.clang-format

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ Macros: [
9090
'STDEXEC_IMMOVABLE_NO_UNIQUE_ADDRESS=[[no_unique_address]]',
9191
'STDEXEC_MISSING_MEMBER(X,Y)=true',
9292
'STDEXEC_DEFINE_MEMBER(X)=void foo() {}',
93-
'STDEXEC_AUTO_RETURN(...)=->decltype(auto){ return __VA_ARGS__; }'
93+
'STDEXEC_AUTO_RETURN(...)=->decltype(auto){ return __VA_ARGS__; }',
94+
'STDEXEC_TRY(...)=try { __VA_ARGS__ }',
95+
'STDEXEC_CATCH(...)=catch __VA_ARGS__',
9496
]
9597
MaxEmptyLinesToKeep: 2
9698
NamespaceIndentation: All

include/stdexec/__detail/__config.hpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -523,19 +523,34 @@ namespace stdexec {
523523
# include <cuda_runtime_api.h>
524524
#endif
525525

526+
// The following macros are used to conditionally compile exception handling code. They
527+
// are used in the same way as `try` and `catch` blocks, but they allow for different
528+
// behavior based on whether exceptions are enabled or not, and whether the code is being
529+
// compiled for device or not.
530+
//
531+
// Usage:
532+
// STDEXEC_TRY({
533+
// ... // Code that may throw an exception
534+
// })
535+
// STDEXEC_CATCH_OR((cuda_error& e) { // Handle CUDA exceptions
536+
// ...
537+
// })
538+
// STDEXEC_CATCH((...) { // Handle any other exceptions
539+
// ...
540+
// })
526541
// clang-format off
527542
#if STDEXEC_NO_EXCEPTIONS() || (STDEXEC_CUDA_COMPILATION() && defined(__CUDA_ARCH__))
528-
# define STDEXEC_TRY
529-
# define STDEXEC_CATCH(...) STDEXEC_CATCH_I
530-
# define STDEXEC_CATCH_I(...)
543+
# define STDEXEC_TRY(...) { __VA_ARGS__ }
544+
# define STDEXEC_CATCH(...)
545+
# define STDEXEC_CATCH_OR(...)
531546
#elif STDEXEC_CUDA_COMPILATION() && STDEXEC_NVHPC()
532-
# define STDEXEC_TRY if target (nv::target::is_host) { try
533-
# define STDEXEC_CATCH(...) catch(__VA_ARGS__) STDEXEC_CATCH_I
534-
# define STDEXEC_CATCH_I(...) { __VA_ARGS__ } } else { __VA_ARGS__ }
547+
# define STDEXEC_TRY(...) if target (nv::target::is_device) { __VA_ARGS__ } else { try { __VA_ARGS__ }
548+
# define STDEXEC_CATCH(...) catch __VA_ARGS__ }
549+
# define STDEXEC_CATCH_OR(...) catch __VA_ARGS__
535550
#else
536-
# define STDEXEC_TRY try
537-
# define STDEXEC_CATCH(...) catch(__VA_ARGS__) STDEXEC_CATCH_I
538-
# define STDEXEC_CATCH_I(...) { __VA_ARGS__ }
551+
# define STDEXEC_TRY(...) try { __VA_ARGS__ }
552+
# define STDEXEC_CATCH(...) catch __VA_ARGS__
553+
# define STDEXEC_CATCH_OR(...) catch __VA_ARGS__
539554
#endif
540555
// clang-format on
541556

include/stdexec/__detail/__receivers.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,20 @@ namespace stdexec {
192192
template <class _Receiver, class _Fun, class... _As>
193193
STDEXEC_ATTRIBUTE(host, device)
194194
void __set_value_invoke(_Receiver&& __rcvr, _Fun&& __fun, _As&&... __as) noexcept {
195-
STDEXEC_TRY {
195+
STDEXEC_TRY({
196196
if constexpr (same_as<void, __invoke_result_t<_Fun, _As...>>) {
197197
__invoke(static_cast<_Fun&&>(__fun), static_cast<_As&&>(__as)...);
198198
stdexec::set_value(static_cast<_Receiver&&>(__rcvr));
199199
} else {
200-
set_value(
200+
stdexec::set_value(
201201
static_cast<_Receiver&&>(__rcvr),
202202
__invoke(static_cast<_Fun&&>(__fun), static_cast<_As&&>(__as)...));
203203
}
204-
}
205-
STDEXEC_CATCH(...)(if constexpr (!__nothrow_invocable<_Fun, _As...>) {
206-
stdexec::set_error(static_cast<_Receiver&&>(__rcvr), std::current_exception());
204+
})
205+
STDEXEC_CATCH((...) { //
206+
if constexpr (!__nothrow_invocable<_Fun, _As...>) {
207+
stdexec::set_error(static_cast<_Receiver&&>(__rcvr), std::current_exception());
208+
}
207209
})
208210
}
209211

test/nvexec/let_stopped.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace {
3939
}
4040

4141
TEST_CASE(
42-
"let_stopped can preceed a sender without values",
42+
"nvexec let_stopped can preceed a sender without values",
4343
"[cuda][stream][adaptors][let_stopped]") {
4444
nvexec::stream_context stream_ctx{};
4545

test/nvexec/upon_error.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace {
3838
}
3939

4040
TEST_CASE(
41-
"upon_error can preceed a sender without values",
41+
"nvexec upon_error can preceed a sender without values",
4242
"[cuda][stream][adaptors][upon_error]") {
4343
nvexec::stream_context stream_ctx{};
4444

@@ -62,7 +62,7 @@ namespace {
6262
}
6363

6464
TEST_CASE(
65-
"upon_error can succeed a sender without values",
65+
"nvexec upon_error can succeed a sender without values",
6666
"[cuda][stream][adaptors][upon_error]") {
6767
nvexec::stream_context stream_ctx{};
6868

0 commit comments

Comments
 (0)