//===----------------------------------------------------------------------===//
//
// Part of libcu++, the C++ Standard Library for your entire system,
// under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCUDACXX_CUDA_UTIL
#define _LIBCUDACXX_CUDA_UTIL

#include "cuda/std/detail/__config"
#include "cuda/std/detail/__pragma_push"

#include <iosfwd>  // for string forward decl

#include <cuda/exception>

#ifdef _LIBCUDACXX_NO_EXCEPTIONS
#include <cstdlib>
#endif
#include "cuda/std/detail/__pragma_push"

#include <cuda_runtime_api.h>

_LIBCUDACXX_BEGIN_NAMESPACE_CUDA

namespace detail {
inline _LIBCUDACXX_INLINE_VISIBILITY
void __throw_on_cuda_error(::cudaError_t __error, char const *__msg) {
  if (::cudaSuccess != __error) {
    ::cudaGetLastError(); // Clear CUDA error state
#ifndef _LIBCUDACXX_NO_EXCEPTIONS
    throw cuda::cuda_error{__error, __msg};
#else
    ::std::abort();
#endif
  }
}

inline _LIBCUDACXX_INLINE_VISIBILITY
void __throw_on_cuda_error(::cudaError_t __error) {
  __throw_on_cuda_error(__error, "");
}

} // namespace detail

_LIBCUDACXX_END_NAMESPACE_CUDA

#include "../std/detail/__pragma_pop"

#endif // _LIBCUDACXX_CUDA_UTIL
