Skip to content

Commit 7162fd7

Browse files
authored
[libc++] Split the monolithic __threading_support header (#79654)
The <__threading_support> header is a huge beast and it's really difficult to navigate. I find myself struggling to find what I want every time I have to open it, and I've been considering splitting it up for years for that reason. This patch aims not to contain any functional change. The various implementations of the threading base are simply moved to separate headers and then the individual headers are simplified in mechanical ways. For example, we used to have redundant declarations of all the functions at the top of `__threading_support`, and those are removed since they are not needed anymore. The various #ifdefs are also simplified and removed when they become unnecessary. Finally, this patch adds documentation for the API we expect from any threading implementation.
1 parent f89d707 commit 7162fd7

34 files changed

+749
-490
lines changed

libcxx/docs/DesignDocs/ThreadingSupportAPI.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ These different models provide entirely different interfaces from each
1414
other. To address this libc++ wraps the underlying threading API in a new and
1515
consistent API, which it uses internally to implement threading primitives.
1616

17-
The ``<__threading_support>`` header is where libc++ defines its internal
18-
threading interface. It contains forward declarations of the internal threading
19-
interface as well as definitions for the interface.
17+
The ``<__thread/support.h>`` header is where libc++ defines its internal
18+
threading interface. It documents the functions and declarations required
19+
to fullfil the internal threading interface.
2020

2121
External Threading API and the ``<__external_threading>`` header
2222
================================================================
@@ -25,10 +25,10 @@ In order to support vendors with custom threading API's libc++ allows the
2525
entire internal threading interface to be provided by an external,
2626
vendor provided, header.
2727

28-
When ``_LIBCPP_HAS_THREAD_API_EXTERNAL`` is defined the ``<__threading_support>``
28+
When ``_LIBCPP_HAS_THREAD_API_EXTERNAL`` is defined the ``<__thread/support.h>``
2929
header simply forwards to the ``<__external_threading>`` header (which must exist).
3030
It is expected that the ``<__external_threading>`` header provide the exact
31-
interface normally provided by ``<__threading_support>``.
31+
interface normally provided by ``<__thread/support.h>``.
3232

3333
External Threading Library
3434
==========================
@@ -58,6 +58,10 @@ Threading Configuration Macros
5858
This macro is defined when libc++ should use POSIX threads to implement the
5959
internal threading API.
6060

61+
**_LIBCPP_HAS_THREAD_API_C11**
62+
This macro is defined when libc++ should use C11 threads to implement the
63+
internal threading API.
64+
6165
**_LIBCPP_HAS_THREAD_API_WIN32**
6266
This macro is defined when libc++ should use Win32 threads to implement the
6367
internal threading API.

libcxx/include/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,10 +688,14 @@ set(files
688688
__thread/id.h
689689
__thread/jthread.h
690690
__thread/poll_with_backoff.h
691+
__thread/support.h
692+
__thread/support/c11.h
693+
__thread/support/external.h
694+
__thread/support/pthread.h
695+
__thread/support/windows.h
691696
__thread/this_thread.h
692697
__thread/thread.h
693698
__thread/timed_backoff_policy.h
694-
__threading_support
695699
__tree
696700
__tuple/make_tuple_types.h
697701
__tuple/pair_like.h

libcxx/include/__atomic/atomic_flag.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <__atomic/memory_order.h>
1616
#include <__chrono/duration.h>
1717
#include <__config>
18-
#include <__threading_support>
18+
#include <__thread/support.h>
1919
#include <cstdint>
2020

2121
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)

libcxx/include/__atomic/atomic_sync.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <__config>
1818
#include <__memory/addressof.h>
1919
#include <__thread/poll_with_backoff.h>
20-
#include <__threading_support>
20+
#include <__thread/support.h>
2121
#include <__type_traits/decay.h>
2222
#include <cstring>
2323

libcxx/include/__condition_variable/condition_variable.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
#ifndef _LIBCPP___CONDITION_VARIABLE_CONDITION_VARIABLE_H
1010
#define _LIBCPP___CONDITION_VARIABLE_CONDITION_VARIABLE_H
1111

12+
#include <__chrono/duration.h>
1213
#include <__chrono/steady_clock.h>
1314
#include <__chrono/system_clock.h>
1415
#include <__chrono/time_point.h>
1516
#include <__config>
1617
#include <__mutex/mutex.h>
1718
#include <__mutex/unique_lock.h>
1819
#include <__system_error/system_error.h>
19-
#include <__threading_support>
20+
#include <__thread/support.h>
2021
#include <__type_traits/enable_if.h>
2122
#include <__type_traits/is_floating_point.h>
2223
#include <__utility/move.h>

libcxx/include/__mutex/mutex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#define _LIBCPP___MUTEX_MUTEX_H
1111

1212
#include <__config>
13-
#include <__threading_support>
13+
#include <__thread/support.h>
1414
#include <__type_traits/is_nothrow_default_constructible.h>
1515

1616
#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
@@ -43,7 +43,7 @@ struct _LIBCPP_TEMPLATE_VIS formatter<__thread_id, _CharT> {
4343

4444
template <class _FormatContext>
4545
_LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(__thread_id __id, _FormatContext& __ctx) const {
46-
// In __threading_support __libcpp_thread_id is either a
46+
// In __thread/support/pthread.h, __libcpp_thread_id is either a
4747
// unsigned long long or a pthread_t.
4848
//
4949
// The type of pthread_t is left unspecified in POSIX so it can be any

libcxx/include/__thread/id.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <__config>
1515
#include <__fwd/hash.h>
1616
#include <__fwd/ostream.h>
17-
#include <__threading_support>
17+
#include <__thread/support.h>
1818

1919
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2020
# pragma GCC system_header

libcxx/include/__thread/jthread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include <__functional/invoke.h>
1616
#include <__stop_token/stop_source.h>
1717
#include <__stop_token/stop_token.h>
18+
#include <__thread/support.h>
1819
#include <__thread/thread.h>
19-
#include <__threading_support>
2020
#include <__type_traits/decay.h>
2121
#include <__type_traits/is_constructible.h>
2222
#include <__type_traits/is_same.h>

libcxx/include/__thread/support.h

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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_SUPPORT_H
11+
#define _LIBCPP___THREAD_SUPPORT_H
12+
13+
#include <__config>
14+
15+
#ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
16+
# pragma GCC system_header
17+
#endif
18+
19+
/*
20+
21+
//
22+
// The library supports multiple implementations of the basic threading functionality.
23+
// The following functionality must be provided by any implementation:
24+
//
25+
26+
using __libcpp_timespec_t = ...;
27+
28+
//
29+
// Mutex
30+
//
31+
using __libcpp_mutex_t = ...;
32+
#define _LIBCPP_MUTEX_INITIALIZER ...
33+
34+
using __libcpp_recursive_mutex_t = ...;
35+
36+
int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t*);
37+
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t*);
38+
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t*);
39+
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t*);
40+
int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t*);
41+
42+
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_mutex_lock(__libcpp_mutex_t*);
43+
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS bool __libcpp_mutex_trylock(__libcpp_mutex_t*);
44+
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_mutex_unlock(__libcpp_mutex_t*);
45+
int __libcpp_mutex_destroy(__libcpp_mutex_t*);
46+
47+
//
48+
// Condition Variable
49+
//
50+
using __libcpp_condvar_t = ...;
51+
#define _LIBCPP_CONDVAR_INITIALIZER ...
52+
53+
int __libcpp_condvar_signal(__libcpp_condvar_t*);
54+
int __libcpp_condvar_broadcast(__libcpp_condvar_t*);
55+
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_condvar_wait(__libcpp_condvar_t*, __libcpp_mutex_t*);
56+
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS
57+
int __libcpp_condvar_timedwait(__libcpp_condvar_t*, __libcpp_mutex_t*, __libcpp_timespec_t*);
58+
int __libcpp_condvar_destroy(__libcpp_condvar_t*);
59+
60+
//
61+
// Execute once
62+
//
63+
using __libcpp_exec_once_flag = ...;
64+
#define _LIBCPP_EXEC_ONCE_INITIALIZER ...
65+
66+
int __libcpp_execute_once(__libcpp_exec_once_flag*, void (*__init_routine)());
67+
68+
//
69+
// Thread id
70+
//
71+
using __libcpp_thread_id = ...;
72+
73+
bool __libcpp_thread_id_equal(__libcpp_thread_id, __libcpp_thread_id);
74+
bool __libcpp_thread_id_less(__libcpp_thread_id, __libcpp_thread_id);
75+
76+
//
77+
// Thread
78+
//
79+
#define _LIBCPP_NULL_THREAD ...
80+
using __libcpp_thread_t = ...;
81+
82+
bool __libcpp_thread_isnull(const __libcpp_thread_t*);
83+
int __libcpp_thread_create(__libcpp_thread_t*, void* (*__func)(void*), void* __arg);
84+
__libcpp_thread_id __libcpp_thread_get_current_id();
85+
__libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t*);
86+
int __libcpp_thread_join(__libcpp_thread_t*);
87+
int __libcpp_thread_detach(__libcpp_thread_t*);
88+
void __libcpp_thread_yield();
89+
void __libcpp_thread_sleep_for(const chrono::nanoseconds&);
90+
91+
//
92+
// Thread local storage
93+
//
94+
#define _LIBCPP_TLS_DESTRUCTOR_CC ...
95+
using __libcpp_tls_key = ...;
96+
97+
int __libcpp_tls_create(__libcpp_tls_key*, void (*__at_exit)(void*));
98+
void* __libcpp_tls_get(__libcpp_tls_key);
99+
int __libcpp_tls_set(__libcpp_tls_key, void*);
100+
101+
*/
102+
103+
#if !defined(_LIBCPP_HAS_NO_THREADS)
104+
105+
# if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
106+
# include <__thread/support/external.h>
107+
# elif defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
108+
# include <__thread/support/pthread.h>
109+
# elif defined(_LIBCPP_HAS_THREAD_API_C11)
110+
# include <__thread/support/c11.h>
111+
# elif defined(_LIBCPP_HAS_THREAD_API_WIN32)
112+
# include <__thread/support/windows.h>
113+
# else
114+
# error "No threading API was selected"
115+
# endif
116+
117+
#endif // !_LIBCPP_HAS_NO_THREADS
118+
119+
#endif // _LIBCPP___THREAD_SUPPORT_H

0 commit comments

Comments
 (0)