diff options
| author | Louis Dionne <ldionne.2@gmail.com> | 2023-12-18 14:01:33 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-18 14:01:33 -0500 |
| commit | 9783f28cbb155e4a8d49c12e1c60ce14dcfaf0c7 (patch) | |
| tree | 4119e3edc01dd51cf2752b2a3341c34d8a3700ac /libcxx/include/__thread | |
| parent | e5c523e8610492b3256dde6856811b527b4dcb35 (diff) | |
[libc++] Format the code base (#74334)
This patch runs clang-format on all of libcxx/include and libcxx/src, in
accordance with the RFC discussed at [1]. Follow-up patches will format
the benchmarks, the test suite and remaining parts of the code. I'm
splitting this one into its own patch so the diff is a bit easier to
review.
This patch was generated with:
find libcxx/include libcxx/src -type f \
| grep -v 'module.modulemap.in' \
| grep -v 'CMakeLists.txt' \
| grep -v 'README.txt' \
| grep -v 'libcxx.imp' \
| grep -v '__config_site.in' \
| xargs clang-format -i
A Git merge driver is available in libcxx/utils/clang-format-merge-driver.sh
to help resolve merge and rebase issues across these formatting changes.
[1]: https://discourse.llvm.org/t/rfc-clang-formatting-all-of-libc-once-and-for-all
Diffstat (limited to 'libcxx/include/__thread')
| -rw-r--r-- | libcxx/include/__thread/formatter.h | 62 | ||||
| -rw-r--r-- | libcxx/include/__thread/poll_with_backoff.h | 37 | ||||
| -rw-r--r-- | libcxx/include/__thread/this_thread.h | 63 | ||||
| -rw-r--r-- | libcxx/include/__thread/thread.h | 324 | ||||
| -rw-r--r-- | libcxx/include/__thread/timed_backoff_policy.h | 28 |
5 files changed, 227 insertions, 287 deletions
diff --git a/libcxx/include/__thread/formatter.h b/libcxx/include/__thread/formatter.h index 0e3a11c514e1..0454864ce939 100644 --- a/libcxx/include/__thread/formatter.h +++ b/libcxx/include/__thread/formatter.h @@ -31,47 +31,47 @@ _LIBCPP_BEGIN_NAMESPACE_STD -#ifndef _LIBCPP_HAS_NO_THREADS +# ifndef _LIBCPP_HAS_NO_THREADS 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); - } +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. + 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"); + 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::__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}; + __format_spec::__parser<_CharT> __parser_{.__alignment_ = __format_spec::__alignment::__right}; }; -#endif // !_LIBCPP_HAS_NO_THREADS +# endif // !_LIBCPP_HAS_NO_THREADS _LIBCPP_END_NAMESPACE_STD diff --git a/libcxx/include/__thread/poll_with_backoff.h b/libcxx/include/__thread/poll_with_backoff.h index d027a2779d8e..0a2eef9a52d2 100644 --- a/libcxx/include/__thread/poll_with_backoff.h +++ b/libcxx/include/__thread/poll_with_backoff.h @@ -34,23 +34,23 @@ static _LIBCPP_CONSTEXPR const int __libcpp_polling_count = 64; // // - __max_elapsed is the maximum duration to try polling for. If the maximum duration is exceeded, // the polling loop will return false to report a timeout. -template<class _Fn, class _BFn> -_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI -bool __libcpp_thread_poll_with_backoff(_Fn&& __f, _BFn&& __bf, chrono::nanoseconds __max_elapsed = chrono::nanoseconds::zero()) { - auto const __start = chrono::high_resolution_clock::now(); - for (int __count = 0;;) { - if (__f()) - return true; // _Fn completion means success - if (__count < __libcpp_polling_count) { - __count += 1; - continue; - } - chrono::nanoseconds const __elapsed = chrono::high_resolution_clock::now() - __start; - if (__max_elapsed != chrono::nanoseconds::zero() && __max_elapsed < __elapsed) - return false; // timeout failure - if (__bf(__elapsed)) - return false; // _BFn completion means failure +template <class _Fn, class _BFn> +_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_poll_with_backoff( + _Fn&& __f, _BFn&& __bf, chrono::nanoseconds __max_elapsed = chrono::nanoseconds::zero()) { + auto const __start = chrono::high_resolution_clock::now(); + for (int __count = 0;;) { + if (__f()) + return true; // _Fn completion means success + if (__count < __libcpp_polling_count) { + __count += 1; + continue; } + chrono::nanoseconds const __elapsed = chrono::high_resolution_clock::now() - __start; + if (__max_elapsed != chrono::nanoseconds::zero() && __max_elapsed < __elapsed) + return false; // timeout failure + if (__bf(__elapsed)) + return false; // _BFn completion means failure + } } // A trivial backoff policy that always immediately returns the control to @@ -60,10 +60,7 @@ bool __libcpp_thread_poll_with_backoff(_Fn&& __f, _BFn&& __bf, chrono::nanosecon // so this should most likely only be used on single-threaded systems where there // are no other threads to compete with. struct __spinning_backoff_policy { - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR - bool operator()(chrono::nanoseconds const&) const { - return false; - } + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator()(chrono::nanoseconds const&) const { return false; } }; _LIBCPP_END_NAMESPACE_STD diff --git a/libcxx/include/__thread/this_thread.h b/libcxx/include/__thread/this_thread.h index 6b79aee9531f..6fb42533aab7 100644 --- a/libcxx/include/__thread/this_thread.h +++ b/libcxx/include/__thread/this_thread.h @@ -27,56 +27,43 @@ _LIBCPP_PUSH_MACROS _LIBCPP_BEGIN_NAMESPACE_STD -namespace this_thread -{ +namespace this_thread { _LIBCPP_EXPORTED_FROM_ABI void sleep_for(const chrono::nanoseconds& __ns); template <class _Rep, class _Period> -_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 <climits> - // and issues with long double folding on PowerPC with GCC. - _LIBCPP_CONSTEXPR chrono::duration<long double> __max = - chrono::duration<long double>(9223372036.0L); - chrono::nanoseconds __ns; - if (__d < __max) - { - __ns = chrono::duration_cast<chrono::nanoseconds>(__d); - if (__ns < __d) - ++__ns; - } - else - __ns = chrono::nanoseconds::max(); - this_thread::sleep_for(__ns); - } +_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 <climits> + // and issues with long double folding on PowerPC with GCC. + _LIBCPP_CONSTEXPR chrono::duration<long double> __max = chrono::duration<long double>(9223372036.0L); + chrono::nanoseconds __ns; + if (__d < __max) { + __ns = chrono::duration_cast<chrono::nanoseconds>(__d); + if (__ns < __d) + ++__ns; + } else + __ns = chrono::nanoseconds::max(); + this_thread::sleep_for(__ns); + } } template <class _Clock, class _Duration> -_LIBCPP_HIDE_FROM_ABI void -sleep_until(const chrono::time_point<_Clock, _Duration>& __t) -{ - mutex __mut; - condition_variable __cv; - unique_lock<mutex> __lk(__mut); - while (_Clock::now() < __t) - __cv.wait_until(__lk, __t); +_LIBCPP_HIDE_FROM_ABI void sleep_until(const chrono::time_point<_Clock, _Duration>& __t) { + mutex __mut; + condition_variable __cv; + unique_lock<mutex> __lk(__mut); + while (_Clock::now() < __t) + __cv.wait_until(__lk, __t); } template <class _Duration> -inline _LIBCPP_HIDE_FROM_ABI -void -sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t) -{ - this_thread::sleep_for(__t - chrono::steady_clock::now()); +inline _LIBCPP_HIDE_FROM_ABI void sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t) { + this_thread::sleep_for(__t - chrono::steady_clock::now()); } -inline _LIBCPP_HIDE_FROM_ABI -void yield() _NOEXCEPT {__libcpp_thread_yield();} +inline _LIBCPP_HIDE_FROM_ABI void yield() _NOEXCEPT { __libcpp_thread_yield(); } } // namespace this_thread diff --git a/libcxx/include/__thread/thread.h b/libcxx/include/__thread/thread.h index ee37a0320d69..f3300752ac9e 100644 --- a/libcxx/include/__thread/thread.h +++ b/libcxx/include/__thread/thread.h @@ -34,262 +34,220 @@ _LIBCPP_BEGIN_NAMESPACE_STD -template <class _Tp> class __thread_specific_ptr; +template <class _Tp> +class __thread_specific_ptr; class _LIBCPP_EXPORTED_FROM_ABI __thread_struct; class _LIBCPP_HIDDEN __thread_struct_imp; class __assoc_sub_state; _LIBCPP_EXPORTED_FROM_ABI __thread_specific_ptr<__thread_struct>& __thread_local_data(); -class _LIBCPP_EXPORTED_FROM_ABI __thread_struct -{ - __thread_struct_imp* __p_; +class _LIBCPP_EXPORTED_FROM_ABI __thread_struct { + __thread_struct_imp* __p_; + + __thread_struct(const __thread_struct&); + __thread_struct& operator=(const __thread_struct&); - __thread_struct(const __thread_struct&); - __thread_struct& operator=(const __thread_struct&); public: - __thread_struct(); - ~__thread_struct(); + __thread_struct(); + ~__thread_struct(); - void notify_all_at_thread_exit(condition_variable*, mutex*); - void __make_ready_at_thread_exit(__assoc_sub_state*); + 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_; +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_EXPORTED_FROM_ABI __thread_specific_ptr<__thread_struct>& __thread_local_data(); + // 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_EXPORTED_FROM_ABI __thread_specific_ptr<__thread_struct>& __thread_local_data(); - __thread_specific_ptr(const __thread_specific_ptr&); - __thread_specific_ptr& operator=(const __thread_specific_ptr&); + __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*); + _LIBCPP_HIDDEN static void _LIBCPP_TLS_DESTRUCTOR_CC __at_thread_exit(void*); public: - typedef _Tp* pointer; + typedef _Tp* pointer; - ~__thread_specific_ptr(); + ~__thread_specific_ptr(); - _LIBCPP_HIDE_FROM_ABI - pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));} - _LIBCPP_HIDE_FROM_ABI - pointer operator*() const {return *get();} - _LIBCPP_HIDE_FROM_ABI - pointer operator->() const {return get();} - void set_pointer(pointer __p); + _LIBCPP_HIDE_FROM_ABI pointer get() const { return static_cast<_Tp*>(__libcpp_tls_get(__key_)); } + _LIBCPP_HIDE_FROM_ABI pointer operator*() const { return *get(); } + _LIBCPP_HIDE_FROM_ABI 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); +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); +__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. +__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_UNCATEGORIZED(get() == nullptr, - "Attempting to overwrite thread local data"); - std::__libcpp_tls_set(__key_, __p); +void __thread_specific_ptr<_Tp>::set_pointer(pointer __p) { + _LIBCPP_ASSERT_UNCATEGORIZED(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_HIDE_FROM_ABI - size_t operator()(__thread_id __v) const _NOEXCEPT - { - return hash<__libcpp_thread_id>()(__v.__id_); - } +template <> +struct _LIBCPP_TEMPLATE_VIS hash<__thread_id> : public __unary_function<__thread_id, size_t> { + _LIBCPP_HIDE_FROM_ABI size_t operator()(__thread_id __v) const _NOEXCEPT { + return hash<__libcpp_thread_id>()(__v.__id_); + } }; #ifndef _LIBCPP_HAS_NO_LOCALIZATION template <class _CharT, class _Traits> _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) { - // [thread.thread.id]/9 - // Effects: Inserts the text representation for charT of id into out. - // - // [thread.thread.id]/2 - // The text representation for the character type charT of an - // object of type thread::id is an unspecified sequence of charT - // such that, for two objects of type thread::id x and y, if - // x == y is true, the thread::id objects have the same text - // representation, and if x != y is true, the thread::id objects - // have distinct text representations. - // - // Since various flags in the output stream can affect how the - // thread id is represented (e.g. numpunct or showbase), we - // use a temporary stream instead and just output the thread - // id representation as a string. - - basic_ostringstream<_CharT, _Traits> __sstr; - __sstr.imbue(locale::classic()); - __sstr << __id.__id_; - return __os << __sstr.str(); + // [thread.thread.id]/9 + // Effects: Inserts the text representation for charT of id into out. + // + // [thread.thread.id]/2 + // The text representation for the character type charT of an + // object of type thread::id is an unspecified sequence of charT + // such that, for two objects of type thread::id x and y, if + // x == y is true, the thread::id objects have the same text + // representation, and if x != y is true, the thread::id objects + // have distinct text representations. + // + // Since various flags in the output stream can affect how the + // thread id is represented (e.g. numpunct or showbase), we + // use a temporary stream instead and just output the thread + // id representation as a string. + + basic_ostringstream<_CharT, _Traits> __sstr; + __sstr.imbue(locale::classic()); + __sstr << __id.__id_; + return __os << __sstr.str(); } #endif // _LIBCPP_HAS_NO_LOCALIZATION -class _LIBCPP_EXPORTED_FROM_ABI thread -{ - __libcpp_thread_t __t_; +class _LIBCPP_EXPORTED_FROM_ABI thread { + __libcpp_thread_t __t_; + + thread(const thread&); + thread& operator=(const thread&); - thread(const thread&); - thread& operator=(const thread&); public: - typedef __thread_id id; - typedef __libcpp_thread_t native_handle_type; + typedef __thread_id id; + typedef __libcpp_thread_t native_handle_type; - _LIBCPP_HIDE_FROM_ABI - thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {} + _LIBCPP_HIDE_FROM_ABI 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); + 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); #endif - ~thread(); - - _LIBCPP_HIDE_FROM_ABI - thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) { - __t.__t_ = _LIBCPP_NULL_THREAD; - } - - _LIBCPP_HIDE_FROM_ABI - thread& operator=(thread&& __t) _NOEXCEPT { - if (!__libcpp_thread_isnull(&__t_)) - terminate(); - __t_ = __t.__t_; - __t.__t_ = _LIBCPP_NULL_THREAD; - return *this; - } - - _LIBCPP_HIDE_FROM_ABI - void swap(thread& __t) _NOEXCEPT {std::swap(__t_, __t.__t_);} - - _LIBCPP_HIDE_FROM_ABI - bool joinable() const _NOEXCEPT {return !__libcpp_thread_isnull(&__t_);} - void join(); - void detach(); - _LIBCPP_HIDE_FROM_ABI - id get_id() const _NOEXCEPT {return __libcpp_thread_get_id(&__t_);} - _LIBCPP_HIDE_FROM_ABI - native_handle_type native_handle() _NOEXCEPT {return __t_;} - - static unsigned hardware_concurrency() _NOEXCEPT; + ~thread(); + + _LIBCPP_HIDE_FROM_ABI thread(thread&& __t) _NOEXCEPT : __t_(__t.__t_) { __t.__t_ = _LIBCPP_NULL_THREAD; } + + _LIBCPP_HIDE_FROM_ABI thread& operator=(thread&& __t) _NOEXCEPT { + if (!__libcpp_thread_isnull(&__t_)) + terminate(); + __t_ = __t.__t_; + __t.__t_ = _LIBCPP_NULL_THREAD; + return *this; + } + + _LIBCPP_HIDE_FROM_ABI void swap(thread& __t) _NOEXCEPT { std::swap(__t_, __t.__t_); } + + _LIBCPP_HIDE_FROM_ABI bool joinable() const _NOEXCEPT { return !__libcpp_thread_isnull(&__t_); } + void join(); + void detach(); + _LIBCPP_HIDE_FROM_ABI id get_id() const _NOEXCEPT { return __libcpp_thread_get_id(&__t_); } + _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() _NOEXCEPT { return __t_; } + + static unsigned hardware_concurrency() _NOEXCEPT; }; #ifndef _LIBCPP_CXX03_LANG -template <class _TSp, class _Fp, class ..._Args, size_t ..._Indices> -inline _LIBCPP_HIDE_FROM_ABI -void -__thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>) -{ - std::__invoke(std::move(std::get<1>(__t)), std::move(std::get<_Indices>(__t))...); +template <class _TSp, class _Fp, class... _Args, size_t... _Indices> +inline _LIBCPP_HIDE_FROM_ABI void __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>) { + std::__invoke(std::move(std::get<1>(__t)), std::move(std::get<_Indices>(__t))...); } template <class _Fp> -_LIBCPP_HIDE_FROM_ABI -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(std::get<0>(*__p.get()).release()); - typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index; - std::__thread_execute(*__p.get(), _Index()); - return nullptr; +_LIBCPP_HIDE_FROM_ABI 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(std::get<0>(*__p.get()).release()); + typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index; + std::__thread_execute(*__p.get(), _Index()); + return nullptr; } -template <class _Fp, class ..._Args, - class - > -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(std::move(__tsp), - std::forward<_Fp>(__f), - std::forward<_Args>(__args)...)); - int __ec = std::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get()); - if (__ec == 0) - __p.release(); - else - __throw_system_error(__ec, "thread constructor failed"); +template <class _Fp, class... _Args, class > +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(std::move(__tsp), std::forward<_Fp>(__f), std::forward<_Args>(__args)...)); + int __ec = std::__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 +#else // _LIBCPP_CXX03_LANG template <class _Fp> 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_; + // 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 <class _Fp> -_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; +_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 <class _Fp> -thread::thread(_Fp __f) -{ - - typedef __thread_invoke_pair<_Fp> _InvokePair; - typedef unique_ptr<_InvokePair> _PairPtr; - _PairPtr __pp(new _InvokePair(__f)); - int __ec = std::__libcpp_thread_create(&__t_, &__thread_proxy_cxx03<_InvokePair>, __pp.get()); - if (__ec == 0) - __pp.release(); - else - __throw_system_error(__ec, "thread constructor failed"); +thread::thread(_Fp __f) { + typedef __thread_invoke_pair<_Fp> _InvokePair; + typedef unique_ptr<_InvokePair> _PairPtr; + _PairPtr __pp(new _InvokePair(__f)); + int __ec = std::__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_HIDE_FROM_ABI -void swap(thread& __x, thread& __y) _NOEXCEPT {__x.swap(__y);} +inline _LIBCPP_HIDE_FROM_ABI void swap(thread& __x, thread& __y) _NOEXCEPT { __x.swap(__y); } _LIBCPP_END_NAMESPACE_STD diff --git a/libcxx/include/__thread/timed_backoff_policy.h b/libcxx/include/__thread/timed_backoff_policy.h index 456cfcc3d986..525f52b34914 100644 --- a/libcxx/include/__thread/timed_backoff_policy.h +++ b/libcxx/include/__thread/timed_backoff_policy.h @@ -17,25 +17,23 @@ # include <__chrono/duration.h> # include <__threading_support> -#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) -# pragma GCC system_header -#endif +# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +# endif _LIBCPP_BEGIN_NAMESPACE_STD struct __libcpp_timed_backoff_policy { - _LIBCPP_HIDE_FROM_ABI - bool operator()(chrono::nanoseconds __elapsed) const - { - if(__elapsed > chrono::milliseconds(128)) - __libcpp_thread_sleep_for(chrono::milliseconds(8)); - else if(__elapsed > chrono::microseconds(64)) - __libcpp_thread_sleep_for(__elapsed / 2); - else if(__elapsed > chrono::microseconds(4)) - __libcpp_thread_yield(); - else - {} // poll - return false; + _LIBCPP_HIDE_FROM_ABI bool operator()(chrono::nanoseconds __elapsed) const { + if (__elapsed > chrono::milliseconds(128)) + __libcpp_thread_sleep_for(chrono::milliseconds(8)); + else if (__elapsed > chrono::microseconds(64)) + __libcpp_thread_sleep_for(__elapsed / 2); + else if (__elapsed > chrono::microseconds(4)) + __libcpp_thread_yield(); + else { + } // poll + return false; } }; |
