diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -657,7 +657,11 @@ __system_error/error_code.h __system_error/error_condition.h __system_error/system_error.h + __thread/formatter.h + __thread/jthread.h __thread/poll_with_backoff.h + __thread/this_thread.h + __thread/thread.h __thread/timed_backoff_policy.h __threading_support __tree diff --git a/libcxx/include/__stop_token/stop_state.h b/libcxx/include/__stop_token/stop_state.h --- a/libcxx/include/__stop_token/stop_state.h +++ b/libcxx/include/__stop_token/stop_state.h @@ -13,8 +13,8 @@ #include <__config> #include <__stop_token/atomic_unique_lock.h> #include <__stop_token/intrusive_list_view.h> +#include <__threading_support> #include -#include #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) # pragma GCC system_header @@ -61,7 +61,7 @@ using __callback_list = __intrusive_list_view<__stop_callback_base>; __callback_list __callback_list_; - std::thread::id __requesting_thread_; + __thread_id __requesting_thread_; public: _LIBCPP_HIDE_FROM_ABI __stop_state() noexcept = default; diff --git a/libcxx/include/__thread/formatter.h b/libcxx/include/__thread/formatter.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__thread/formatter.h @@ -0,0 +1,76 @@ +// -*- 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___THREAD_FORMATTER +#define _LIBCPP___THREAD_FORMATTER + +#include <__concepts/arithmetic.h> +#include <__config> +#include <__format/concepts.h> +#include <__format/format_parse_context.h> +#include <__format/formatter.h> +#include <__format/formatter_integral.h> +#include <__format/parser_std_format_spec.h> +#include <__threading_support> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#ifdef _LIBCPP_HAS_NO_THREADS +# error " is not supported since libc++ has been configured without support for threads." +#endif + +#if _LIBCPP_STD_VER >= 23 + +_LIBCPP_BEGIN_NAMESPACE_STD + +template <__fmt_char_type _CharT> +struct _LIBCPP_TEMPLATE_VIS formatter<__thread_id, _CharT> { +public: + template + _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) { + return __parser_.__parse(__ctx, __format_spec::__fields_fill_align_width); + } + + template + _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(__thread_id __id, _FormatContext& __ctx) const { + // In __threading_support __libcpp_thread_id is either a + // unsigned long long or a pthread_t. + // + // The type of pthread_t is left unspecified in POSIX so it can be any + // type. The most logical types are an integral or pointer. + // On Linux systems pthread_t is an unsigned long long. + // On Apple systems pthread_t is a pointer type. + // + // Note the output should match what the stream operator does. Since + // the ostream operator has been shipped years before this formatter + // was added to the Standard, this formatter does what the stream + // operator does. This may require platform specific changes. + + using _Tp = decltype(__get_underlying_id(__id)); + using _Cp = conditional_t, _Tp, conditional_t, uintptr_t, void>>; + static_assert(!is_same_v<_Cp, void>, "unsupported thread::id type, please file a bug report"); + + __format_spec::__parsed_specifications<_CharT> __specs = __parser_.__get_parsed_std_specifications(__ctx); + if constexpr (is_pointer_v<_Tp>) { + __specs.__std_.__alternate_form_ = true; + __specs.__std_.__type_ = __format_spec::__type::__hexadecimal_lower_case; + } + return __formatter::__format_integer(reinterpret_cast<_Cp>(__get_underlying_id(__id)), __ctx, __specs); + } + + __format_spec::__parser<_CharT> __parser_{.__alignment_ = __format_spec::__alignment::__right}; +}; + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER >= 23 + +#endif // _LIBCPP___THREAD_FORMATTER diff --git a/libcxx/include/__thread/jthread.h b/libcxx/include/__thread/jthread.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__thread/jthread.h @@ -0,0 +1,132 @@ +// -*- 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___THREAD_JTHREAD +#define _LIBCPP___THREAD_JTHREAD + +#include <__config> +#include <__functional/invoke.h> +#include <__stop_token/stop_source.h> +#include <__stop_token/stop_token.h> +#include <__thread/thread.h> +#include <__threading_support> +#include <__type_traits/decay.h> +#include <__type_traits/is_constructible.h> +#include <__type_traits/is_same.h> +#include <__type_traits/remove_cvref.h> +#include <__utility/forward.h> +#include <__utility/move.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#ifdef _LIBCPP_HAS_NO_THREADS +# error " is not supported since libc++ has been configured without support for threads." +#endif + +#if _LIBCPP_STD_VER >= 20 + +_LIBCPP_BEGIN_NAMESPACE_STD + +class jthread { +public: + // types + using id = thread::id; + using native_handle_type = thread::native_handle_type; + + // [thread.jthread.cons], constructors, move, and assignment + _LIBCPP_HIDE_FROM_ABI jthread() noexcept : __stop_source_(std::nostopstate) {} + + template + _LIBCPP_HIDE_FROM_ABI explicit jthread(_Fun&& __fun, _Args&&... __args) + requires(!std::is_same_v, jthread>) + : __stop_source_(), __thread_(__init_thread(std::forward<_Fun>(__fun), std::forward<_Args>(__args)...)) { + static_assert(is_constructible_v, _Fun>); + static_assert((is_constructible_v, _Args> && ...)); + static_assert(is_invocable_v, decay_t<_Args>...> || + is_invocable_v, stop_token, decay_t<_Args>...>); + } + + _LIBCPP_HIDE_FROM_ABI ~jthread() { + if (joinable()) { + request_stop(); + join(); + } + } + + jthread(const jthread&) = delete; + + _LIBCPP_HIDE_FROM_ABI jthread(jthread&& __other) noexcept = default; + + jthread& operator=(const jthread&) = delete; + + _LIBCPP_HIDE_FROM_ABI jthread& operator=(jthread&& __other) noexcept { + if (this != &__other) { + if (joinable()) { + request_stop(); + join(); + } + __stop_source_ = std::move(__other.__stop_source_); + __thread_ = std::move(__other.__thread_); + } + + return *this; + } + + // [thread.jthread.mem], members + _LIBCPP_HIDE_FROM_ABI void swap(jthread& __other) noexcept { + std::swap(__stop_source_, __other.__stop_source_); + std::swap(__thread_, __other.__thread_); + } + + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool joinable() const noexcept { return get_id() != id(); } + + _LIBCPP_HIDE_FROM_ABI void join() { __thread_.join(); } + + _LIBCPP_HIDE_FROM_ABI void detach() { __thread_.detach(); } + + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI id get_id() const noexcept { return __thread_.get_id(); } + + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __thread_.native_handle(); } + + // [thread.jthread.stop], stop token handling + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_source get_stop_source() noexcept { return __stop_source_; } + + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_token get_stop_token() const noexcept { return __stop_source_.get_token(); } + + _LIBCPP_HIDE_FROM_ABI bool request_stop() noexcept { return __stop_source_.request_stop(); } + + // [thread.jthread.special], specialized algorithms + _LIBCPP_HIDE_FROM_ABI friend void swap(jthread& __lhs, jthread& __rhs) noexcept { __lhs.swap(__rhs); } + + // [thread.jthread.static], static members + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static unsigned int hardware_concurrency() noexcept { + return thread::hardware_concurrency(); + } + +private: + template + _LIBCPP_HIDE_FROM_ABI thread __init_thread(_Fun&& __fun, _Args&&... __args) { + if constexpr (is_invocable_v, stop_token, decay_t<_Args>...>) { + return thread(std::forward<_Fun>(__fun), __stop_source_.get_token(), std::forward<_Args>(__args)...); + } else { + return thread(std::forward<_Fun>(__fun), std::forward<_Args>(__args)...); + } + } + + stop_source __stop_source_; + thread __thread_; +}; + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER >= 20 + +#endif // _LIBCPP___THREAD_JTHREAD diff --git a/libcxx/include/__thread/this_thread.h b/libcxx/include/__thread/this_thread.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__thread/this_thread.h @@ -0,0 +1,78 @@ +// -*- 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___THREAD_THIS_THREAD +#define _LIBCPP___THREAD_THIS_THREAD + +#include <__chrono/steady_clock.h> +#include <__chrono/time_point.h> +#include <__condition_variable/condition_variable.h> +#include <__config> +#include <__mutex/mutex.h> +#include <__mutex/unique_lock.h> +#include <__threading_support> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + +#ifdef _LIBCPP_HAS_NO_THREADS +# error " is not supported since libc++ has been configured without support for threads." +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace this_thread { + +_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns); + +template +_LIBCPP_HIDE_FROM_ABI void sleep_for(const chrono::duration<_Rep, _Period>& __d) { + if (__d > chrono::duration<_Rep, _Period>::zero()) { + // The standard guarantees a 64bit signed integer resolution for nanoseconds, + // so use INT64_MAX / 1e9 as cut-off point. Use a constant to avoid + // and issues with long double folding on PowerPC with GCC. + _LIBCPP_CONSTEXPR chrono::duration __max = chrono::duration(9223372036.0L); + chrono::nanoseconds __ns; + if (__d < __max) { + __ns = chrono::duration_cast(__d); + if (__ns < __d) + ++__ns; + } else + __ns = chrono::nanoseconds::max(); + this_thread::sleep_for(__ns); + } +} + +template +_LIBCPP_HIDE_FROM_ABI void sleep_until(const chrono::time_point<_Clock, _Duration>& __t) { + mutex __mut; + condition_variable __cv; + unique_lock __lk(__mut); + while (_Clock::now() < __t) + __cv.wait_until(__lk, __t); +} + +template +inline _LIBCPP_INLINE_VISIBILITY void sleep_until(const chrono::time_point& __t) { + this_thread::sleep_for(__t - chrono::steady_clock::now()); +} + +inline _LIBCPP_INLINE_VISIBILITY void yield() _NOEXCEPT { __libcpp_thread_yield(); } + +} // namespace this_thread + +_LIBCPP_END_NAMESPACE_STD + +_LIBCPP_POP_MACROS + +#endif // _LIBCPP___THREAD_THIS_THREAD diff --git a/libcxx/include/__thread/thread.h b/libcxx/include/__thread/thread.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__thread/thread.h @@ -0,0 +1,231 @@ +// -*- 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___THREAD_THREAD +#define _LIBCPP___THREAD_THREAD + +#include <__condition_variable/condition_variable.h> +#include <__config> +#include <__exception/terminate.h> +#include <__functional/hash.h> +#include <__functional/unary_function.h> +#include <__memory/unique_ptr.h> +#include <__mutex/mutex.h> +#include <__system_error/system_error.h> +#include <__threading_support> +#include <__utility/forward.h> +#include + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#ifdef _LIBCPP_HAS_NO_THREADS +# error " is not supported since libc++ has been configured without support for threads." +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +template +class __thread_specific_ptr; +class _LIBCPP_TYPE_VIS __thread_struct; +class _LIBCPP_HIDDEN __thread_struct_imp; +class __assoc_sub_state; + +_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data(); + +class _LIBCPP_TYPE_VIS __thread_struct { + __thread_struct_imp* __p_; + + __thread_struct(const __thread_struct&); + __thread_struct& operator=(const __thread_struct&); + +public: + __thread_struct(); + ~__thread_struct(); + + void notify_all_at_thread_exit(condition_variable*, mutex*); + void __make_ready_at_thread_exit(__assoc_sub_state*); +}; + +template +class __thread_specific_ptr { + __libcpp_tls_key __key_; + + // Only __thread_local_data() may construct a __thread_specific_ptr + // and only with _Tp == __thread_struct. + static_assert((is_same<_Tp, __thread_struct>::value), ""); + __thread_specific_ptr(); + friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data(); + + __thread_specific_ptr(const __thread_specific_ptr&); + __thread_specific_ptr& operator=(const __thread_specific_ptr&); + + _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*); + +public: + typedef _Tp* pointer; + + ~__thread_specific_ptr(); + + _LIBCPP_INLINE_VISIBILITY pointer get() const { return static_cast<_Tp*>(__libcpp_tls_get(__key_)); } + _LIBCPP_INLINE_VISIBILITY pointer operator*() const { return *get(); } + _LIBCPP_INLINE_VISIBILITY pointer operator->() const { return get(); } + void set_pointer(pointer __p); +}; + +template +void _LIBCPP_TLS_DESTRUCTOR_CC __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) { + delete static_cast(__p); +} + +template +__thread_specific_ptr<_Tp>::__thread_specific_ptr() { + int __ec = __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit); + if (__ec) + __throw_system_error(__ec, "__thread_specific_ptr construction failed"); +} + +template +__thread_specific_ptr<_Tp>::~__thread_specific_ptr() { + // __thread_specific_ptr is only created with a static storage duration + // so this destructor is only invoked during program termination. Invoking + // pthread_key_delete(__key_) may prevent other threads from deleting their + // thread local data. For this reason we leak the key. +} + +template +void __thread_specific_ptr<_Tp>::set_pointer(pointer __p) { + _LIBCPP_ASSERT(get() == nullptr, "Attempting to overwrite thread local data"); + std::__libcpp_tls_set(__key_, __p); +} + +template <> +struct _LIBCPP_TEMPLATE_VIS hash<__thread_id> : public __unary_function<__thread_id, size_t> { + _LIBCPP_INLINE_VISIBILITY size_t operator()(__thread_id __v) const _NOEXCEPT { + return hash<__libcpp_thread_id>()(__v.__id_); + } +}; + +template +_LIBCPP_INLINE_VISIBILITY basic_ostream<_CharT, _Traits>& +operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) { + return __os << __id.__id_; +} + +class _LIBCPP_TYPE_VIS thread { + __libcpp_thread_t __t_; + + thread(const thread&); + thread& operator=(const thread&); + +public: + typedef __thread_id id; + typedef __libcpp_thread_t native_handle_type; + + _LIBCPP_INLINE_VISIBILITY thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {} +#ifndef _LIBCPP_CXX03_LANG + template , thread>::value> > + _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS explicit thread(_Fp&& __f, _Args&&... __args); +#else // _LIBCPP_CXX03_LANG + template + _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS explicit thread(_Fp __f); +#endif + ~thread(); + + _LIBCPP_INLINE_VISIBILITY thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) { __t.__t_ = _LIBCPP_NULL_THREAD; } + + _LIBCPP_INLINE_VISIBILITY thread& operator=(thread&& __t) _NOEXCEPT { + if (!__libcpp_thread_isnull(&__t_)) + terminate(); + __t_ = __t.__t_; + __t.__t_ = _LIBCPP_NULL_THREAD; + return *this; + } + + _LIBCPP_INLINE_VISIBILITY void swap(thread& __t) _NOEXCEPT { _VSTD::swap(__t_, __t.__t_); } + + _LIBCPP_INLINE_VISIBILITY bool joinable() const _NOEXCEPT { return !__libcpp_thread_isnull(&__t_); } + void join(); + void detach(); + _LIBCPP_INLINE_VISIBILITY id get_id() const _NOEXCEPT { return __libcpp_thread_get_id(&__t_); } + _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() _NOEXCEPT { return __t_; } + + static unsigned hardware_concurrency() _NOEXCEPT; +}; + +#ifndef _LIBCPP_CXX03_LANG + +template +inline _LIBCPP_INLINE_VISIBILITY void __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>) { + _VSTD::__invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...); +} + +template +_LIBCPP_INLINE_VISIBILITY void* __thread_proxy(void* __vp) { + // _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...> + unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); + __thread_local_data().set_pointer(_VSTD::get<0>(*__p.get()).release()); + typedef typename __make_tuple_indices::value, 2>::type _Index; + _VSTD::__thread_execute(*__p.get(), _Index()); + return nullptr; +} + +template +thread::thread(_Fp&& __f, _Args&&... __args) { + typedef unique_ptr<__thread_struct> _TSPtr; + _TSPtr __tsp(new __thread_struct); + typedef tuple<_TSPtr, __decay_t<_Fp>, __decay_t<_Args>...> _Gp; + unique_ptr<_Gp> __p(new _Gp(_VSTD::move(__tsp), _VSTD::forward<_Fp>(__f), _VSTD::forward<_Args>(__args)...)); + int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get()); + if (__ec == 0) + __p.release(); + else + __throw_system_error(__ec, "thread constructor failed"); +} + +#else // _LIBCPP_CXX03_LANG + +template +struct __thread_invoke_pair { + // This type is used to pass memory for thread local storage and a functor + // to a newly created thread because std::pair doesn't work with + // std::unique_ptr in C++03. + _LIBCPP_HIDE_FROM_ABI __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {} + unique_ptr<__thread_struct> __tsp_; + _Fp __fn_; +}; + +template +_LIBCPP_HIDE_FROM_ABI void* __thread_proxy_cxx03(void* __vp) { + unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); + __thread_local_data().set_pointer(__p->__tsp_.release()); + (__p->__fn_)(); + return nullptr; +} + +template +thread::thread(_Fp __f) { + typedef __thread_invoke_pair<_Fp> _InvokePair; + typedef unique_ptr<_InvokePair> _PairPtr; + _PairPtr __pp(new _InvokePair(__f)); + int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get()); + if (__ec == 0) + __pp.release(); + else + __throw_system_error(__ec, "thread constructor failed"); +} + +#endif // _LIBCPP_CXX03_LANG + +inline _LIBCPP_INLINE_VISIBILITY void swap(thread& __x, thread& __y) _NOEXCEPT { __x.swap(__y); } + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___THREAD_THREAD diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in --- a/libcxx/include/module.modulemap.in +++ b/libcxx/include/module.modulemap.in @@ -1516,7 +1516,11 @@ export * module __thread { + module formatter { private header "__thread/formatter.h" } + module jthread { private header "__thread/jthread.h" } module poll_with_backoff { private header "__thread/poll_with_backoff.h" } + module this_thread { private header "__thread/this_thread.h" } + module thread { private header "__thread/thread.h" } module timed_backoff_policy { private header "__thread/timed_backoff_policy.h" } } } diff --git a/libcxx/include/thread b/libcxx/include/thread --- a/libcxx/include/thread +++ b/libcxx/include/thread @@ -67,6 +67,45 @@ template struct formatter; +class jthread // C++20 +{ + public: + // types + using id = thread::id; + using native_handle_type = thread::native_handle_type; + + // [thread.jthread.cons], constructors, move, and assignment + jthread() noexcept; + template explicit jthread(F&& f, Args&&... args); + ~jthread(); + jthread(const jthread&) = delete; + jthread(jthread&&) noexcept; + jthread& operator=(const jthread&) = delete; + jthread& operator=(jthread&&) noexcept; + + // [thread.jthread.mem], members + void swap(jthread&) noexcept; + [[nodiscard]] bool joinable() const noexcept; + void join(); + void detach(); + [[nodiscard]] id get_id() const noexcept; + [[nodiscard]] native_handle_type native_handle(); // see [thread.req.native] + + // [thread.jthread.stop], stop token handling + [[nodiscard]] stop_source get_stop_source() noexcept; + [[nodiscard]] stop_token get_stop_token() const noexcept; + bool request_stop() noexcept; + + // [thread.jthread.special], specialized algorithms + friend void swap(jthread& lhs, jthread& rhs) noexcept; + + // [thread.jthread.static], static members + [[nodiscard]] static unsigned int hardware_concurrency() noexcept; + + private: + stop_source ssource; // exposition only +}; + namespace this_thread { @@ -88,33 +127,28 @@ #include <__assert> // all public C++ headers provide the assertion handler #include <__availability> -#include <__chrono/steady_clock.h> -#include <__chrono/time_point.h> -#include <__concepts/arithmetic.h> -#include <__condition_variable/condition_variable.h> #include <__config> -#include <__exception/terminate.h> -#include <__format/concepts.h> -#include <__format/format_parse_context.h> -#include <__format/formatter.h> -#include <__format/formatter_integral.h> -#include <__format/parser_std_format_spec.h> -#include <__functional/hash.h> -#include <__functional/unary_function.h> -#include <__memory/addressof.h> -#include <__memory/unique_ptr.h> -#include <__mutex/mutex.h> -#include <__mutex/unique_lock.h> -#include <__system_error/system_error.h> +#include <__thread/formatter.h> +#include <__thread/jthread.h> #include <__thread/poll_with_backoff.h> +#include <__thread/this_thread.h> +#include <__thread/thread.h> #include <__thread/timed_backoff_policy.h> #include <__threading_support> -#include <__utility/forward.h> +#include + +// Some tests rely on these chrono dependencies +#include <__chrono/steady_clock.h> +#include <__chrono/time_point.h> + +/* Does not seem to fail without those? +#include <__memory/addressof.h> +*/ + +// These are previously included public headers #include #include #include -#include -#include // standard-mandated includes @@ -125,347 +159,9 @@ # pragma GCC system_header #endif -_LIBCPP_PUSH_MACROS -#include <__undef_macros> - #ifdef _LIBCPP_HAS_NO_THREADS -# error " is not supported since libc++ has been configured without support for threads." -#endif - -_LIBCPP_BEGIN_NAMESPACE_STD - -template class __thread_specific_ptr; -class _LIBCPP_TYPE_VIS __thread_struct; -class _LIBCPP_HIDDEN __thread_struct_imp; -class __assoc_sub_state; - -_LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data(); - -class _LIBCPP_TYPE_VIS __thread_struct -{ - __thread_struct_imp* __p_; - - __thread_struct(const __thread_struct&); - __thread_struct& operator=(const __thread_struct&); -public: - __thread_struct(); - ~__thread_struct(); - - void notify_all_at_thread_exit(condition_variable*, mutex*); - void __make_ready_at_thread_exit(__assoc_sub_state*); -}; - -template -class __thread_specific_ptr -{ - __libcpp_tls_key __key_; - - // Only __thread_local_data() may construct a __thread_specific_ptr - // and only with _Tp == __thread_struct. - static_assert((is_same<_Tp, __thread_struct>::value), ""); - __thread_specific_ptr(); - friend _LIBCPP_FUNC_VIS __thread_specific_ptr<__thread_struct>& __thread_local_data(); - - __thread_specific_ptr(const __thread_specific_ptr&); - __thread_specific_ptr& operator=(const __thread_specific_ptr&); - - _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*); - -public: - typedef _Tp* pointer; - - ~__thread_specific_ptr(); - - _LIBCPP_INLINE_VISIBILITY - pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));} - _LIBCPP_INLINE_VISIBILITY - pointer operator*() const {return *get();} - _LIBCPP_INLINE_VISIBILITY - pointer operator->() const {return get();} - void set_pointer(pointer __p); -}; - -template -void _LIBCPP_TLS_DESTRUCTOR_CC -__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) -{ - delete static_cast(__p); -} - -template -__thread_specific_ptr<_Tp>::__thread_specific_ptr() -{ - int __ec = - __libcpp_tls_create(&__key_, &__thread_specific_ptr::__at_thread_exit); - if (__ec) - __throw_system_error(__ec, "__thread_specific_ptr construction failed"); -} - -template -__thread_specific_ptr<_Tp>::~__thread_specific_ptr() -{ - // __thread_specific_ptr is only created with a static storage duration - // so this destructor is only invoked during program termination. Invoking - // pthread_key_delete(__key_) may prevent other threads from deleting their - // thread local data. For this reason we leak the key. -} - -template -void -__thread_specific_ptr<_Tp>::set_pointer(pointer __p) -{ - _LIBCPP_ASSERT(get() == nullptr, - "Attempting to overwrite thread local data"); - std::__libcpp_tls_set(__key_, __p); -} - -template<> -struct _LIBCPP_TEMPLATE_VIS hash<__thread_id> - : public __unary_function<__thread_id, size_t> -{ - _LIBCPP_INLINE_VISIBILITY - size_t operator()(__thread_id __v) const _NOEXCEPT - { - return hash<__libcpp_thread_id>()(__v.__id_); - } -}; - -template -_LIBCPP_INLINE_VISIBILITY -basic_ostream<_CharT, _Traits>& -operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) -{return __os << __id.__id_;} - -#if _LIBCPP_STD_VER >= 23 -template <__fmt_char_type _CharT> -struct _LIBCPP_TEMPLATE_VIS formatter<__thread_id, _CharT> { - public: - template - _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) { - return __parser_.__parse(__ctx, __format_spec::__fields_fill_align_width); - } - - template - _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(__thread_id __id, _FormatContext& __ctx) const { - // In __threading_support __libcpp_thread_id is either a - // unsigned long long or a pthread_t. - // - // The type of pthread_t is left unspecified in POSIX so it can be any - // type. The most logical types are an integral or pointer. - // On Linux systems pthread_t is an unsigned long long. - // On Apple systems pthread_t is a pointer type. - // - // Note the output should match what the stream operator does. Since - // the ostream operator has been shipped years before this formatter - // was added to the Standard, this formatter does what the stream - // operator does. This may require platform specific changes. - - using _Tp = decltype(__get_underlying_id(__id)); - using _Cp = conditional_t, _Tp, conditional_t, uintptr_t, void>>; - static_assert(!is_same_v<_Cp, void>, "unsupported thread::id type, please file a bug report"); - - __format_spec::__parsed_specifications<_CharT> __specs = __parser_.__get_parsed_std_specifications(__ctx); - if constexpr (is_pointer_v<_Tp>) { - __specs.__std_.__alternate_form_ = true; - __specs.__std_.__type_ = __format_spec::__type::__hexadecimal_lower_case; - } - return __formatter::__format_integer(reinterpret_cast<_Cp>(__get_underlying_id(__id)), __ctx, __specs); - } - - __format_spec::__parser<_CharT> __parser_{.__alignment_ = __format_spec::__alignment::__right}; -}; -#endif // _LIBCPP_STD_VER >= 23 - -class _LIBCPP_TYPE_VIS thread -{ - __libcpp_thread_t __t_; - - thread(const thread&); - thread& operator=(const thread&); -public: - typedef __thread_id id; - typedef __libcpp_thread_t native_handle_type; - - _LIBCPP_INLINE_VISIBILITY - thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {} -#ifndef _LIBCPP_CXX03_LANG - template , thread>::value> > - _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS - explicit thread(_Fp&& __f, _Args&&... __args); -#else // _LIBCPP_CXX03_LANG - template - _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS - explicit thread(_Fp __f); +# error " is not supported since libc++ has been configured without support for threads." #endif - ~thread(); - - _LIBCPP_INLINE_VISIBILITY - thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) { - __t.__t_ = _LIBCPP_NULL_THREAD; - } - - _LIBCPP_INLINE_VISIBILITY - thread& operator=(thread&& __t) _NOEXCEPT { - if (!__libcpp_thread_isnull(&__t_)) - terminate(); - __t_ = __t.__t_; - __t.__t_ = _LIBCPP_NULL_THREAD; - return *this; - } - - _LIBCPP_INLINE_VISIBILITY - void swap(thread& __t) _NOEXCEPT {_VSTD::swap(__t_, __t.__t_);} - - _LIBCPP_INLINE_VISIBILITY - bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);} - void join(); - void detach(); - _LIBCPP_INLINE_VISIBILITY - id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);} - _LIBCPP_INLINE_VISIBILITY - native_handle_type native_handle() _NOEXCEPT {return __t_;} - - static unsigned hardware_concurrency() _NOEXCEPT; -}; - -#ifndef _LIBCPP_CXX03_LANG - -template -inline _LIBCPP_INLINE_VISIBILITY -void -__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>) -{ - _VSTD::__invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...); -} - -template -_LIBCPP_INLINE_VISIBILITY -void* __thread_proxy(void* __vp) -{ - // _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...> - unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); - __thread_local_data().set_pointer(_VSTD::get<0>(*__p.get()).release()); - typedef typename __make_tuple_indices::value, 2>::type _Index; - _VSTD::__thread_execute(*__p.get(), _Index()); - return nullptr; -} - -template -thread::thread(_Fp&& __f, _Args&&... __args) -{ - typedef unique_ptr<__thread_struct> _TSPtr; - _TSPtr __tsp(new __thread_struct); - typedef tuple<_TSPtr, __decay_t<_Fp>, __decay_t<_Args>...> _Gp; - unique_ptr<_Gp> __p( - new _Gp(_VSTD::move(__tsp), - _VSTD::forward<_Fp>(__f), - _VSTD::forward<_Args>(__args)...)); - int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get()); - if (__ec == 0) - __p.release(); - else - __throw_system_error(__ec, "thread constructor failed"); -} - -#else // _LIBCPP_CXX03_LANG - -template -struct __thread_invoke_pair { - // This type is used to pass memory for thread local storage and a functor - // to a newly created thread because std::pair doesn't work with - // std::unique_ptr in C++03. - _LIBCPP_HIDE_FROM_ABI __thread_invoke_pair(_Fp& __f) : __tsp_(new __thread_struct), __fn_(__f) {} - unique_ptr<__thread_struct> __tsp_; - _Fp __fn_; -}; - -template -_LIBCPP_HIDE_FROM_ABI void* __thread_proxy_cxx03(void* __vp) -{ - unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp)); - __thread_local_data().set_pointer(__p->__tsp_.release()); - (__p->__fn_)(); - return nullptr; -} - -template -thread::thread(_Fp __f) -{ - - typedef __thread_invoke_pair<_Fp> _InvokePair; - typedef unique_ptr<_InvokePair> _PairPtr; - _PairPtr __pp(new _InvokePair(__f)); - int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get()); - if (__ec == 0) - __pp.release(); - else - __throw_system_error(__ec, "thread constructor failed"); -} - -#endif // _LIBCPP_CXX03_LANG - -inline _LIBCPP_INLINE_VISIBILITY -void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);} - -namespace this_thread -{ - -_LIBCPP_FUNC_VIS void sleep_for(const chrono::nanoseconds& __ns); - -template -_LIBCPP_HIDE_FROM_ABI void -sleep_for(const chrono::duration<_Rep, _Period>& __d) -{ - if (__d > chrono::duration<_Rep, _Period>::zero()) - { - // The standard guarantees a 64bit signed integer resolution for nanoseconds, - // so use INT64_MAX / 1e9 as cut-off point. Use a constant to avoid - // and issues with long double folding on PowerPC with GCC. - _LIBCPP_CONSTEXPR chrono::duration __max = - chrono::duration(9223372036.0L); - chrono::nanoseconds __ns; - if (__d < __max) - { - __ns = chrono::duration_cast(__d); - if (__ns < __d) - ++__ns; - } - else - __ns = chrono::nanoseconds::max(); - this_thread::sleep_for(__ns); - } -} - -template -_LIBCPP_HIDE_FROM_ABI void -sleep_until(const chrono::time_point<_Clock, _Duration>& __t) -{ - mutex __mut; - condition_variable __cv; - unique_lock __lk(__mut); - while (_Clock::now() < __t) - __cv.wait_until(__lk, __t); -} - -template -inline _LIBCPP_INLINE_VISIBILITY -void -sleep_until(const chrono::time_point& __t) -{ - this_thread::sleep_for(__t - chrono::steady_clock::now()); -} - -inline _LIBCPP_INLINE_VISIBILITY -void yield() _NOEXCEPT {__libcpp_thread_yield();} - -} // namespace this_thread - -_LIBCPP_END_NAMESPACE_STD - -_LIBCPP_POP_MACROS #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17 # include diff --git a/libcxx/test/std/thread/thread.jthread/assign.move.pass.cpp b/libcxx/test/std/thread/thread.jthread/assign.move.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/thread/thread.jthread/assign.move.pass.cpp @@ -0,0 +1,88 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: no-threads +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// XFAIL: availability-synchronization_library-missing +// ADDITIONAL_COMPILE_FLAGS: -Wno-self-move + +// jthread& operator=(jthread&&) noexcept; + +#include +#include +#include +#include +#include +#include + +#include "test_macros.h" + +static_assert(std::is_nothrow_move_assignable_v); + +int main(int, char**) { + // If &x == this is true, there are no effects. + { + std::jthread j([] {}); + auto id = j.get_id(); + auto ssource = j.get_stop_source(); + j = std::move(j); + assert(j.get_id() == id); + assert(j.get_stop_source() == ssource); + } + + // if joinable() is true, calls request_stop() and then join() + // request_stop is called + { + std::jthread j1([] {}); + bool called = false; + std::stop_callback cb(j1.get_stop_token(), [&called] { called = true; }); + + std::jthread j2([] {}); + j1 = std::move(j2); + assert(called); + } + + // if joinable() is true, calls request_stop() and then join() + // join is called + { + bool called = false; + std::jthread j1([&called] { + std::this_thread::sleep_for(std::chrono::milliseconds{2}); + called = true; + }); + + std::jthread j2; + j1 = std::move(j2); + assert(called); + } + + // then assigns the state of x to *this + { + std::jthread j1([] {}); + std::jthread j2([] {}); + auto id2 = j2.get_id(); + auto ssource2 = j2.get_stop_source(); + + j1 = std::move(j2); + + assert(j1.get_id() == id2); + assert(j1.get_stop_source() == ssource2); + } + + // sets x to a default constructed state + { + std::jthread j1([] {}); + std::jthread j2([] {}); + j1 = std::move(j2); + + assert(j2.get_id() == std::jthread::id()); + assert(!j2.get_stop_source().stop_possible()); + } + + return 0; +} diff --git a/libcxx/test/std/thread/thread.jthread/cons.default.pass.cpp b/libcxx/test/std/thread/thread.jthread/cons.default.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/thread/thread.jthread/cons.default.pass.cpp @@ -0,0 +1,32 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: no-threads +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// XFAIL: availability-synchronization_library-missing + +// jthread() noexcept; + +#include +#include +#include +#include + +#include "test_macros.h" + +static_assert(std::is_nothrow_default_constructible_v); + +int main(int, char**) { + { + std::jthread jt = {}; // implicit + assert(!jt.get_stop_source().stop_possible()); + assert(jt.get_id() == std::jthread::id()); + } + + return 0; +} diff --git a/libcxx/test/std/thread/thread.jthread/cons.func.token.pass.cpp b/libcxx/test/std/thread/thread.jthread/cons.func.token.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/thread/thread.jthread/cons.func.token.pass.cpp @@ -0,0 +1,155 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: no-threads +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// XFAIL: availability-synchronization_library-missing + +// template +// explicit jthread(F&& f, Args&&... args); + +#include +#include +#include +#include + +#include "test_macros.h" + +template +struct Func { + void operator()(Args...) const; +}; + +// Constraints: remove_cvref_t is not the same type as jthread. +static_assert(std::is_constructible_v>); +static_assert(std::is_constructible_v, int>); +static_assert(!std::is_constructible_v); + +// explicit +template +void conversion_test(T); + +template +concept ImplicitlyConstructible = requires(Args&&... args) { conversion_test({std::forward(args)...}); }; + +static_assert(!ImplicitlyConstructible>); +static_assert(!ImplicitlyConstructible, int>); + +int main(int, char**) { + // Effects: Initializes ssource + // Postconditions: get_id() != id() is true and ssource.stop_possible() is true + // and *this represents the newly started thread. + { + std::jthread jt{[] {}}; + assert(jt.get_stop_source().stop_possible()); + assert(jt.get_id() != std::jthread::id()); + } + + // The new thread of execution executes + // invoke(auto(std::forward(f)), get_stop_token(), auto(std::forward(args))...) + // if that expression is well-formed, + { + int result = 0; + std::jthread jt{[&result](std::stop_token st, int i) { + assert(st.stop_possible()); + assert(!st.stop_requested()); + result += i; + }, + 5}; + jt.join(); + assert(result == 5); + } + + // otherwise + // invoke(auto(std::forward(f)), auto(std::forward(args))...) + { + int result = 0; + std::jthread jt{[&result](int i) { result += i; }, 5}; + jt.join(); + assert(result == 5); + } + + // with the values produced by auto being materialized ([conv.rval]) in the constructing thread. + { + struct TrackThread { + std::jthread::id threadId; + bool copyConstructed = false; + bool moveConstructed = false; + + TrackThread() : threadId(std::this_thread::get_id()) {} + TrackThread(const TrackThread&) : threadId(std::this_thread::get_id()), copyConstructed(true) {} + TrackThread(TrackThread&&) : threadId(std::this_thread::get_id()), moveConstructed(true) {} + }; + + auto mainThread = std::this_thread::get_id(); + + TrackThread arg1; + std::jthread jt1{[mainThread](const TrackThread& arg) { + assert(arg.threadId == mainThread); + assert(arg.threadId != std::this_thread::get_id()); + assert(arg.copyConstructed); + }, + arg1}; + + TrackThread arg2; + std::jthread jt2{[mainThread](const TrackThread& arg) { + assert(arg.threadId == mainThread); + assert(arg.threadId != std::this_thread::get_id()); + assert(arg.moveConstructed); + }, + std::move(arg2)}; + } + + // [Note 1: This implies that any exceptions not thrown from the invocation of the copy + // of f will be thrown in the constructing thread, not the new thread. — end note] + { + struct Exception { + std::jthread::id threadId; + }; + struct ThrowOnCopyFunc { + ThrowOnCopyFunc() = default; + ThrowOnCopyFunc(const ThrowOnCopyFunc&) { throw Exception{std::this_thread::get_id()}; } + void operator()() const {} + }; + ThrowOnCopyFunc f1; + try { + std::jthread jt{f1}; + assert(false); + } catch (const Exception& e) { + assert(e.threadId == std::this_thread::get_id()); + } + } + + // Synchronization: The completion of the invocation of the constructor + // synchronizes with the beginning of the invocation of the copy of f. + { + int flag = 0; + struct Arg { + int& flag_; + Arg(int& f) : flag_(f) {} + + Arg(const Arg& other) : flag_(other.flag_) { flag_ = 5; } + }; + + Arg arg(flag); + std::jthread jt( + [&flag](const auto&) { + assert(flag == 5); // happens-after the copy-construction of arg + }, + arg); + } + + // TODO: test the following: + // + // Throws: system_error if unable to start the new thread. + // Error conditions: + // resource_unavailable_try_again — the system lacked the necessary resources to create another thread, + // or the system-imposed limit on the number of threads in a process would be exceeded. + + return 0; +} diff --git a/libcxx/test/std/thread/thread.jthread/cons.move.pass.cpp b/libcxx/test/std/thread/thread.jthread/cons.move.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/thread/thread.jthread/cons.move.pass.cpp @@ -0,0 +1,49 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: no-threads +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// XFAIL: availability-synchronization_library-missing + +// jthread(jthread&& x) noexcept; + +#include +#include +#include +#include +#include + +#include "test_macros.h" + +static_assert(std::is_nothrow_move_constructible_v); + +int main(int, char**) { + { + // x.get_id() == id() and get_id() returns the value of x.get_id() prior + // to the start of construction. + std::jthread j1{[] {}}; + auto id1 = j1.get_id(); + + std::jthread j2(std::move(j1)); + assert(j2.get_id() == id1); + } + + { + // ssource has the value of x.ssource prior to the start of construction + // and x.ssource.stop_possible() is false. + std::jthread j1{[] {}}; + auto ss1 = j1.get_stop_source(); + + std::jthread j2(std::move(j1)); + assert(ss1 == j2.get_stop_source()); + assert(!j1.get_stop_source().stop_possible()); + assert(j2.get_stop_source().stop_possible()); + } + + return 0; +} diff --git a/libcxx/test/std/thread/thread.jthread/copy.delete.compile.pass.cpp b/libcxx/test/std/thread/thread.jthread/copy.delete.compile.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/thread/thread.jthread/copy.delete.compile.pass.cpp @@ -0,0 +1,20 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: no-threads +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// XFAIL: availability-synchronization_library-missing + +// jthread(const jthread&) = delete; +// jthread& operator=(const jthread&) = delete; + +#include +#include + +static_assert(!std::is_copy_constructible_v); +static_assert(!std::is_copy_assignable_v); diff --git a/libcxx/test/std/thread/thread.jthread/dtor.pass.cpp b/libcxx/test/std/thread/thread.jthread/dtor.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/thread/thread.jthread/dtor.pass.cpp @@ -0,0 +1,49 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: no-threads +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// XFAIL: availability-synchronization_library-missing + +// ~jthread(); + +#include +#include +#include +#include +#include +#include "test_macros.h" + +int main(int, char**) { + // !joinable() + { std::jthread jt; } + + // If joinable() is true, calls request_stop() and then join(). + // request_stop is called + { + std::optional jt([] {}); + bool called = false; + std::stop_callback cb(jt->get_stop_token(), [&called] { called = true; }); + jt.reset(); + assert(called); + } + + // If joinable() is true, calls request_stop() and then join(). + // join is called + { + bool called = false; + std::optional jt([&called] { + std::this_thread::sleep_for(std::chrono::milliseconds{2}); + called = true; + }); + jt.reset(); + assert(called); + } + + return 0; +} diff --git a/libcxx/test/std/thread/thread.jthread/typedef.compile.pass.cpp b/libcxx/test/std/thread/thread.jthread/typedef.compile.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/thread/thread.jthread/typedef.compile.pass.cpp @@ -0,0 +1,20 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: no-threads +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// XFAIL: availability-synchronization_library-missing + +// using id = thread::id; +// using native_handle_type = thread::native_handle_type; + +#include +#include + +static_assert(std::is_same_v); +static_assert(std::is_same_v);