Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
exception_guard.h154 linesDownload Raw Back to __utility
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6// SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES.7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCUDACXX___UTILITY_EXCEPTION_GUARD_H11#define _LIBCUDACXX___UTILITY_EXCEPTION_GUARD_H12 13#ifndef __cuda_std__14#include <__config>15#endif // __cuda_std__16 17#include "../__assert"18#include "../__type_traits/is_nothrow_move_constructible.h"19#include "../__utility/exchange.h"20#include "../__utility/move.h"21 22#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)23#  pragma GCC system_header24#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)25#  pragma clang system_header26#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)27#  pragma system_header28#endif // no system header29 30_LIBCUDACXX_BEGIN_NAMESPACE_STD31 32// __exception_guard is a helper class for writing code with the strong exception guarantee.33//34// When writing code that can throw an exception, one can store rollback instructions in an35// exception guard so that if an exception is thrown at any point during the lifetime of the36// exception guard, it will be rolled back automatically. When the exception guard is done, one37// must mark it as being complete so it isn't rolled back when the exception guard is destroyed.38//39// Exception guards are not default constructible, they can't be copied or assigned to, but40// they can be moved around for convenience.41//42// __exception_guard is a no-op in -fno-exceptions mode to produce better code-gen. This means43// that we don't provide the strong exception guarantees. However, Clang doesn't generate cleanup44// code with exceptions disabled, so even if we wanted to provide the strong exception guarantees45// we couldn't. This is also only relevant for constructs with a stack of46// -fexceptions > -fno-exceptions > -fexceptions code, since the exception can't be caught where47// exceptions are disabled. While -fexceptions > -fno-exceptions is quite common48// (e.g. libc++.dylib > -fno-exceptions), having another layer with exceptions enabled seems a lot49// less common, especially one that tries to catch an exception through -fno-exceptions code.50//51// __exception_guard can help greatly simplify code that would normally be cluttered by52// `#if _LIBCUDACXX_NO_EXCEPTIONS`. For example:53//54//    template <class Iterator, class Size, class OutputIterator>55//    Iterator uninitialized_copy_n(Iterator iter, Size n, OutputIterator out) {56//        typedef typename iterator_traits<Iterator>::value_type value_type;57//        __exception_guard guard([start=out, &out] {58//            _CUDA_VSTD::destroy(start, out);59//        });60//61//        for (; n > 0; ++iter, ++out, --n) {62//            ::new ((void*)_CUDA_VSTD::addressof(*out)) value_type(*iter);63//        }64//        guard.__complete();65//        return out;66//    }67//68 69template <class _Rollback>70struct __exception_guard_exceptions {71  __exception_guard_exceptions() = delete;72 73  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX1774  explicit __exception_guard_exceptions(_Rollback __rollback)75      : __rollback_(_CUDA_VSTD::move(__rollback)), __completed_(false) {}76 77  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX1778  __exception_guard_exceptions(__exception_guard_exceptions&& __other)79      noexcept(_LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _Rollback))80      : __rollback_(_CUDA_VSTD::move(__other.__rollback_)), __completed_(__other.__completed_) {81    __other.__completed_ = true;82  }83 84  __exception_guard_exceptions(__exception_guard_exceptions const&)            = delete;85  __exception_guard_exceptions& operator=(__exception_guard_exceptions const&) = delete;86  __exception_guard_exceptions& operator=(__exception_guard_exceptions&&)      = delete;87 88  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX1789  void __complete() noexcept { __completed_ = true; }90 91  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX1792  ~__exception_guard_exceptions() {93    if (!__completed_)94      __rollback_();95  }96 97private:98  _Rollback __rollback_;99  bool __completed_;100};101 102_LIBCUDACXX_CTAD_SUPPORTED_FOR_TYPE(__exception_guard_exceptions);103 104template <class _Rollback>105struct __exception_guard_noexceptions {106  __exception_guard_noexceptions() = delete;107  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17 _LIBCUDACXX_NODEBUG108  explicit __exception_guard_noexceptions(_Rollback) {}109 110  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17 _LIBCUDACXX_NODEBUG111  __exception_guard_noexceptions(__exception_guard_noexceptions&& __other)112      noexcept(_LIBCUDACXX_TRAIT(is_nothrow_move_constructible, _Rollback))113      : __completed_(__other.__completed_) {114    __other.__completed_ = true;115  }116 117  __exception_guard_noexceptions(__exception_guard_noexceptions const&)            = delete;118  __exception_guard_noexceptions& operator=(__exception_guard_noexceptions const&) = delete;119  __exception_guard_noexceptions& operator=(__exception_guard_noexceptions&&)      = delete;120 121  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17 _LIBCUDACXX_NODEBUG122  void __complete() noexcept {123    __completed_ = true;124  }125 126  _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17 _LIBCUDACXX_NODEBUG127  ~__exception_guard_noexceptions() {128    _LIBCUDACXX_ASSERT(__completed_, "__exception_guard not completed with exceptions disabled");129  }130 131private:132  bool __completed_ = false;133};134 135_LIBCUDACXX_CTAD_SUPPORTED_FOR_TYPE(__exception_guard_noexceptions);136 137#ifdef _LIBCUDACXX_NO_EXCEPTIONS138template <class _Rollback>139using __exception_guard = __exception_guard_noexceptions<_Rollback>;140#else141template <class _Rollback>142using __exception_guard = __exception_guard_exceptions<_Rollback>;143#endif144 145template <class _Rollback>146_LIBCUDACXX_INLINE_VISIBILITY constexpr147__exception_guard<_Rollback> __make_exception_guard(_Rollback __rollback) {148  return __exception_guard<_Rollback>(_CUDA_VSTD::move(__rollback));149}150 151_LIBCUDACXX_END_NAMESPACE_STD152 153#endif // _LIBCUDACXX___UTILITY_EXCEPTION_GUARD_H154 
codekingpro/portable-devtools · Team Ai