diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -675,6 +675,7 @@ __system_error/error_condition.h __system_error/system_error.h __thread/formatter.h + __thread/id.h __thread/poll_with_backoff.h __thread/this_thread.h __thread/thread.h diff --git a/libcxx/include/__thread/formatter.h b/libcxx/include/__thread/formatter.h --- a/libcxx/include/__thread/formatter.h +++ b/libcxx/include/__thread/formatter.h @@ -17,7 +17,7 @@ #include <__format/formatter.h> #include <__format/formatter_integral.h> #include <__format/parser_std_format_spec.h> -#include <__threading_support> +#include <__thread/id.h> #include <__type_traits/conditional.h> #include <__type_traits/is_pointer.h> #include <__type_traits/is_same.h> diff --git a/libcxx/include/__thread/id.h b/libcxx/include/__thread/id.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__thread/id.h @@ -0,0 +1,143 @@ +// -*- 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_ID_H +#define _LIBCPP___THREAD_ID_H + +#include <__compare/ordering.h> +#include <__config> +#include <__fwd/hash.h> +#include <__threading_support> +#include + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +class _LIBCPP_EXPORTED_FROM_ABI thread; +class _LIBCPP_EXPORTED_FROM_ABI __thread_id; + +namespace this_thread +{ + +_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT; + +} // namespace this_thread + +template<> struct hash<__thread_id>; + +class _LIBCPP_TEMPLATE_VIS __thread_id +{ + // FIXME: pthread_t is a pointer on Darwin but a long on Linux. + // NULL is the no-thread value on Darwin. Someone needs to check + // on other platforms. We assume 0 works everywhere for now. + __libcpp_thread_id __id_; + + static _LIBCPP_HIDE_FROM_ABI + bool __lt_impl(__thread_id __x, __thread_id __y) _NOEXCEPT + { // id==0 is always less than any other thread_id + if (__x.__id_ == 0) return __y.__id_ != 0; + if (__y.__id_ == 0) return false; + return __libcpp_thread_id_less(__x.__id_, __y.__id_); + } + +public: + _LIBCPP_INLINE_VISIBILITY + __thread_id() _NOEXCEPT : __id_(0) {} + + _LIBCPP_INLINE_VISIBILITY + void __reset() { __id_ = 0; } + + friend _LIBCPP_HIDE_FROM_ABI bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT; +#if _LIBCPP_STD_VER <= 17 + friend _LIBCPP_HIDE_FROM_ABI bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT; +#else // _LIBCPP_STD_VER <= 17 + friend _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept; +#endif // _LIBCPP_STD_VER <= 17 + + template + friend + _LIBCPP_INLINE_VISIBILITY + basic_ostream<_CharT, _Traits>& + operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id); + +private: + _LIBCPP_INLINE_VISIBILITY + __thread_id(__libcpp_thread_id __id) : __id_(__id) {} + + _LIBCPP_HIDE_FROM_ABI friend __libcpp_thread_id __get_underlying_id(const __thread_id __id) { return __id.__id_; } + + friend __thread_id this_thread::get_id() _NOEXCEPT; + friend class _LIBCPP_EXPORTED_FROM_ABI thread; + friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>; +}; + +inline _LIBCPP_HIDE_FROM_ABI +bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT { + // Don't pass id==0 to underlying routines + if (__x.__id_ == 0) + return __y.__id_ == 0; + if (__y.__id_ == 0) + return false; + return __libcpp_thread_id_equal(__x.__id_, __y.__id_); +} + +#if _LIBCPP_STD_VER <= 17 + +inline _LIBCPP_HIDE_FROM_ABI +bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT { + return !(__x == __y); +} + +inline _LIBCPP_HIDE_FROM_ABI +bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT { + return __thread_id::__lt_impl(__x.__id_, __y.__id_); +} + +inline _LIBCPP_HIDE_FROM_ABI bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__y < __x); } +inline _LIBCPP_HIDE_FROM_ABI bool operator>(__thread_id __x, __thread_id __y) _NOEXCEPT { return __y < __x; } +inline _LIBCPP_HIDE_FROM_ABI bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__x < __y); } + +#else // _LIBCPP_STD_VER <= 17 + +inline _LIBCPP_HIDE_FROM_ABI +strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept { + if (__x == __y) + return strong_ordering::equal; + if (__thread_id::__lt_impl(__x, __y)) + return strong_ordering::less; + return strong_ordering::greater; +} + +#endif // _LIBCPP_STD_VER <= 17 + +namespace this_thread +{ + +_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT; + +} // namespace this_thread + +namespace this_thread +{ + +inline _LIBCPP_INLINE_VISIBILITY +__thread_id +get_id() _NOEXCEPT +{ + return __libcpp_thread_get_current_id(); +} + +} // namespace this_thread + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___THREAD_ID_H diff --git a/libcxx/include/__thread/thread.h b/libcxx/include/__thread/thread.h --- a/libcxx/include/__thread/thread.h +++ b/libcxx/include/__thread/thread.h @@ -18,6 +18,7 @@ #include <__memory/unique_ptr.h> #include <__mutex/mutex.h> #include <__system_error/system_error.h> +#include <__thread/id.h> #include <__threading_support> #include <__utility/forward.h> #include diff --git a/libcxx/include/__threading_support b/libcxx/include/__threading_support --- a/libcxx/include/__threading_support +++ b/libcxx/include/__threading_support @@ -13,13 +13,9 @@ #include <__availability> #include <__chrono/convert_to_timespec.h> #include <__chrono/duration.h> -#include <__compare/ordering.h> #include <__config> -#include <__fwd/hash.h> #include <__thread/poll_with_backoff.h> #include -#include -#include #ifdef __MVS__ # include <__support/ibm/nanosleep.h> @@ -589,118 +585,8 @@ #endif - #endif // !_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL || _LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL -class _LIBCPP_EXPORTED_FROM_ABI thread; -class _LIBCPP_EXPORTED_FROM_ABI __thread_id; - -namespace this_thread -{ - -_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT; - -} // namespace this_thread - -template<> struct hash<__thread_id>; - -class _LIBCPP_TEMPLATE_VIS __thread_id -{ - // FIXME: pthread_t is a pointer on Darwin but a long on Linux. - // NULL is the no-thread value on Darwin. Someone needs to check - // on other platforms. We assume 0 works everywhere for now. - __libcpp_thread_id __id_; - - static _LIBCPP_HIDE_FROM_ABI - bool __lt_impl(__thread_id __x, __thread_id __y) _NOEXCEPT - { // id==0 is always less than any other thread_id - if (__x.__id_ == 0) return __y.__id_ != 0; - if (__y.__id_ == 0) return false; - return __libcpp_thread_id_less(__x.__id_, __y.__id_); - } - -public: - _LIBCPP_INLINE_VISIBILITY - __thread_id() _NOEXCEPT : __id_(0) {} - - _LIBCPP_INLINE_VISIBILITY - void __reset() { __id_ = 0; } - - friend _LIBCPP_HIDE_FROM_ABI bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT; -#if _LIBCPP_STD_VER <= 17 - friend _LIBCPP_HIDE_FROM_ABI bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT; -#else // _LIBCPP_STD_VER <= 17 - friend _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept; -#endif // _LIBCPP_STD_VER <= 17 - - template - friend - _LIBCPP_INLINE_VISIBILITY - basic_ostream<_CharT, _Traits>& - operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id); - -private: - _LIBCPP_INLINE_VISIBILITY - __thread_id(__libcpp_thread_id __id) : __id_(__id) {} - - _LIBCPP_HIDE_FROM_ABI friend __libcpp_thread_id __get_underlying_id(const __thread_id __id) { return __id.__id_; } - - friend __thread_id this_thread::get_id() _NOEXCEPT; - friend class _LIBCPP_EXPORTED_FROM_ABI thread; - friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>; -}; - -inline _LIBCPP_HIDE_FROM_ABI -bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT { - // Don't pass id==0 to underlying routines - if (__x.__id_ == 0) - return __y.__id_ == 0; - if (__y.__id_ == 0) - return false; - return __libcpp_thread_id_equal(__x.__id_, __y.__id_); -} - -#if _LIBCPP_STD_VER <= 17 - -inline _LIBCPP_HIDE_FROM_ABI -bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT { - return !(__x == __y); -} - -inline _LIBCPP_HIDE_FROM_ABI -bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT { - return __thread_id::__lt_impl(__x.__id_, __y.__id_); -} - -inline _LIBCPP_HIDE_FROM_ABI bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__y < __x); } -inline _LIBCPP_HIDE_FROM_ABI bool operator>(__thread_id __x, __thread_id __y) _NOEXCEPT { return __y < __x; } -inline _LIBCPP_HIDE_FROM_ABI bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__x < __y); } - -#else // _LIBCPP_STD_VER <= 17 - -inline _LIBCPP_HIDE_FROM_ABI -strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept { - if (__x == __y) - return strong_ordering::equal; - if (__thread_id::__lt_impl(__x, __y)) - return strong_ordering::less; - return strong_ordering::greater; -} - -#endif // _LIBCPP_STD_VER <= 17 - -namespace this_thread -{ - -inline _LIBCPP_INLINE_VISIBILITY -__thread_id -get_id() _NOEXCEPT -{ - return __libcpp_thread_get_current_id(); -} - -} // namespace this_thread - #endif // !_LIBCPP_HAS_NO_THREADS _LIBCPP_END_NAMESPACE_STD diff --git a/libcxx/include/mutex b/libcxx/include/mutex --- a/libcxx/include/mutex +++ b/libcxx/include/mutex @@ -196,6 +196,7 @@ #include <__mutex/mutex.h> #include <__mutex/tag_types.h> #include <__mutex/unique_lock.h> +#include <__thread/id.h> #include <__threading_support> #include <__utility/forward.h> #include diff --git a/libcxx/src/mutex.cpp b/libcxx/src/mutex.cpp --- a/libcxx/src/mutex.cpp +++ b/libcxx/src/mutex.cpp @@ -9,6 +9,7 @@ #include <__assert> #include #include +#include #include "include/atomic_support.h" @@ -144,7 +145,7 @@ void recursive_timed_mutex::lock() { - __thread_id id = this_thread::get_id(); + thread::id id = this_thread::get_id(); unique_lock lk(__m_); if (id ==__id_) { @@ -162,7 +163,7 @@ bool recursive_timed_mutex::try_lock() noexcept { - __thread_id id = this_thread::get_id(); + thread::id id = this_thread::get_id(); unique_lock lk(__m_, try_to_lock); if (lk.owns_lock() && (__count_ == 0 || id == __id_)) { diff --git a/libcxx/utils/data/ignore_format.txt b/libcxx/utils/data/ignore_format.txt --- a/libcxx/utils/data/ignore_format.txt +++ b/libcxx/utils/data/ignore_format.txt @@ -473,6 +473,7 @@ libcxx/include/__system_error/errc.h libcxx/include/thread libcxx/include/__thread/formatter.h +libcxx/include/__thread/id.h libcxx/include/__threading_support libcxx/include/__thread/poll_with_backoff.h libcxx/include/__thread/this_thread.h