Skip to content

Commit 053d9e5

Browse files
committed
[libc++] Move __thread_id out of <__threading_support>
This makes <__threading_support> closer to handling only the bridge between the system's implementation of threading and the rest of libc++. Differential Revision: https://reviews.llvm.org/D154464
1 parent 477f6bc commit 053d9e5

File tree

19 files changed

+162
-163
lines changed

19 files changed

+162
-163
lines changed

libcxx/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ Deprecations and Removals
100100
- ``<string>``, ``<string_view>``, and ``<mutex>`` no longer include ``<functional>``
101101
in any C++ version (it was previously included in C++20 and earlier).
102102

103+
- ``<atomic>``, ``<barrier>``, ``<latch>``, ``<numeric>``, ``<semaphore>`` and ``<shared_mutex>`` no longer include ``<iosfwd>``
104+
(it was previously included in all Standard versions).
105+
103106
- The headers ``<experimental/algorithm>`` and ``<experimental/functional>`` have been removed, since all the contents
104107
have been implemented in namespace ``std`` for at least two releases.
105108

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ set(files
676676
__system_error/error_condition.h
677677
__system_error/system_error.h
678678
__thread/formatter.h
679+
__thread/id.h
679680
__thread/poll_with_backoff.h
680681
__thread/this_thread.h
681682
__thread/thread.h

libcxx/include/__condition_variable/condition_variable.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <__type_traits/enable_if.h>
2121
#include <__type_traits/is_floating_point.h>
2222
#include <__utility/move.h>
23+
#include <limits>
2324
#include <ratio>
2425

2526
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)

libcxx/include/__thread/formatter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <__format/formatter.h>
1818
#include <__format/formatter_integral.h>
1919
#include <__format/parser_std_format_spec.h>
20-
#include <__threading_support>
20+
#include <__thread/id.h>
2121
#include <__type_traits/conditional.h>
2222
#include <__type_traits/is_pointer.h>
2323
#include <__type_traits/is_same.h>

libcxx/include/__thread/id.h

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// -*- C++ -*-
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef _LIBCPP___THREAD_ID_H
11+
#define _LIBCPP___THREAD_ID_H
12+
13+
#include <__compare/ordering.h>
14+
#include <__config>
15+
#include <__fwd/hash.h>
16+
#include <__threading_support>
17+
#include <iosfwd>
18+
19+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20+
# pragma GCC system_header
21+
#endif
22+
23+
_LIBCPP_BEGIN_NAMESPACE_STD
24+
25+
#ifndef _LIBCPP_HAS_NO_THREADS
26+
27+
class _LIBCPP_EXPORTED_FROM_ABI thread;
28+
class _LIBCPP_EXPORTED_FROM_ABI __thread_id;
29+
30+
namespace this_thread
31+
{
32+
33+
_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
34+
35+
} // namespace this_thread
36+
37+
template<> struct hash<__thread_id>;
38+
39+
class _LIBCPP_TEMPLATE_VIS __thread_id
40+
{
41+
// FIXME: pthread_t is a pointer on Darwin but a long on Linux.
42+
// NULL is the no-thread value on Darwin. Someone needs to check
43+
// on other platforms. We assume 0 works everywhere for now.
44+
__libcpp_thread_id __id_;
45+
46+
static _LIBCPP_HIDE_FROM_ABI
47+
bool __lt_impl(__thread_id __x, __thread_id __y) _NOEXCEPT
48+
{ // id==0 is always less than any other thread_id
49+
if (__x.__id_ == 0) return __y.__id_ != 0;
50+
if (__y.__id_ == 0) return false;
51+
return __libcpp_thread_id_less(__x.__id_, __y.__id_);
52+
}
53+
54+
public:
55+
_LIBCPP_INLINE_VISIBILITY
56+
__thread_id() _NOEXCEPT : __id_(0) {}
57+
58+
_LIBCPP_INLINE_VISIBILITY
59+
void __reset() { __id_ = 0; }
60+
61+
friend _LIBCPP_HIDE_FROM_ABI bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT;
62+
#if _LIBCPP_STD_VER <= 17
63+
friend _LIBCPP_HIDE_FROM_ABI bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT;
64+
#else // _LIBCPP_STD_VER <= 17
65+
friend _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept;
66+
#endif // _LIBCPP_STD_VER <= 17
67+
68+
template<class _CharT, class _Traits>
69+
friend
70+
_LIBCPP_INLINE_VISIBILITY
71+
basic_ostream<_CharT, _Traits>&
72+
operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id);
73+
74+
private:
75+
_LIBCPP_INLINE_VISIBILITY
76+
__thread_id(__libcpp_thread_id __id) : __id_(__id) {}
77+
78+
_LIBCPP_HIDE_FROM_ABI friend __libcpp_thread_id __get_underlying_id(const __thread_id __id) { return __id.__id_; }
79+
80+
friend __thread_id this_thread::get_id() _NOEXCEPT;
81+
friend class _LIBCPP_EXPORTED_FROM_ABI thread;
82+
friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
83+
};
84+
85+
inline _LIBCPP_HIDE_FROM_ABI
86+
bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT {
87+
// Don't pass id==0 to underlying routines
88+
if (__x.__id_ == 0)
89+
return __y.__id_ == 0;
90+
if (__y.__id_ == 0)
91+
return false;
92+
return __libcpp_thread_id_equal(__x.__id_, __y.__id_);
93+
}
94+
95+
#if _LIBCPP_STD_VER <= 17
96+
97+
inline _LIBCPP_HIDE_FROM_ABI
98+
bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT {
99+
return !(__x == __y);
100+
}
101+
102+
inline _LIBCPP_HIDE_FROM_ABI
103+
bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT {
104+
return __thread_id::__lt_impl(__x.__id_, __y.__id_);
105+
}
106+
107+
inline _LIBCPP_HIDE_FROM_ABI bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__y < __x); }
108+
inline _LIBCPP_HIDE_FROM_ABI bool operator>(__thread_id __x, __thread_id __y) _NOEXCEPT { return __y < __x; }
109+
inline _LIBCPP_HIDE_FROM_ABI bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__x < __y); }
110+
111+
#else // _LIBCPP_STD_VER <= 17
112+
113+
inline _LIBCPP_HIDE_FROM_ABI
114+
strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept {
115+
if (__x == __y)
116+
return strong_ordering::equal;
117+
if (__thread_id::__lt_impl(__x, __y))
118+
return strong_ordering::less;
119+
return strong_ordering::greater;
120+
}
121+
122+
#endif // _LIBCPP_STD_VER <= 17
123+
124+
namespace this_thread
125+
{
126+
127+
_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
128+
129+
} // namespace this_thread
130+
131+
namespace this_thread
132+
{
133+
134+
inline _LIBCPP_INLINE_VISIBILITY
135+
__thread_id
136+
get_id() _NOEXCEPT
137+
{
138+
return __libcpp_thread_get_current_id();
139+
}
140+
141+
} // namespace this_thread
142+
143+
#endif // !_LIBCPP_HAS_NO_THREADS
144+
145+
_LIBCPP_END_NAMESPACE_STD
146+
147+
#endif // _LIBCPP___THREAD_ID_H

libcxx/include/__thread/thread.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
#include <__memory/unique_ptr.h>
1919
#include <__mutex/mutex.h>
2020
#include <__system_error/system_error.h>
21+
#include <__thread/id.h>
2122
#include <__threading_support>
2223
#include <__utility/forward.h>
24+
#include <iosfwd>
2325
#include <tuple>
2426

2527
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)

libcxx/include/__threading_support

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@
1313
#include <__availability>
1414
#include <__chrono/convert_to_timespec.h>
1515
#include <__chrono/duration.h>
16-
#include <__compare/ordering.h>
1716
#include <__config>
18-
#include <__fwd/hash.h>
1917
#include <__thread/poll_with_backoff.h>
2018
#include <errno.h>
21-
#include <iosfwd>
22-
#include <limits>
2319

2420
#ifdef __MVS__
2521
# include <__support/ibm/nanosleep.h>
@@ -589,118 +585,8 @@ int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
589585

590586
#endif
591587

592-
593588
#endif // !_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL || _LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL
594589

595-
class _LIBCPP_EXPORTED_FROM_ABI thread;
596-
class _LIBCPP_EXPORTED_FROM_ABI __thread_id;
597-
598-
namespace this_thread
599-
{
600-
601-
_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
602-
603-
} // namespace this_thread
604-
605-
template<> struct hash<__thread_id>;
606-
607-
class _LIBCPP_TEMPLATE_VIS __thread_id
608-
{
609-
// FIXME: pthread_t is a pointer on Darwin but a long on Linux.
610-
// NULL is the no-thread value on Darwin. Someone needs to check
611-
// on other platforms. We assume 0 works everywhere for now.
612-
__libcpp_thread_id __id_;
613-
614-
static _LIBCPP_HIDE_FROM_ABI
615-
bool __lt_impl(__thread_id __x, __thread_id __y) _NOEXCEPT
616-
{ // id==0 is always less than any other thread_id
617-
if (__x.__id_ == 0) return __y.__id_ != 0;
618-
if (__y.__id_ == 0) return false;
619-
return __libcpp_thread_id_less(__x.__id_, __y.__id_);
620-
}
621-
622-
public:
623-
_LIBCPP_INLINE_VISIBILITY
624-
__thread_id() _NOEXCEPT : __id_(0) {}
625-
626-
_LIBCPP_INLINE_VISIBILITY
627-
void __reset() { __id_ = 0; }
628-
629-
friend _LIBCPP_HIDE_FROM_ABI bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT;
630-
#if _LIBCPP_STD_VER <= 17
631-
friend _LIBCPP_HIDE_FROM_ABI bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT;
632-
#else // _LIBCPP_STD_VER <= 17
633-
friend _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept;
634-
#endif // _LIBCPP_STD_VER <= 17
635-
636-
template<class _CharT, class _Traits>
637-
friend
638-
_LIBCPP_INLINE_VISIBILITY
639-
basic_ostream<_CharT, _Traits>&
640-
operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id);
641-
642-
private:
643-
_LIBCPP_INLINE_VISIBILITY
644-
__thread_id(__libcpp_thread_id __id) : __id_(__id) {}
645-
646-
_LIBCPP_HIDE_FROM_ABI friend __libcpp_thread_id __get_underlying_id(const __thread_id __id) { return __id.__id_; }
647-
648-
friend __thread_id this_thread::get_id() _NOEXCEPT;
649-
friend class _LIBCPP_EXPORTED_FROM_ABI thread;
650-
friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
651-
};
652-
653-
inline _LIBCPP_HIDE_FROM_ABI
654-
bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT {
655-
// Don't pass id==0 to underlying routines
656-
if (__x.__id_ == 0)
657-
return __y.__id_ == 0;
658-
if (__y.__id_ == 0)
659-
return false;
660-
return __libcpp_thread_id_equal(__x.__id_, __y.__id_);
661-
}
662-
663-
#if _LIBCPP_STD_VER <= 17
664-
665-
inline _LIBCPP_HIDE_FROM_ABI
666-
bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT {
667-
return !(__x == __y);
668-
}
669-
670-
inline _LIBCPP_HIDE_FROM_ABI
671-
bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT {
672-
return __thread_id::__lt_impl(__x.__id_, __y.__id_);
673-
}
674-
675-
inline _LIBCPP_HIDE_FROM_ABI bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__y < __x); }
676-
inline _LIBCPP_HIDE_FROM_ABI bool operator>(__thread_id __x, __thread_id __y) _NOEXCEPT { return __y < __x; }
677-
inline _LIBCPP_HIDE_FROM_ABI bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__x < __y); }
678-
679-
#else // _LIBCPP_STD_VER <= 17
680-
681-
inline _LIBCPP_HIDE_FROM_ABI
682-
strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept {
683-
if (__x == __y)
684-
return strong_ordering::equal;
685-
if (__thread_id::__lt_impl(__x, __y))
686-
return strong_ordering::less;
687-
return strong_ordering::greater;
688-
}
689-
690-
#endif // _LIBCPP_STD_VER <= 17
691-
692-
namespace this_thread
693-
{
694-
695-
inline _LIBCPP_INLINE_VISIBILITY
696-
__thread_id
697-
get_id() _NOEXCEPT
698-
{
699-
return __libcpp_thread_get_current_id();
700-
}
701-
702-
} // namespace this_thread
703-
704590
#endif // !_LIBCPP_HAS_NO_THREADS
705591

706592
_LIBCPP_END_NAMESPACE_STD

libcxx/include/module.modulemap.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,7 @@ module std [system] {
16191619

16201620
module __thread {
16211621
module formatter { private header "__thread/formatter.h" }
1622+
module id { private header "__thread/id.h" }
16221623
module poll_with_backoff { private header "__thread/poll_with_backoff.h" }
16231624
module this_thread { private header "__thread/this_thread.h" }
16241625
module thread { private header "__thread/thread.h" }

libcxx/include/mutex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,11 @@ template<class Callable, class ...Args>
196196
#include <__mutex/mutex.h>
197197
#include <__mutex/tag_types.h>
198198
#include <__mutex/unique_lock.h>
199+
#include <__thread/id.h>
199200
#include <__threading_support>
200201
#include <__utility/forward.h>
201202
#include <cstdint>
203+
#include <limits>
202204
#ifndef _LIBCPP_CXX03_LANG
203205
# include <tuple>
204206
#endif

libcxx/src/mutex.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include <__assert>
10+
#include <__thread/id.h>
1011
#include <limits>
1112
#include <mutex>
1213

0 commit comments

Comments
 (0)