1bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 2bdd1243dSDimitry Andric // 3bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6bdd1243dSDimitry Andric // 7bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 8bdd1243dSDimitry Andric 9bdd1243dSDimitry Andric #ifndef _LIBCPP___UTILITY_TRANSACTION_H 10bdd1243dSDimitry Andric #define _LIBCPP___UTILITY_TRANSACTION_H 11bdd1243dSDimitry Andric 12bdd1243dSDimitry Andric #include <__assert> 13bdd1243dSDimitry Andric #include <__config> 14*0fca6ea1SDimitry Andric #include <__type_traits/is_nothrow_constructible.h> 15bdd1243dSDimitry Andric #include <__utility/exchange.h> 16bdd1243dSDimitry Andric #include <__utility/move.h> 17bdd1243dSDimitry Andric 18bdd1243dSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 19bdd1243dSDimitry Andric # pragma GCC system_header 20bdd1243dSDimitry Andric #endif 21bdd1243dSDimitry Andric 2206c3fb27SDimitry Andric _LIBCPP_PUSH_MACROS 2306c3fb27SDimitry Andric #include <__undef_macros> 2406c3fb27SDimitry Andric 25bdd1243dSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 26bdd1243dSDimitry Andric 27bdd1243dSDimitry Andric // __exception_guard is a helper class for writing code with the strong exception guarantee. 28bdd1243dSDimitry Andric // 29bdd1243dSDimitry Andric // When writing code that can throw an exception, one can store rollback instructions in an 30bdd1243dSDimitry Andric // exception guard so that if an exception is thrown at any point during the lifetime of the 31bdd1243dSDimitry Andric // exception guard, it will be rolled back automatically. When the exception guard is done, one 32bdd1243dSDimitry Andric // must mark it as being complete so it isn't rolled back when the exception guard is destroyed. 33bdd1243dSDimitry Andric // 34bdd1243dSDimitry Andric // Exception guards are not default constructible, they can't be copied or assigned to, but 35bdd1243dSDimitry Andric // they can be moved around for convenience. 36bdd1243dSDimitry Andric // 37bdd1243dSDimitry Andric // __exception_guard is a no-op in -fno-exceptions mode to produce better code-gen. This means 38bdd1243dSDimitry Andric // that we don't provide the strong exception guarantees. However, Clang doesn't generate cleanup 39bdd1243dSDimitry Andric // code with exceptions disabled, so even if we wanted to provide the strong exception guarantees 40bdd1243dSDimitry Andric // we couldn't. This is also only relevant for constructs with a stack of 41bdd1243dSDimitry Andric // -fexceptions > -fno-exceptions > -fexceptions code, since the exception can't be caught where 42bdd1243dSDimitry Andric // exceptions are disabled. While -fexceptions > -fno-exceptions is quite common 43bdd1243dSDimitry Andric // (e.g. libc++.dylib > -fno-exceptions), having another layer with exceptions enabled seems a lot 44bdd1243dSDimitry Andric // less common, especially one that tries to catch an exception through -fno-exceptions code. 45bdd1243dSDimitry Andric // 46bdd1243dSDimitry Andric // __exception_guard can help greatly simplify code that would normally be cluttered by 4706c3fb27SDimitry Andric // `#if _LIBCPP_HAS_NO_EXCEPTIONS`. For example: 48bdd1243dSDimitry Andric // 49bdd1243dSDimitry Andric // template <class Iterator, class Size, class OutputIterator> 50bdd1243dSDimitry Andric // Iterator uninitialized_copy_n(Iterator iter, Size n, OutputIterator out) { 51bdd1243dSDimitry Andric // typedef typename iterator_traits<Iterator>::value_type value_type; 52bdd1243dSDimitry Andric // __exception_guard guard([start=out, &out] { 53bdd1243dSDimitry Andric // std::destroy(start, out); 54bdd1243dSDimitry Andric // }); 55bdd1243dSDimitry Andric // 56bdd1243dSDimitry Andric // for (; n > 0; ++iter, ++out, --n) { 57bdd1243dSDimitry Andric // ::new ((void*)std::addressof(*out)) value_type(*iter); 58bdd1243dSDimitry Andric // } 59bdd1243dSDimitry Andric // guard.__complete(); 60bdd1243dSDimitry Andric // return out; 61bdd1243dSDimitry Andric // } 62bdd1243dSDimitry Andric // 63bdd1243dSDimitry Andric 64bdd1243dSDimitry Andric template <class _Rollback> 651ac55f4cSDimitry Andric struct __exception_guard_exceptions { 661ac55f4cSDimitry Andric __exception_guard_exceptions() = delete; 67bdd1243dSDimitry Andric 681ac55f4cSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __exception_guard_exceptions(_Rollback __rollback) 69bdd1243dSDimitry Andric : __rollback_(std::move(__rollback)), __completed_(false) {} 70bdd1243dSDimitry Andric 711ac55f4cSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 721ac55f4cSDimitry Andric __exception_guard_exceptions(__exception_guard_exceptions&& __other) 73bdd1243dSDimitry Andric _NOEXCEPT_(is_nothrow_move_constructible<_Rollback>::value) 74bdd1243dSDimitry Andric : __rollback_(std::move(__other.__rollback_)), __completed_(__other.__completed_) { 75bdd1243dSDimitry Andric __other.__completed_ = true; 76bdd1243dSDimitry Andric } 77bdd1243dSDimitry Andric 781ac55f4cSDimitry Andric __exception_guard_exceptions(__exception_guard_exceptions const&) = delete; 791ac55f4cSDimitry Andric __exception_guard_exceptions& operator=(__exception_guard_exceptions const&) = delete; 801ac55f4cSDimitry Andric __exception_guard_exceptions& operator=(__exception_guard_exceptions&&) = delete; 81bdd1243dSDimitry Andric 82bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __complete() _NOEXCEPT { __completed_ = true; } 83bdd1243dSDimitry Andric 841ac55f4cSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__exception_guard_exceptions() { 85bdd1243dSDimitry Andric if (!__completed_) 86bdd1243dSDimitry Andric __rollback_(); 87bdd1243dSDimitry Andric } 88bdd1243dSDimitry Andric 89bdd1243dSDimitry Andric private: 90bdd1243dSDimitry Andric _Rollback __rollback_; 91bdd1243dSDimitry Andric bool __completed_; 92bdd1243dSDimitry Andric }; 931ac55f4cSDimitry Andric 941ac55f4cSDimitry Andric _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__exception_guard_exceptions); 951ac55f4cSDimitry Andric 961ac55f4cSDimitry Andric template <class _Rollback> 971ac55f4cSDimitry Andric struct __exception_guard_noexceptions { 981ac55f4cSDimitry Andric __exception_guard_noexceptions() = delete; 99*0fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI 100*0fca6ea1SDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG explicit __exception_guard_noexceptions(_Rollback) {} 101bdd1243dSDimitry Andric 1021ac55f4cSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG 1031ac55f4cSDimitry Andric __exception_guard_noexceptions(__exception_guard_noexceptions&& __other) 104bdd1243dSDimitry Andric _NOEXCEPT_(is_nothrow_move_constructible<_Rollback>::value) 105bdd1243dSDimitry Andric : __completed_(__other.__completed_) { 106bdd1243dSDimitry Andric __other.__completed_ = true; 107bdd1243dSDimitry Andric } 108bdd1243dSDimitry Andric 1091ac55f4cSDimitry Andric __exception_guard_noexceptions(__exception_guard_noexceptions const&) = delete; 1101ac55f4cSDimitry Andric __exception_guard_noexceptions& operator=(__exception_guard_noexceptions const&) = delete; 1111ac55f4cSDimitry Andric __exception_guard_noexceptions& operator=(__exception_guard_noexceptions&&) = delete; 112bdd1243dSDimitry Andric 113bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG void __complete() _NOEXCEPT { 114bdd1243dSDimitry Andric __completed_ = true; 115bdd1243dSDimitry Andric } 116bdd1243dSDimitry Andric 1171ac55f4cSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG ~__exception_guard_noexceptions() { 1181db9f3b2SDimitry Andric _LIBCPP_ASSERT_INTERNAL(__completed_, "__exception_guard not completed with exceptions disabled"); 119bdd1243dSDimitry Andric } 120bdd1243dSDimitry Andric 121bdd1243dSDimitry Andric private: 122bdd1243dSDimitry Andric bool __completed_ = false; 123bdd1243dSDimitry Andric }; 124bdd1243dSDimitry Andric 1251ac55f4cSDimitry Andric _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__exception_guard_noexceptions); 1261ac55f4cSDimitry Andric 12706c3fb27SDimitry Andric #ifdef _LIBCPP_HAS_NO_EXCEPTIONS 1281ac55f4cSDimitry Andric template <class _Rollback> 1291ac55f4cSDimitry Andric using __exception_guard = __exception_guard_noexceptions<_Rollback>; 13006c3fb27SDimitry Andric #else 13106c3fb27SDimitry Andric template <class _Rollback> 13206c3fb27SDimitry Andric using __exception_guard = __exception_guard_exceptions<_Rollback>; 13306c3fb27SDimitry Andric #endif 134bdd1243dSDimitry Andric 135bdd1243dSDimitry Andric template <class _Rollback> 136bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __exception_guard<_Rollback> __make_exception_guard(_Rollback __rollback) { 137bdd1243dSDimitry Andric return __exception_guard<_Rollback>(std::move(__rollback)); 138bdd1243dSDimitry Andric } 139bdd1243dSDimitry Andric 140bdd1243dSDimitry Andric _LIBCPP_END_NAMESPACE_STD 141bdd1243dSDimitry Andric 14206c3fb27SDimitry Andric _LIBCPP_POP_MACROS 14306c3fb27SDimitry Andric 144bdd1243dSDimitry Andric #endif // _LIBCPP___UTILITY_TRANSACTION_H 145