Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc++][pstl] Improve exception handling #88998

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions libcxx/include/__algorithm/pstl_copy.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ template <class _ExecutionPolicy,
_LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_copy_n, _RawPolicy),
[&__policy](
_ForwardIterator __g_first, _Size __g_n, _ForwardOutIterator __g_result) -> optional<_ForwardIterator> {
if constexpr (__has_random_access_iterator_category_or_concept<_ForwardIterator>::value)
if constexpr (__has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
return std::__copy(__policy, std::move(__g_first), std::move(__g_first + __g_n), std::move(__g_result));
else
} else {
(void)__policy;
return std::copy_n(__g_first, __g_n, __g_result);
}
},
std::move(__first),
std::move(__n),
Expand Down
8 changes: 4 additions & 4 deletions libcxx/include/__algorithm/pstl_count.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ template <class _ExecutionPolicy,
class _Tp,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__iter_diff_t<_ForwardIterator>>
__count(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__iter_diff_t<_ForwardIterator>> __count(
_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last, const _Tp& __value) noexcept {
return std::__pstl_frontend_dispatch(
_LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_count, _RawPolicy),
[&](_ForwardIterator __g_first, _ForwardIterator __g_last, const _Tp& __g_value)
Expand All @@ -97,8 +97,8 @@ __count(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator
return __v == __g_value;
});
},
std::move(__first),
std::move(__last),
std::forward<_ForwardIterator>(__first),
std::forward<_ForwardIterator>(__last),
__value);
}

Expand Down
10 changes: 8 additions & 2 deletions libcxx/include/__algorithm/pstl_equal.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ _LIBCPP_HIDE_FROM_ABI bool
equal(_ExecutionPolicy&& __policy, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) {
_LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator1, "equal requires ForwardIterators");
_LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator2, "equal requires ForwardIterators");
return std::equal(__policy, std::move(__first1), std::move(__last1), std::move(__first2), std::equal_to{});
auto __res = std::__equal(__policy, std::move(__first1), std::move(__last1), std::move(__first2), std::equal_to{});
if (!__res)
std::__throw_bad_alloc();
return *__res;
}

template <class _ExecutionPolicy,
Expand Down Expand Up @@ -171,8 +174,11 @@ equal(_ExecutionPolicy&& __policy,
_ForwardIterator2 __last2) {
_LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator1, "equal requires ForwardIterators");
_LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator2, "equal requires ForwardIterators");
return std::equal(
auto __res = std::__equal(
__policy, std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), std::equal_to{});
if (!__res)
std::__throw_bad_alloc();
return *__res;
}

_LIBCPP_END_NAMESPACE_STD
Expand Down
8 changes: 4 additions & 4 deletions libcxx/include/__algorithm/pstl_fill.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ template <class _ExecutionPolicy,
class _Tp,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
_LIBCPP_HIDE_FROM_ABI optional<__empty>
__fill(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) noexcept {
_LIBCPP_HIDE_FROM_ABI optional<__empty> __fill(
_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last, const _Tp& __value) noexcept {
return std::__pstl_frontend_dispatch(
_LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_fill, _RawPolicy),
[&](_ForwardIterator __g_first, _ForwardIterator __g_last, const _Tp& __g_value) {
return std::__for_each(__policy, __g_first, __g_last, [&](__iter_reference<_ForwardIterator> __element) {
__element = __g_value;
});
},
std::move(__first),
std::move(__last),
std::forward<_ForwardIterator>(__first),
std::forward<_ForwardIterator>(__last),
__value);
}

Expand Down
18 changes: 9 additions & 9 deletions libcxx/include/__algorithm/pstl_find.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ template <class _ExecutionPolicy,
class _Predicate,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__remove_cvref_t<_ForwardIterator>>
__find_if_not(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last, _Predicate&& __pred) {
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__remove_cvref_t<_ForwardIterator>> __find_if_not(
_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last, _Predicate&& __pred) noexcept {
return std::__pstl_frontend_dispatch(
_LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_find_if_not, _RawPolicy),
[&](_ForwardIterator&& __g_first, _ForwardIterator&& __g_last, _Predicate&& __g_pred)
Expand All @@ -76,9 +76,9 @@ __find_if_not(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardI
return !__g_pred(__value);
});
},
std::move(__first),
std::move(__last),
std::move(__pred));
std::forward<_ForwardIterator>(__first),
std::forward<_ForwardIterator>(__last),
std::forward<_Predicate>(__pred));
}

template <class _ExecutionPolicy,
Expand All @@ -103,8 +103,8 @@ template <class _ExecutionPolicy,
class _Tp,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__remove_cvref_t<_ForwardIterator>>
__find(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) noexcept {
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__remove_cvref_t<_ForwardIterator>> __find(
_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last, const _Tp& __value) noexcept {
return std::__pstl_frontend_dispatch(
_LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_find, _RawPolicy),
[&](_ForwardIterator __g_first, _ForwardIterator __g_last, const _Tp& __g_value) -> optional<_ForwardIterator> {
Expand All @@ -113,8 +113,8 @@ __find(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator _
return __element == __g_value;
});
},
std::move(__first),
std::move(__last),
std::forward<_ForwardIterator>(__first),
std::forward<_ForwardIterator>(__last),
__value);
}

Expand Down
6 changes: 3 additions & 3 deletions libcxx/include/__algorithm/pstl_generate.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ template <class _ExecutionPolicy,
class _Generator,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
__generate(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last, _Generator&& __gen) {
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty> __generate(
_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last, _Generator&& __gen) noexcept {
return std::__pstl_frontend_dispatch(
_LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_generate, _RawPolicy),
[&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last, _Generator __g_gen) {
Expand Down Expand Up @@ -77,7 +77,7 @@ template <class _ExecutionPolicy,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
__generate_n(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _Size&& __n, _Generator&& __gen) {
__generate_n(_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _Size&& __n, _Generator&& __gen) noexcept {
return std::__pstl_frontend_dispatch(
_LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_generate_n, _RawPolicy),
[&__policy](_ForwardIterator __g_first, _Size __g_n, _Generator __g_gen) {
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__algorithm/pstl_is_partitioned.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ template <class _ExecutionPolicy,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<bool> __is_partitioned(
_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last, _Predicate&& __pred) {
_ExecutionPolicy&& __policy, _ForwardIterator&& __first, _ForwardIterator&& __last, _Predicate&& __pred) noexcept {
return std::__pstl_frontend_dispatch(
_LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_is_partitioned, _RawPolicy),
[&__policy](_ForwardIterator __g_first, _ForwardIterator __g_last, _Predicate __g_pred) {
Expand Down
27 changes: 14 additions & 13 deletions libcxx/include/__algorithm/pstl_merge.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <__type_traits/enable_if.h>
#include <__type_traits/is_execution_policy.h>
#include <__type_traits/remove_cvref.h>
#include <__utility/forward.h>
#include <__utility/move.h>
#include <optional>

Expand All @@ -34,26 +35,26 @@ template <class _ExecutionPolicy,
class _ForwardIterator1,
class _ForwardIterator2,
class _ForwardOutIterator,
class _Comp = std::less<>,
class _Comp,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator>
__merge(_ExecutionPolicy&&,
_ForwardIterator1 __first1,
_ForwardIterator1 __last1,
_ForwardIterator2 __first2,
_ForwardIterator2 __last2,
_ForwardOutIterator __result,
_Comp __comp = {}) noexcept {
_ForwardIterator1&& __first1,
_ForwardIterator1&& __last1,
_ForwardIterator2&& __first2,
_ForwardIterator2&& __last2,
_ForwardOutIterator&& __result,
_Comp&& __comp) noexcept {
using _Backend = typename __select_backend<_RawPolicy>::type;
return std::__pstl_merge<_RawPolicy>(
_Backend{},
std::move(__first1),
std::move(__last1),
std::move(__first2),
std::move(__last2),
std::move(__result),
std::move(__comp));
std::forward<_ForwardIterator1>(__first1),
std::forward<_ForwardIterator1>(__last1),
std::forward<_ForwardIterator2>(__first2),
std::forward<_ForwardIterator2>(__last2),
std::forward<_ForwardOutIterator>(__result),
std::forward<_Comp>(__comp));
}

template <class _ExecutionPolicy,
Expand Down
10 changes: 5 additions & 5 deletions libcxx/include/__algorithm/pstl_replace.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ template <class _ExecutionPolicy,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
__replace(_ExecutionPolicy&& __policy,
_ForwardIterator __first,
_ForwardIterator __last,
_ForwardIterator&& __first,
_ForwardIterator&& __last,
const _Tp& __old_value,
const _Tp& __new_value) noexcept {
return std::__pstl_frontend_dispatch(
Expand All @@ -106,8 +106,8 @@ __replace(_ExecutionPolicy&& __policy,
[&](__iter_reference<_ForwardIterator> __element) { return __element == __g_old_value; },
__g_new_value);
},
std::move(__first),
std::move(__last),
std::forward<_ForwardIterator>(__first),
std::forward<_ForwardIterator>(__last),
__old_value,
__new_value);
}
Expand Down Expand Up @@ -144,7 +144,7 @@ template <class _ExecutionPolicy,
_ForwardIterator&& __last,
_ForwardOutIterator&& __result,
_Pred&& __pred,
const _Tp& __new_value) {
const _Tp& __new_value) noexcept {
return std::__pstl_frontend_dispatch(
_LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_replace_copy_if, _RawPolicy),
[&__policy](_ForwardIterator __g_first,
Expand Down
16 changes: 10 additions & 6 deletions libcxx/include/__algorithm/pstl_sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,20 @@ template <class _ExecutionPolicy,
class _Comp,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty> __sort(
_ExecutionPolicy&& __policy, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp __comp) noexcept {
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
__sort(_ExecutionPolicy&& __policy,
_RandomAccessIterator&& __first,
_RandomAccessIterator&& __last,
_Comp&& __comp) noexcept {
return std::__pstl_frontend_dispatch(
_LIBCPP_PSTL_CUSTOMIZATION_POINT(__pstl_sort, _RawPolicy),
[&__policy](_RandomAccessIterator __g_first, _RandomAccessIterator __g_last, _Comp __g_comp) {
std::stable_sort(__policy, std::move(__g_first), std::move(__g_last), std::move(__g_comp));
return optional<__empty>{__empty{}};
},
std::move(__first),
std::move(__last),
std::move(__comp));
std::forward<_RandomAccessIterator>(__first),
std::forward<_RandomAccessIterator>(__last),
std::forward<_Comp>(__comp));
}

template <class _ExecutionPolicy,
Expand All @@ -73,7 +76,8 @@ template <class _ExecutionPolicy,
_LIBCPP_HIDE_FROM_ABI void
sort(_ExecutionPolicy&& __policy, _RandomAccessIterator __first, _RandomAccessIterator __last) {
_LIBCPP_REQUIRE_CPP17_RANDOM_ACCESS_ITERATOR(_RandomAccessIterator, "sort requires RandomAccessIterators");
std::sort(std::forward<_ExecutionPolicy>(__policy), std::move(__first), std::move(__last), less{});
if (!std::__sort(__policy, std::move(__first), std::move(__last), less{}))
std::__throw_bad_alloc();
}

_LIBCPP_END_NAMESPACE_STD
Expand Down

This file was deleted.

This file was deleted.

Loading
Loading