[libcxx-commits] [PATCH] D151792: [libc++][NFC] Granularise <thread> header
Mark de Wever via Phabricator via libcxx-commits
libcxx-commits at lists.llvm.org
Wed May 31 10:21:21 PDT 2023
Mordante accepted this revision.
Mordante added a comment.
This revision is now accepted and ready to land.
LGTM after undoing the formatting changes and the CI passes.
Can you make two followup commits
- format all the new files, I don't like to do that is the same commit, that makes tracking changes in git harder
- modernize the code `_LIBCPP_INLINE_VISIBILITY` -> `_LIBCPP_HIDE_FROM_ABI`, `_VSTD` -> `std`, `typedef` -> `using`
================
Comment at: libcxx/include/thread:119
#ifdef _LIBCPP_HAS_NO_THREADS
-# error "<thread> is not supported since libc++ has been configured without support for threads."
-#endif
-
-_LIBCPP_BEGIN_NAMESPACE_STD
-
-template <class _Tp> 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 _Tp>
-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 <class _Tp>
-void _LIBCPP_TLS_DESTRUCTOR_CC
-__thread_specific_ptr<_Tp>::__at_thread_exit(void* __p)
-{
- delete static_cast<pointer>(__p);
-}
-
-template <class _Tp>
-__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 <class _Tp>
-__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 <class _Tp>
-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<class _CharT, class _Traits>
-_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 <class _ParseContext>
- _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
- return __parser_.__parse(__ctx, __format_spec::__fields_fill_align_width);
- }
-
- template <class _FormatContext>
- _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<integral<_Tp>, _Tp, conditional_t<is_pointer_v<_Tp>, 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 <class _Fp, class ..._Args,
- class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, thread>::value> >
- _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
- explicit thread(_Fp&& __f, _Args&&... __args);
-#else // _LIBCPP_CXX03_LANG
- template <class _Fp>
- _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
- explicit thread(_Fp __f);
+# error "<thread> is not supported since libc++ has been configured without support for threads."
#endif
----------------
Please don't mix formatting and splitting in one patch.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D151792/new/
https://reviews.llvm.org/D151792
More information about the libcxx-commits
mailing list