diff options
Diffstat (limited to 'gnu/llvm/libcxx/src/support')
19 files changed, 1650 insertions, 0 deletions
diff --git a/gnu/llvm/libcxx/src/support/runtime/exception_fallback.ipp b/gnu/llvm/libcxx/src/support/runtime/exception_fallback.ipp new file mode 100644 index 00000000000..376a0381f54 --- /dev/null +++ b/gnu/llvm/libcxx/src/support/runtime/exception_fallback.ipp @@ -0,0 +1,164 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, 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 +// +//===----------------------------------------------------------------------===// + +#include <cstdio> + +namespace std { + +_LIBCPP_SAFE_STATIC static std::terminate_handler __terminate_handler; +_LIBCPP_SAFE_STATIC static std::unexpected_handler __unexpected_handler; + + +// libcxxrt provides implementations of these functions itself. +unexpected_handler +set_unexpected(unexpected_handler func) _NOEXCEPT +{ + return __libcpp_atomic_exchange(&__unexpected_handler, func); +} + +unexpected_handler +get_unexpected() _NOEXCEPT +{ + return __libcpp_atomic_load(&__unexpected_handler); + +} + +_LIBCPP_NORETURN +void unexpected() +{ + (*get_unexpected())(); + // unexpected handler should not return + terminate(); +} + +terminate_handler +set_terminate(terminate_handler func) _NOEXCEPT +{ + return __libcpp_atomic_exchange(&__terminate_handler, func); +} + +terminate_handler +get_terminate() _NOEXCEPT +{ + return __libcpp_atomic_load(&__terminate_handler); +} + +#ifndef __EMSCRIPTEN__ // We provide this in JS +_LIBCPP_NORETURN +void +terminate() _NOEXCEPT +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + (*get_terminate())(); + // handler should not return + fprintf(stderr, "terminate_handler unexpectedly returned\n"); + ::abort(); +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + // handler should not throw exception + fprintf(stderr, "terminate_handler unexpectedly threw an exception\n"); + ::abort(); + } +#endif // _LIBCPP_NO_EXCEPTIONS +} +#endif // !__EMSCRIPTEN__ + +#if !defined(__EMSCRIPTEN__) +bool uncaught_exception() _NOEXCEPT { return uncaught_exceptions() > 0; } + +int uncaught_exceptions() _NOEXCEPT +{ +#warning uncaught_exception not yet implemented + fprintf(stderr, "uncaught_exceptions not yet implemented\n"); + ::abort(); +} +#endif // !__EMSCRIPTEN__ + + +exception::~exception() _NOEXCEPT +{ +} + +const char* exception::what() const _NOEXCEPT +{ + return "std::exception"; +} + +bad_exception::~bad_exception() _NOEXCEPT +{ +} + +const char* bad_exception::what() const _NOEXCEPT +{ + return "std::bad_exception"; +} + + +bad_alloc::bad_alloc() _NOEXCEPT +{ +} + +bad_alloc::~bad_alloc() _NOEXCEPT +{ +} + +const char* +bad_alloc::what() const _NOEXCEPT +{ + return "std::bad_alloc"; +} + +bad_array_new_length::bad_array_new_length() _NOEXCEPT +{ +} + +bad_array_new_length::~bad_array_new_length() _NOEXCEPT +{ +} + +const char* +bad_array_new_length::what() const _NOEXCEPT +{ + return "bad_array_new_length"; +} + +bad_cast::bad_cast() _NOEXCEPT +{ +} + +bad_typeid::bad_typeid() _NOEXCEPT +{ +} + +bad_cast::~bad_cast() _NOEXCEPT +{ +} + +const char* +bad_cast::what() const _NOEXCEPT +{ + return "std::bad_cast"; +} + +bad_typeid::~bad_typeid() _NOEXCEPT +{ +} + +const char* +bad_typeid::what() const _NOEXCEPT +{ + return "std::bad_typeid"; +} + +} // namespace std diff --git a/gnu/llvm/libcxx/src/support/runtime/exception_glibcxx.ipp b/gnu/llvm/libcxx/src/support/runtime/exception_glibcxx.ipp new file mode 100644 index 00000000000..4bf3c4d8775 --- /dev/null +++ b/gnu/llvm/libcxx/src/support/runtime/exception_glibcxx.ipp @@ -0,0 +1,32 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, 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 __GLIBCXX__ +#error header can only be used when targeting libstdc++ or libsupc++ +#endif + +namespace std { + +bad_alloc::bad_alloc() _NOEXCEPT +{ +} + +bad_array_new_length::bad_array_new_length() _NOEXCEPT +{ +} + +bad_cast::bad_cast() _NOEXCEPT +{ +} + +bad_typeid::bad_typeid() _NOEXCEPT +{ +} + +} // namespace std diff --git a/gnu/llvm/libcxx/src/support/runtime/exception_libcxxabi.ipp b/gnu/llvm/libcxx/src/support/runtime/exception_libcxxabi.ipp new file mode 100644 index 00000000000..6bc049bf382 --- /dev/null +++ b/gnu/llvm/libcxx/src/support/runtime/exception_libcxxabi.ipp @@ -0,0 +1,27 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, 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 _LIBCPPABI_VERSION +#error this header can only be used with libc++abi +#endif + +namespace std { + +bool uncaught_exception() _NOEXCEPT { return uncaught_exceptions() > 0; } + +int uncaught_exceptions() _NOEXCEPT +{ +# if _LIBCPPABI_VERSION > 1001 + return __cxa_uncaught_exceptions(); +# else + return __cxa_uncaught_exception() ? 1 : 0; +# endif +} + +} // namespace std diff --git a/gnu/llvm/libcxx/src/support/runtime/exception_libcxxrt.ipp b/gnu/llvm/libcxx/src/support/runtime/exception_libcxxrt.ipp new file mode 100644 index 00000000000..0ebffdedba9 --- /dev/null +++ b/gnu/llvm/libcxx/src/support/runtime/exception_libcxxrt.ipp @@ -0,0 +1,25 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, 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 LIBCXXRT +#error this header may only be used when targeting libcxxrt +#endif + +namespace std { + +bad_exception::~bad_exception() _NOEXCEPT +{ +} + +const char* bad_exception::what() const _NOEXCEPT +{ + return "std::bad_exception"; +} + +} // namespace std diff --git a/gnu/llvm/libcxx/src/support/runtime/exception_msvc.ipp b/gnu/llvm/libcxx/src/support/runtime/exception_msvc.ipp new file mode 100644 index 00000000000..7315b8261b7 --- /dev/null +++ b/gnu/llvm/libcxx/src/support/runtime/exception_msvc.ipp @@ -0,0 +1,163 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, 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 _LIBCPP_ABI_MICROSOFT +#error this header can only be used when targeting the MSVC ABI +#endif + +#include <stdio.h> +#include <stdlib.h> + +extern "C" { +typedef void (__cdecl* terminate_handler)(); +_LIBCPP_CRT_FUNC terminate_handler __cdecl set_terminate( + terminate_handler _NewTerminateHandler) throw(); +_LIBCPP_CRT_FUNC terminate_handler __cdecl _get_terminate(); + +typedef void (__cdecl* unexpected_handler)(); +unexpected_handler __cdecl set_unexpected( + unexpected_handler _NewUnexpectedHandler) throw(); +unexpected_handler __cdecl _get_unexpected(); + +int __cdecl __uncaught_exceptions(); +} + +namespace std { + +unexpected_handler +set_unexpected(unexpected_handler func) _NOEXCEPT { + return ::set_unexpected(func); +} + +unexpected_handler get_unexpected() _NOEXCEPT { + return ::_get_unexpected(); +} + +_LIBCPP_NORETURN +void unexpected() { + (*get_unexpected())(); + // unexpected handler should not return + terminate(); +} + +terminate_handler set_terminate(terminate_handler func) _NOEXCEPT { + return ::set_terminate(func); +} + +terminate_handler get_terminate() _NOEXCEPT { + return ::_get_terminate(); +} + +_LIBCPP_NORETURN +void terminate() _NOEXCEPT +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + (*get_terminate())(); + // handler should not return + fprintf(stderr, "terminate_handler unexpectedly returned\n"); + ::abort(); +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + // handler should not throw exception + fprintf(stderr, "terminate_handler unexpectedly threw an exception\n"); + ::abort(); + } +#endif // _LIBCPP_NO_EXCEPTIONS +} + +bool uncaught_exception() _NOEXCEPT { return uncaught_exceptions() > 0; } + +int uncaught_exceptions() _NOEXCEPT { + return __uncaught_exceptions(); +} + +#if !defined(_LIBCPP_ABI_VCRUNTIME) +bad_cast::bad_cast() _NOEXCEPT +{ +} + +bad_cast::~bad_cast() _NOEXCEPT +{ +} + +const char * +bad_cast::what() const _NOEXCEPT +{ + return "std::bad_cast"; +} + +bad_typeid::bad_typeid() _NOEXCEPT +{ +} + +bad_typeid::~bad_typeid() _NOEXCEPT +{ +} + +const char * +bad_typeid::what() const _NOEXCEPT +{ + return "std::bad_typeid"; +} + +exception::~exception() _NOEXCEPT +{ +} + +const char* exception::what() const _NOEXCEPT +{ + return "std::exception"; +} + + +bad_exception::~bad_exception() _NOEXCEPT +{ +} + +const char* bad_exception::what() const _NOEXCEPT +{ + return "std::bad_exception"; +} + + +bad_alloc::bad_alloc() _NOEXCEPT +{ +} + +bad_alloc::~bad_alloc() _NOEXCEPT +{ +} + +const char* +bad_alloc::what() const _NOEXCEPT +{ + return "std::bad_alloc"; +} + +bad_array_new_length::bad_array_new_length() _NOEXCEPT +{ +} + +bad_array_new_length::~bad_array_new_length() _NOEXCEPT +{ +} + +const char* +bad_array_new_length::what() const _NOEXCEPT +{ + return "bad_array_new_length"; +} +#endif // !_LIBCPP_ABI_VCRUNTIME + +} // namespace std diff --git a/gnu/llvm/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp b/gnu/llvm/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp new file mode 100644 index 00000000000..82a8e6972fa --- /dev/null +++ b/gnu/llvm/libcxx/src/support/runtime/exception_pointer_cxxabi.ipp @@ -0,0 +1,73 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, 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 HAVE_DEPENDENT_EH_ABI +#error this header may only be used with libc++abi or libcxxrt +#endif + +namespace std { + +exception_ptr::~exception_ptr() _NOEXCEPT { + __cxa_decrement_exception_refcount(__ptr_); +} + +exception_ptr::exception_ptr(const exception_ptr& other) _NOEXCEPT + : __ptr_(other.__ptr_) +{ + __cxa_increment_exception_refcount(__ptr_); +} + +exception_ptr& exception_ptr::operator=(const exception_ptr& other) _NOEXCEPT +{ + if (__ptr_ != other.__ptr_) + { + __cxa_increment_exception_refcount(other.__ptr_); + __cxa_decrement_exception_refcount(__ptr_); + __ptr_ = other.__ptr_; + } + return *this; +} + +nested_exception::nested_exception() _NOEXCEPT + : __ptr_(current_exception()) +{ +} + +nested_exception::~nested_exception() _NOEXCEPT +{ +} + +_LIBCPP_NORETURN +void +nested_exception::rethrow_nested() const +{ + if (__ptr_ == nullptr) + terminate(); + rethrow_exception(__ptr_); +} + +exception_ptr current_exception() _NOEXCEPT +{ + // be nicer if there was a constructor that took a ptr, then + // this whole function would be just: + // return exception_ptr(__cxa_current_primary_exception()); + exception_ptr ptr; + ptr.__ptr_ = __cxa_current_primary_exception(); + return ptr; +} + +_LIBCPP_NORETURN +void rethrow_exception(exception_ptr p) +{ + __cxa_rethrow_primary_exception(p.__ptr_); + // if p.__ptr_ is NULL, above returns so we terminate + terminate(); +} + +} // namespace std diff --git a/gnu/llvm/libcxx/src/support/runtime/exception_pointer_glibcxx.ipp b/gnu/llvm/libcxx/src/support/runtime/exception_pointer_glibcxx.ipp new file mode 100644 index 00000000000..3abc137c67b --- /dev/null +++ b/gnu/llvm/libcxx/src/support/runtime/exception_pointer_glibcxx.ipp @@ -0,0 +1,77 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, 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 +// +//===----------------------------------------------------------------------===// + +// libsupc++ does not implement the dependent EH ABI and the functionality +// it uses to implement std::exception_ptr (which it declares as an alias of +// std::__exception_ptr::exception_ptr) is not directly exported to clients. So +// we have little choice but to hijack std::__exception_ptr::exception_ptr's +// (which fortunately has the same layout as our std::exception_ptr) copy +// constructor, assignment operator and destructor (which are part of its +// stable ABI), and its rethrow_exception(std::__exception_ptr::exception_ptr) +// function. + +namespace std { + +namespace __exception_ptr +{ + +struct exception_ptr +{ + void* __ptr_; + + exception_ptr(const exception_ptr&) _NOEXCEPT; + exception_ptr& operator=(const exception_ptr&) _NOEXCEPT; + ~exception_ptr() _NOEXCEPT; +}; + +} + +_LIBCPP_NORETURN void rethrow_exception(__exception_ptr::exception_ptr); + +exception_ptr::~exception_ptr() _NOEXCEPT +{ + reinterpret_cast<__exception_ptr::exception_ptr*>(this)->~exception_ptr(); +} + +exception_ptr::exception_ptr(const exception_ptr& other) _NOEXCEPT + : __ptr_(other.__ptr_) +{ + new (reinterpret_cast<void*>(this)) __exception_ptr::exception_ptr( + reinterpret_cast<const __exception_ptr::exception_ptr&>(other)); +} + +exception_ptr& exception_ptr::operator=(const exception_ptr& other) _NOEXCEPT +{ + *reinterpret_cast<__exception_ptr::exception_ptr*>(this) = + reinterpret_cast<const __exception_ptr::exception_ptr&>(other); + return *this; +} + +nested_exception::nested_exception() _NOEXCEPT + : __ptr_(current_exception()) +{ +} + + +_LIBCPP_NORETURN +void +nested_exception::rethrow_nested() const +{ + if (__ptr_ == nullptr) + terminate(); + rethrow_exception(__ptr_); +} + +_LIBCPP_NORETURN +void rethrow_exception(exception_ptr p) +{ + rethrow_exception(reinterpret_cast<__exception_ptr::exception_ptr&>(p)); +} + +} // namespace std diff --git a/gnu/llvm/libcxx/src/support/runtime/exception_pointer_msvc.ipp b/gnu/llvm/libcxx/src/support/runtime/exception_pointer_msvc.ipp new file mode 100644 index 00000000000..b1a36952232 --- /dev/null +++ b/gnu/llvm/libcxx/src/support/runtime/exception_pointer_msvc.ipp @@ -0,0 +1,86 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, 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 +// +//===----------------------------------------------------------------------===// + +#include <stdio.h> +#include <stdlib.h> + +_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrCreate(void*); +_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrDestroy(void*); +_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrCopy(void*, const void*); +_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrAssign(void*, const void*); +_LIBCPP_CRT_FUNC bool __cdecl __ExceptionPtrCompare(const void*, const void*); +_LIBCPP_CRT_FUNC bool __cdecl __ExceptionPtrToBool(const void*); +_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrSwap(void*, void*); +_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrCurrentException(void*); +[[noreturn]] _LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrRethrow(const void*); +_LIBCPP_CRT_FUNC void __cdecl +__ExceptionPtrCopyException(void*, const void*, const void*); + +namespace std { + +exception_ptr::exception_ptr() _NOEXCEPT { __ExceptionPtrCreate(this); } +exception_ptr::exception_ptr(nullptr_t) _NOEXCEPT { __ExceptionPtrCreate(this); } + +exception_ptr::exception_ptr(const exception_ptr& __other) _NOEXCEPT { + __ExceptionPtrCopy(this, &__other); +} +exception_ptr& exception_ptr::operator=(const exception_ptr& __other) _NOEXCEPT { + __ExceptionPtrAssign(this, &__other); + return *this; +} + +exception_ptr& exception_ptr::operator=(nullptr_t) _NOEXCEPT { + exception_ptr dummy; + __ExceptionPtrAssign(this, &dummy); + return *this; +} + +exception_ptr::~exception_ptr() _NOEXCEPT { __ExceptionPtrDestroy(this); } + +exception_ptr::operator bool() const _NOEXCEPT { + return __ExceptionPtrToBool(this); +} + +bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT { + return __ExceptionPtrCompare(&__x, &__y); +} + + +void swap(exception_ptr& lhs, exception_ptr& rhs) _NOEXCEPT { + __ExceptionPtrSwap(&rhs, &lhs); +} + +exception_ptr __copy_exception_ptr(void* __except, const void* __ptr) { + exception_ptr __ret = nullptr; + if (__ptr) + __ExceptionPtrCopyException(&__ret, __except, __ptr); + return __ret; +} + +exception_ptr current_exception() _NOEXCEPT { + exception_ptr __ret; + __ExceptionPtrCurrentException(&__ret); + return __ret; +} + +_LIBCPP_NORETURN +void rethrow_exception(exception_ptr p) { __ExceptionPtrRethrow(&p); } + +nested_exception::nested_exception() _NOEXCEPT : __ptr_(current_exception()) {} + +nested_exception::~nested_exception() _NOEXCEPT {} + +_LIBCPP_NORETURN +void nested_exception::rethrow_nested() const { + if (__ptr_ == nullptr) + terminate(); + rethrow_exception(__ptr_); +} + +} // namespace std diff --git a/gnu/llvm/libcxx/src/support/runtime/exception_pointer_unimplemented.ipp b/gnu/llvm/libcxx/src/support/runtime/exception_pointer_unimplemented.ipp new file mode 100644 index 00000000000..991bca9ecfe --- /dev/null +++ b/gnu/llvm/libcxx/src/support/runtime/exception_pointer_unimplemented.ipp @@ -0,0 +1,79 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, 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 +// +//===----------------------------------------------------------------------===// + +#include <stdio.h> +#include <stdlib.h> + +namespace std { + +exception_ptr::~exception_ptr() _NOEXCEPT +{ +# warning exception_ptr not yet implemented + fprintf(stderr, "exception_ptr not yet implemented\n"); + ::abort(); +} + +exception_ptr::exception_ptr(const exception_ptr& other) _NOEXCEPT + : __ptr_(other.__ptr_) +{ +# warning exception_ptr not yet implemented + fprintf(stderr, "exception_ptr not yet implemented\n"); + ::abort(); +} + +exception_ptr& exception_ptr::operator=(const exception_ptr& other) _NOEXCEPT +{ +# warning exception_ptr not yet implemented + fprintf(stderr, "exception_ptr not yet implemented\n"); + ::abort(); +} + +nested_exception::nested_exception() _NOEXCEPT + : __ptr_(current_exception()) +{ +} + +#if !defined(__GLIBCXX__) + +nested_exception::~nested_exception() _NOEXCEPT +{ +} + +#endif + +_LIBCPP_NORETURN +void +nested_exception::rethrow_nested() const +{ +# warning exception_ptr not yet implemented + fprintf(stderr, "exception_ptr not yet implemented\n"); + ::abort(); +#if 0 + if (__ptr_ == nullptr) + terminate(); + rethrow_exception(__ptr_); +#endif // FIXME +} + +exception_ptr current_exception() _NOEXCEPT +{ +# warning exception_ptr not yet implemented + fprintf(stderr, "exception_ptr not yet implemented\n"); + ::abort(); +} + +_LIBCPP_NORETURN +void rethrow_exception(exception_ptr p) +{ +# warning exception_ptr not yet implemented + fprintf(stderr, "exception_ptr not yet implemented\n"); + ::abort(); +} + +} // namespace std diff --git a/gnu/llvm/libcxx/src/support/runtime/new_handler_fallback.ipp b/gnu/llvm/libcxx/src/support/runtime/new_handler_fallback.ipp new file mode 100644 index 00000000000..72050939429 --- /dev/null +++ b/gnu/llvm/libcxx/src/support/runtime/new_handler_fallback.ipp @@ -0,0 +1,26 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, 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 +// +//===----------------------------------------------------------------------===// + +namespace std { + +_LIBCPP_SAFE_STATIC static std::new_handler __new_handler; + +new_handler +set_new_handler(new_handler handler) _NOEXCEPT +{ + return __libcpp_atomic_exchange(&__new_handler, handler); +} + +new_handler +get_new_handler() _NOEXCEPT +{ + return __libcpp_atomic_load(&__new_handler); +} + +} // namespace std diff --git a/gnu/llvm/libcxx/src/support/runtime/stdexcept_default.ipp b/gnu/llvm/libcxx/src/support/runtime/stdexcept_default.ipp new file mode 100644 index 00000000000..c827ca4c513 --- /dev/null +++ b/gnu/llvm/libcxx/src/support/runtime/stdexcept_default.ipp @@ -0,0 +1,64 @@ +//===--------------------- stdexcept_default.ipp --------------------------===// +// +// Part of the LLVM Project, 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 +// +//===----------------------------------------------------------------------===// + +#include "../../include/refstring.h" + +/* For _LIBCPPABI_VERSION */ +#if !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY) && \ + (defined(LIBCXX_BUILDING_LIBCXXABI) || defined(LIBCXXRT)) +#include <cxxabi.h> +#endif + +static_assert(sizeof(std::__libcpp_refstring) == sizeof(const char*), ""); + +namespace std // purposefully not using versioning namespace +{ + +logic_error::logic_error(const string& msg) : __imp_(msg.c_str()) {} + +logic_error::logic_error(const char* msg) : __imp_(msg) {} + +logic_error::logic_error(const logic_error& le) _NOEXCEPT : __imp_(le.__imp_) {} + +logic_error& logic_error::operator=(const logic_error& le) _NOEXCEPT { + __imp_ = le.__imp_; + return *this; +} + +runtime_error::runtime_error(const string& msg) : __imp_(msg.c_str()) {} + +runtime_error::runtime_error(const char* msg) : __imp_(msg) {} + +runtime_error::runtime_error(const runtime_error& re) _NOEXCEPT + : __imp_(re.__imp_) {} + +runtime_error& runtime_error::operator=(const runtime_error& re) _NOEXCEPT { + __imp_ = re.__imp_; + return *this; +} + +#if !defined(_LIBCPPABI_VERSION) && !defined(LIBSTDCXX) + +const char* logic_error::what() const _NOEXCEPT { return __imp_.c_str(); } + +const char* runtime_error::what() const _NOEXCEPT { return __imp_.c_str(); } + +logic_error::~logic_error() _NOEXCEPT {} +domain_error::~domain_error() _NOEXCEPT {} +invalid_argument::~invalid_argument() _NOEXCEPT {} +length_error::~length_error() _NOEXCEPT {} +out_of_range::~out_of_range() _NOEXCEPT {} + +runtime_error::~runtime_error() _NOEXCEPT {} +range_error::~range_error() _NOEXCEPT {} +overflow_error::~overflow_error() _NOEXCEPT {} +underflow_error::~underflow_error() _NOEXCEPT {} + +#endif + +} // namespace std diff --git a/gnu/llvm/libcxx/src/support/runtime/stdexcept_vcruntime.ipp b/gnu/llvm/libcxx/src/support/runtime/stdexcept_vcruntime.ipp new file mode 100644 index 00000000000..94eed465ae9 --- /dev/null +++ b/gnu/llvm/libcxx/src/support/runtime/stdexcept_vcruntime.ipp @@ -0,0 +1,16 @@ +//===------------------- stdexcept_vcruntime.ipp --------------------------===// +// +// Part of the LLVM Project, 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 _LIBCPP_ABI_VCRUNTIME +#error This file may only be used when defering to vcruntime +#endif + +namespace std { +logic_error::logic_error(std::string const& s) : exception(s.c_str()) {} +runtime_error::runtime_error(std::string const& s) : exception(s.c_str()) {} +} // namespace std diff --git a/gnu/llvm/libcxx/src/support/solaris/README b/gnu/llvm/libcxx/src/support/solaris/README new file mode 100644 index 00000000000..89c887a3b4a --- /dev/null +++ b/gnu/llvm/libcxx/src/support/solaris/README @@ -0,0 +1,4 @@ +This directory contains a partial implementation of the xlocale APIs for +Solaris. Some portions are lifted from FreeBSD libc, and so are covered by a +2-clause BSD license instead of the MIT/UUIC license that the rest of libc++ is +distributed under. diff --git a/gnu/llvm/libcxx/src/support/solaris/mbsnrtowcs.inc b/gnu/llvm/libcxx/src/support/solaris/mbsnrtowcs.inc new file mode 100644 index 00000000000..074045277ca --- /dev/null +++ b/gnu/llvm/libcxx/src/support/solaris/mbsnrtowcs.inc @@ -0,0 +1,76 @@ + + +/*- + * As noted in the source, some portions of this implementation are copied from + * FreeBSD libc. These are covered by the following copyright: + * + * Copyright (c) 2002-2004 Tim J. Robbins. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +size_t +mbsnrtowcs_l(wchar_t * __restrict dst, const char ** __restrict src, + size_t nms, size_t len, mbstate_t * __restrict ps, locale_t loc) +{ + const char *s; + size_t nchr; + wchar_t wc; + size_t nb; + FIX_LOCALE(loc); + + s = *src; + nchr = 0; + + if (dst == NULL) { + for (;;) { + if ((nb = mbrtowc_l(&wc, s, nms, ps, loc)) == (size_t)-1) + /* Invalid sequence - mbrtowc() sets errno. */ + return ((size_t)-1); + else if (nb == 0 || nb == (size_t)-2) + return (nchr); + s += nb; + nms -= nb; + nchr++; + } + /*NOTREACHED*/ + } + + while (len-- > 0) { + if ((nb = mbrtowc_l(dst, s, nms, ps, loc)) == (size_t)-1) { + *src = s; + return ((size_t)-1); + } else if (nb == (size_t)-2) { + *src = s + nms; + return (nchr); + } else if (nb == 0) { + *src = NULL; + return (nchr); + } + s += nb; + nms -= nb; + nchr++; + dst++; + } + *src = s; + return (nchr); +} diff --git a/gnu/llvm/libcxx/src/support/solaris/wcsnrtombs.inc b/gnu/llvm/libcxx/src/support/solaris/wcsnrtombs.inc new file mode 100644 index 00000000000..239079e4719 --- /dev/null +++ b/gnu/llvm/libcxx/src/support/solaris/wcsnrtombs.inc @@ -0,0 +1,92 @@ +/*- + * Copyright (c) 2002-2004 Tim J. Robbins. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + + +size_t +wcsnrtombs_l(char * __restrict dst, const wchar_t ** __restrict src, + size_t nwc, size_t len, mbstate_t * __restrict ps, locale_t loc) +{ + FIX_LOCALE(loc); + mbstate_t mbsbak; + char buf[MB_CUR_MAX_L(loc)]; + const wchar_t *s; + size_t nbytes; + size_t nb; + + s = *src; + nbytes = 0; + + if (dst == NULL) { + while (nwc-- > 0) { + if ((nb = wcrtomb_l(buf, *s, ps, loc)) == (size_t)-1) + /* Invalid character - wcrtomb() sets errno. */ + return ((size_t)-1); + else if (*s == L'\0') + return (nbytes + nb - 1); + s++; + nbytes += nb; + } + return (nbytes); + } + + while (len > 0 && nwc-- > 0) { + if (len > (size_t)MB_CUR_MAX_L(loc)) { + /* Enough space to translate in-place. */ + if ((nb = wcrtomb_l(dst, *s, ps, loc)) == (size_t)-1) { + *src = s; + return ((size_t)-1); + } + } else { + /* + * May not be enough space; use temp. buffer. + * + * We need to save a copy of the conversion state + * here so we can restore it if the multibyte + * character is too long for the buffer. + */ + mbsbak = *ps; + if ((nb = wcrtomb_l(buf, *s, ps, loc)) == (size_t)-1) { + *src = s; + return ((size_t)-1); + } + if (nb > (int)len) { + /* MB sequence for character won't fit. */ + *ps = mbsbak; + break; + } + memcpy(dst, buf, nb); + } + if (*s == L'\0') { + *src = NULL; + return (nbytes + nb - 1); + } + s++; + dst += nb; + len -= nb; + nbytes += nb; + } + *src = s; + return (nbytes); +} diff --git a/gnu/llvm/libcxx/src/support/solaris/xlocale.cpp b/gnu/llvm/libcxx/src/support/solaris/xlocale.cpp new file mode 100644 index 00000000000..d68a39f4dfe --- /dev/null +++ b/gnu/llvm/libcxx/src/support/solaris/xlocale.cpp @@ -0,0 +1,68 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, 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 +// +//===----------------------------------------------------------------------===// + +#ifdef __sun__ + +#include "support/solaris/xlocale.h" +#include <stdarg.h> +#include <stdio.h> +#include <sys/localedef.h> + +extern "C" { + +int isxdigit_l(int __c, locale_t __l) { + return isxdigit(__c); +} + +int iswxdigit_l(wint_t __c, locale_t __l) { + return isxdigit(__c); +} + +// FIXME: This disregards the locale, which is Very Wrong +#define vsnprintf_l(__s, __n, __l, __format, __va) \ + vsnprintf(__s, __n, __format, __va) + +int snprintf_l(char *__s, size_t __n, locale_t __l, const char *__format, ...) +{ + va_list __va; + va_start(__va, __format); + int __res = vsnprintf_l(__s, __n , __l, __format, __va); + va_end(__va); + return __res; +} + +int asprintf_l(char **__s, locale_t __l, const char *__format, ...) { + va_list __va; + va_start(__va, __format); + // FIXME: + int __res = vasprintf(__s, __format, __va); + va_end(__va); + return __res; +} + +int sscanf_l(const char *__s, locale_t __l, const char *__format, ...) { + va_list __va; + va_start(__va, __format); + // FIXME: + int __res = vsscanf(__s, __format, __va); + va_end(__va); + return __res; +} + +size_t mbrtowc_l(wchar_t *__pwc, const char *__pmb, + size_t __max, mbstate_t *__ps, locale_t __loc) { + return mbrtowc(__pwc, __pmb, __max, __ps); +} + +struct lconv *localeconv_l(locale_t __l) { + return localeconv(); +} + +}; + +#endif // __sun__ diff --git a/gnu/llvm/libcxx/src/support/win32/locale_win32.cpp b/gnu/llvm/libcxx/src/support/win32/locale_win32.cpp new file mode 100644 index 00000000000..b7062db352a --- /dev/null +++ b/gnu/llvm/libcxx/src/support/win32/locale_win32.cpp @@ -0,0 +1,139 @@ +// -*- C++ -*- +//===-------------------- support/win32/locale_win32.cpp ------------------===// +// +// Part of the LLVM Project, 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 +// +//===----------------------------------------------------------------------===// + +#include <locale> +#include <cstdarg> // va_start, va_end +#include <memory> +#include <type_traits> + +int __libcpp_vasprintf(char **sptr, const char *__restrict fmt, va_list ap); + +using std::__libcpp_locale_guard; + +// FIXME: base currently unused. Needs manual work to construct the new locale +locale_t newlocale( int mask, const char * locale, locale_t /*base*/ ) +{ + return {_create_locale( LC_ALL, locale ), locale}; +} + +decltype(MB_CUR_MAX) MB_CUR_MAX_L( locale_t __l ) +{ +#if defined(_LIBCPP_MSVCRT) + return ___mb_cur_max_l_func(__l); +#else + __libcpp_locale_guard __current(__l); + return MB_CUR_MAX; +#endif +} + +lconv *localeconv_l( locale_t &loc ) +{ + __libcpp_locale_guard __current(loc); + lconv *lc = localeconv(); + if (!lc) + return lc; + return loc.__store_lconv(lc); +} +size_t mbrlen_l( const char *__restrict s, size_t n, + mbstate_t *__restrict ps, locale_t loc ) +{ + __libcpp_locale_guard __current(loc); + return mbrlen( s, n, ps ); +} +size_t mbsrtowcs_l( wchar_t *__restrict dst, const char **__restrict src, + size_t len, mbstate_t *__restrict ps, locale_t loc ) +{ + __libcpp_locale_guard __current(loc); + return mbsrtowcs( dst, src, len, ps ); +} +size_t wcrtomb_l( char *__restrict s, wchar_t wc, mbstate_t *__restrict ps, + locale_t loc ) +{ + __libcpp_locale_guard __current(loc); + return wcrtomb( s, wc, ps ); +} +size_t mbrtowc_l( wchar_t *__restrict pwc, const char *__restrict s, + size_t n, mbstate_t *__restrict ps, locale_t loc ) +{ + __libcpp_locale_guard __current(loc); + return mbrtowc( pwc, s, n, ps ); +} +size_t mbsnrtowcs_l( wchar_t *__restrict dst, const char **__restrict src, + size_t nms, size_t len, mbstate_t *__restrict ps, locale_t loc ) +{ + __libcpp_locale_guard __current(loc); + return mbsnrtowcs( dst, src, nms, len, ps ); +} +size_t wcsnrtombs_l( char *__restrict dst, const wchar_t **__restrict src, + size_t nwc, size_t len, mbstate_t *__restrict ps, locale_t loc ) +{ + __libcpp_locale_guard __current(loc); + return wcsnrtombs( dst, src, nwc, len, ps ); +} +wint_t btowc_l( int c, locale_t loc ) +{ + __libcpp_locale_guard __current(loc); + return btowc( c ); +} +int wctob_l( wint_t c, locale_t loc ) +{ + __libcpp_locale_guard __current(loc); + return wctob( c ); +} + +int snprintf_l(char *ret, size_t n, locale_t loc, const char *format, ...) +{ + va_list ap; + va_start( ap, format ); +#if defined(_LIBCPP_MSVCRT) + // FIXME: Remove usage of internal CRT function and globals. + int result = __stdio_common_vsprintf( + _CRT_INTERNAL_LOCAL_PRINTF_OPTIONS | _CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR, + ret, n, format, loc, ap); +#else + __libcpp_locale_guard __current(loc); + int result = vsnprintf( ret, n, format, ap ); +#endif + va_end(ap); + return result; +} + +int asprintf_l( char **ret, locale_t loc, const char *format, ... ) +{ + va_list ap; + va_start( ap, format ); + int result = vasprintf_l( ret, loc, format, ap ); + va_end(ap); + return result; +} +int vasprintf_l( char **ret, locale_t loc, const char *format, va_list ap ) +{ + __libcpp_locale_guard __current(loc); + return __libcpp_vasprintf( ret, format, ap ); +} + +#if !defined(_LIBCPP_MSVCRT) +float strtof_l(const char* nptr, char** endptr, locale_t loc) { + __libcpp_locale_guard __current(loc); + return strtof(nptr, endptr); +} + +long double strtold_l(const char* nptr, char** endptr, locale_t loc) { + __libcpp_locale_guard __current(loc); + return strtold(nptr, endptr); +} +#endif + +#if defined(__MINGW32__) && __MSVCRT_VERSION__ < 0x0800 +size_t strftime_l(char *ret, size_t n, const char *format, const struct tm *tm, + locale_t loc) { + __libcpp_locale_guard __current(loc); + return strftime(ret, n, format, tm); +} +#endif diff --git a/gnu/llvm/libcxx/src/support/win32/support.cpp b/gnu/llvm/libcxx/src/support/win32/support.cpp new file mode 100644 index 00000000000..d156e02e3e8 --- /dev/null +++ b/gnu/llvm/libcxx/src/support/win32/support.cpp @@ -0,0 +1,164 @@ +// -*- C++ -*- +//===----------------------- support/win32/support.h ----------------------===// +// +// Part of the LLVM Project, 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 +// +//===----------------------------------------------------------------------===// + +#include <cstdarg> // va_start, va_end +#include <cstddef> // size_t +#include <cstdlib> // malloc +#include <cstdio> // vsprintf, vsnprintf +#include <cstring> // strcpy, wcsncpy +#include <cwchar> // mbstate_t + + +// Like sprintf, but when return value >= 0 it returns +// a pointer to a malloc'd string in *sptr. +// If return >= 0, use free to delete *sptr. +int __libcpp_vasprintf( char **sptr, const char *__restrict format, va_list ap ) +{ + *sptr = NULL; + // Query the count required. + int count = _vsnprintf( NULL, 0, format, ap ); + if (count < 0) + return count; + size_t buffer_size = static_cast<size_t>(count) + 1; + char* p = static_cast<char*>(malloc(buffer_size)); + if ( ! p ) + return -1; + // If we haven't used exactly what was required, something is wrong. + // Maybe bug in vsnprintf. Report the error and return. + if (_vsnprintf(p, buffer_size, format, ap) != count) { + free(p); + return -1; + } + // All good. This is returning memory to the caller not freeing it. + *sptr = p; + return count; +} + +// Returns >= 0: the number of wide characters found in the +// multi byte sequence src (of src_size_bytes), that fit in the buffer dst +// (of max_dest_chars elements size). The count returned excludes the +// null terminator. When dst is NULL, no characters are copied +// and no "out" parameters are updated. +// Returns (size_t) -1: an incomplete sequence encountered. +// Leaves *src pointing the next character to convert or NULL +// if a null character was converted from *src. +size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src, + size_t src_size_bytes, size_t max_dest_chars, mbstate_t *__restrict ps ) +{ + const size_t terminated_sequence = static_cast<size_t>(0); + //const size_t invalid_sequence = static_cast<size_t>(-1); + const size_t incomplete_sequence = static_cast< size_t>(-2); + + size_t dest_converted = 0; + size_t source_converted = 0; + size_t source_remaining = src_size_bytes; + size_t result = 0; + bool have_result = false; + + // If dst is null then max_dest_chars should be ignored according to the + // standard. Setting max_dest_chars to a large value has this effect. + if (!dst) + max_dest_chars = static_cast<size_t>(-1); + + while ( source_remaining ) { + if ( dst && dest_converted >= max_dest_chars ) + break; + // Converts one multi byte character. + // if result > 0, it's the size in bytes of that character. + // othewise if result is zero it indicates the null character has been found. + // otherwise it's an error and errno may be set. + size_t char_size = mbrtowc( dst ? dst + dest_converted : NULL, *src + source_converted, source_remaining, ps ); + // Don't do anything to change errno from here on. + if ( char_size > 0 ) { + source_remaining -= char_size; + source_converted += char_size; + ++dest_converted; + continue; + } + result = char_size; + have_result = true; + break; + } + if ( dst ) { + if ( have_result && result == terminated_sequence ) + *src = NULL; + else + *src += source_converted; + } + if ( have_result && result != terminated_sequence && result != incomplete_sequence ) + return static_cast<size_t>(-1); + + return dest_converted; +} + +// Converts max_source_chars from the wide character buffer pointer to by *src, +// into the multi byte character sequence buffer stored at dst which must be +// dst_size_bytes bytes in size. +// Returns >= 0: the number of bytes in the sequence +// converted from *src, excluding the null terminator. +// Returns size_t(-1) if an error occurs, also sets errno. +// If dst is NULL dst_size_bytes is ignored and no bytes are copied to dst +// and no "out" parameters are updated. +size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src, + size_t max_source_chars, size_t dst_size_bytes, mbstate_t *__restrict ps ) +{ + //const size_t invalid_sequence = static_cast<size_t>(-1); + + size_t source_converted = 0; + size_t dest_converted = 0; + size_t dest_remaining = dst_size_bytes; + size_t char_size = 0; + const errno_t no_error = ( errno_t) 0; + errno_t result = ( errno_t ) 0; + bool have_result = false; + bool terminator_found = false; + + // If dst is null then dst_size_bytes should be ignored according to the + // standard. Setting dest_remaining to a large value has this effect. + if (!dst) + dest_remaining = static_cast<size_t>(-1); + + while ( source_converted != max_source_chars ) { + if ( ! dest_remaining ) + break; + wchar_t c = (*src)[source_converted]; + if ( dst ) + result = wcrtomb_s( &char_size, dst + dest_converted, dest_remaining, c, ps); + else + result = wcrtomb_s( &char_size, NULL, 0, c, ps); + // If result is zero there is no error and char_size contains the + // size of the multi-byte-sequence converted. + // Otherwise result indicates an errno type error. + if ( result == no_error ) { + if ( c == L'\0' ) { + terminator_found = true; + break; + } + ++source_converted; + if ( dst ) + dest_remaining -= char_size; + dest_converted += char_size; + continue; + } + have_result = true; + break; + } + if ( dst ) { + if ( terminator_found ) + *src = NULL; + else + *src = *src + source_converted; + } + if ( have_result && result != no_error ) { + errno = result; + return static_cast<size_t>(-1); + } + + return dest_converted; +} diff --git a/gnu/llvm/libcxx/src/support/win32/thread_win32.cpp b/gnu/llvm/libcxx/src/support/win32/thread_win32.cpp new file mode 100644 index 00000000000..83e7e9f6ce5 --- /dev/null +++ b/gnu/llvm/libcxx/src/support/win32/thread_win32.cpp @@ -0,0 +1,275 @@ +// -*- C++ -*- +//===-------------------- support/win32/thread_win32.cpp ------------------===// +// +// Part of the LLVM Project, 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 +// +//===----------------------------------------------------------------------===// + +#include <__threading_support> +#include <windows.h> +#include <process.h> +#include <fibersapi.h> + +_LIBCPP_BEGIN_NAMESPACE_STD + +static_assert(sizeof(__libcpp_mutex_t) == sizeof(SRWLOCK), ""); +static_assert(alignof(__libcpp_mutex_t) == alignof(SRWLOCK), ""); + +static_assert(sizeof(__libcpp_recursive_mutex_t) == sizeof(CRITICAL_SECTION), + ""); +static_assert(alignof(__libcpp_recursive_mutex_t) == alignof(CRITICAL_SECTION), + ""); + +static_assert(sizeof(__libcpp_condvar_t) == sizeof(CONDITION_VARIABLE), ""); +static_assert(alignof(__libcpp_condvar_t) == alignof(CONDITION_VARIABLE), ""); + +static_assert(sizeof(__libcpp_exec_once_flag) == sizeof(INIT_ONCE), ""); +static_assert(alignof(__libcpp_exec_once_flag) == alignof(INIT_ONCE), ""); + +static_assert(sizeof(__libcpp_thread_id) == sizeof(DWORD), ""); +static_assert(alignof(__libcpp_thread_id) == alignof(DWORD), ""); + +static_assert(sizeof(__libcpp_thread_t) == sizeof(HANDLE), ""); +static_assert(alignof(__libcpp_thread_t) == alignof(HANDLE), ""); + +static_assert(sizeof(__libcpp_tls_key) == sizeof(DWORD), ""); +static_assert(alignof(__libcpp_tls_key) == alignof(DWORD), ""); + +// Mutex +int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m) +{ + InitializeCriticalSection((LPCRITICAL_SECTION)__m); + return 0; +} + +int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m) +{ + EnterCriticalSection((LPCRITICAL_SECTION)__m); + return 0; +} + +bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m) +{ + return TryEnterCriticalSection((LPCRITICAL_SECTION)__m) != 0; +} + +int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m) +{ + LeaveCriticalSection((LPCRITICAL_SECTION)__m); + return 0; +} + +int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m) +{ + DeleteCriticalSection((LPCRITICAL_SECTION)__m); + return 0; +} + +int __libcpp_mutex_lock(__libcpp_mutex_t *__m) +{ + AcquireSRWLockExclusive((PSRWLOCK)__m); + return 0; +} + +bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m) +{ + return TryAcquireSRWLockExclusive((PSRWLOCK)__m) != 0; +} + +int __libcpp_mutex_unlock(__libcpp_mutex_t *__m) +{ + ReleaseSRWLockExclusive((PSRWLOCK)__m); + return 0; +} + +int __libcpp_mutex_destroy(__libcpp_mutex_t *__m) +{ + static_cast<void>(__m); + return 0; +} + +// Condition Variable +int __libcpp_condvar_signal(__libcpp_condvar_t *__cv) +{ + WakeConditionVariable((PCONDITION_VARIABLE)__cv); + return 0; +} + +int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv) +{ + WakeAllConditionVariable((PCONDITION_VARIABLE)__cv); + return 0; +} + +int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m) +{ + SleepConditionVariableSRW((PCONDITION_VARIABLE)__cv, (PSRWLOCK)__m, INFINITE, 0); + return 0; +} + +int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m, + __libcpp_timespec_t *__ts) +{ + using namespace _VSTD::chrono; + + auto duration = seconds(__ts->tv_sec) + nanoseconds(__ts->tv_nsec); + auto abstime = + system_clock::time_point(duration_cast<system_clock::duration>(duration)); + auto timeout_ms = duration_cast<milliseconds>(abstime - system_clock::now()); + + if (!SleepConditionVariableSRW((PCONDITION_VARIABLE)__cv, (PSRWLOCK)__m, + timeout_ms.count() > 0 ? timeout_ms.count() + : 0, + 0)) + { + auto __ec = GetLastError(); + return __ec == ERROR_TIMEOUT ? ETIMEDOUT : __ec; + } + return 0; +} + +int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv) +{ + static_cast<void>(__cv); + return 0; +} + +// Execute Once +static inline _LIBCPP_INLINE_VISIBILITY BOOL CALLBACK +__libcpp_init_once_execute_once_thunk(PINIT_ONCE __init_once, PVOID __parameter, + PVOID *__context) +{ + static_cast<void>(__init_once); + static_cast<void>(__context); + + void (*init_routine)(void) = reinterpret_cast<void (*)(void)>(__parameter); + init_routine(); + return TRUE; +} + +int __libcpp_execute_once(__libcpp_exec_once_flag *__flag, + void (*__init_routine)(void)) +{ + if (!InitOnceExecuteOnce((PINIT_ONCE)__flag, __libcpp_init_once_execute_once_thunk, + reinterpret_cast<void *>(__init_routine), NULL)) + return GetLastError(); + return 0; +} + +// Thread ID +bool __libcpp_thread_id_equal(__libcpp_thread_id __lhs, + __libcpp_thread_id __rhs) +{ + return __lhs == __rhs; +} + +bool __libcpp_thread_id_less(__libcpp_thread_id __lhs, __libcpp_thread_id __rhs) +{ + return __lhs < __rhs; +} + +// Thread +struct __libcpp_beginthreadex_thunk_data +{ + void *(*__func)(void *); + void *__arg; +}; + +static inline _LIBCPP_INLINE_VISIBILITY unsigned WINAPI +__libcpp_beginthreadex_thunk(void *__raw_data) +{ + auto *__data = + static_cast<__libcpp_beginthreadex_thunk_data *>(__raw_data); + auto *__func = __data->__func; + void *__arg = __data->__arg; + delete __data; + return static_cast<unsigned>(reinterpret_cast<uintptr_t>(__func(__arg))); +} + +bool __libcpp_thread_isnull(const __libcpp_thread_t *__t) { + return *__t == 0; +} + +int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *), + void *__arg) +{ + auto *__data = new __libcpp_beginthreadex_thunk_data; + __data->__func = __func; + __data->__arg = __arg; + + *__t = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, + __libcpp_beginthreadex_thunk, + __data, 0, nullptr)); + + if (*__t) + return 0; + return GetLastError(); +} + +__libcpp_thread_id __libcpp_thread_get_current_id() +{ + return GetCurrentThreadId(); +} + +__libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t) +{ + return GetThreadId(*__t); +} + +int __libcpp_thread_join(__libcpp_thread_t *__t) +{ + if (WaitForSingleObjectEx(*__t, INFINITE, FALSE) == WAIT_FAILED) + return GetLastError(); + if (!CloseHandle(*__t)) + return GetLastError(); + return 0; +} + +int __libcpp_thread_detach(__libcpp_thread_t *__t) +{ + if (!CloseHandle(*__t)) + return GetLastError(); + return 0; +} + +void __libcpp_thread_yield() +{ + SwitchToThread(); +} + +void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns) +{ + using namespace chrono; + // round-up to the nearest milisecond + milliseconds __ms = + duration_cast<milliseconds>(__ns + chrono::nanoseconds(999999)); + // FIXME(compnerd) this should be an alertable sleep (WFSO or SleepEx) + Sleep(__ms.count()); +} + +// Thread Local Storage +int __libcpp_tls_create(__libcpp_tls_key* __key, + void(_LIBCPP_TLS_DESTRUCTOR_CC* __at_exit)(void*)) +{ + DWORD index = FlsAlloc(__at_exit); + if (index == FLS_OUT_OF_INDEXES) + return GetLastError(); + *__key = index; + return 0; +} + +void *__libcpp_tls_get(__libcpp_tls_key __key) +{ + return FlsGetValue(__key); +} + +int __libcpp_tls_set(__libcpp_tls_key __key, void *__p) +{ + if (!FlsSetValue(__key, __p)) + return GetLastError(); + return 0; +} + +_LIBCPP_END_NAMESPACE_STD |