Skip to content

Commit cf7d4f5

Browse files
authored
[libc++] Handle threads-related .cpp files like we do all other source files (#71100)
Source files in libc++ are added to the CMake targets only if they are required by the configuration. We do this pretty consistently for all configurations like no-filesystem, no-random-device, etc. but we didn't do it for no-threads. This patch makes this consistent for no-threads, which is helpful in reducing the amount of work required to port libc++ to some platforms without threads. Indeed, with the previous approach, several threads-related source files would end up including headers that might fail to compile properly on some platforms. This issue is sidestepped entirely by making the approach for no-threads consistent with the other configurations.
1 parent c5ecf5a commit cf7d4f5

12 files changed

+100
-118
lines changed

libcxx/src/CMakeLists.txt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@ set(LIBCXX_LIB_CMAKEFILES_DIR "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTOR
44
set(LIBCXX_SOURCES
55
algorithm.cpp
66
any.cpp
7-
atomic.cpp
8-
barrier.cpp
97
bind.cpp
8+
call_once.cpp
109
charconv.cpp
1110
chrono.cpp
12-
condition_variable.cpp
13-
condition_variable_destructor.cpp
1411
error_category.cpp
1512
exception.cpp
1613
filesystem/filesystem_clock.cpp
1714
filesystem/filesystem_error.cpp
1815
filesystem/path_parser.h
1916
filesystem/path.cpp
2017
functional.cpp
21-
future.cpp
2218
hash.cpp
2319
include/apple_availability.h
2420
include/atomic_support.h
@@ -37,8 +33,6 @@ set(LIBCXX_SOURCES
3733
legacy_pointer_safety.cpp
3834
memory.cpp
3935
memory_resource.cpp
40-
mutex.cpp
41-
mutex_destructor.cpp
4236
new_handler.cpp
4337
new_helpers.cpp
4438
optional.cpp
@@ -47,7 +41,6 @@ set(LIBCXX_SOURCES
4741
ryu/d2fixed.cpp
4842
ryu/d2s.cpp
4943
ryu/f2s.cpp
50-
shared_mutex.cpp
5144
stdexcept.cpp
5245
string.cpp
5346
support/runtime/exception_fallback.ipp
@@ -62,14 +55,27 @@ set(LIBCXX_SOURCES
6255
support/runtime/stdexcept_default.ipp
6356
support/runtime/stdexcept_vcruntime.ipp
6457
system_error.cpp
65-
thread.cpp
6658
typeinfo.cpp
6759
valarray.cpp
6860
variant.cpp
6961
vector.cpp
7062
verbose_abort.cpp
7163
)
7264

65+
if (LIBCXX_ENABLE_THREADS)
66+
list(APPEND LIBCXX_SOURCES
67+
atomic.cpp
68+
barrier.cpp
69+
condition_variable_destructor.cpp
70+
condition_variable.cpp
71+
future.cpp
72+
mutex_destructor.cpp
73+
mutex.cpp
74+
shared_mutex.cpp
75+
thread.cpp
76+
)
77+
endif()
78+
7379
if (LIBCXX_ENABLE_RANDOM_DEVICE)
7480
list(APPEND LIBCXX_SOURCES
7581
random.cpp

libcxx/src/atomic.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <__config>
10-
#ifndef _LIBCPP_HAS_NO_THREADS
11-
129
#include <__thread/timed_backoff_policy.h>
1310
#include <atomic>
1411
#include <climits>
@@ -219,5 +216,3 @@ void __libcpp_atomic_wait(__cxx_atomic_contention_t const volatile* __location,
219216
}
220217

221218
_LIBCPP_END_NAMESPACE_STD
222-
223-
#endif //_LIBCPP_HAS_NO_THREADS

libcxx/src/barrier.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <__config>
10-
11-
#ifndef _LIBCPP_HAS_NO_THREADS
12-
139
#include <barrier>
1410
#include <thread>
1511

@@ -93,5 +89,3 @@ void __destroy_barrier_algorithm_base(__barrier_algorithm_base* __barrier)
9389
#endif // !defined(_LIBCPP_HAS_NO_TREE_BARRIER)
9490

9591
_LIBCPP_END_NAMESPACE_STD
96-
97-
#endif //_LIBCPP_HAS_NO_THREADS

libcxx/src/call_once.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <__mutex/once_flag.h>
10+
#include <__utility/exception_guard.h>
11+
12+
#ifndef _LIBCPP_HAS_NO_THREADS
13+
# include <__threading_support>
14+
#endif
15+
16+
#include "include/atomic_support.h"
17+
18+
_LIBCPP_BEGIN_NAMESPACE_STD
19+
20+
// If dispatch_once_f ever handles C++ exceptions, and if one can get to it
21+
// without illegal macros (unexpected macros not beginning with _UpperCase or
22+
// __lowercase), and if it stops spinning waiting threads, then call_once should
23+
// call into dispatch_once_f instead of here. Relevant radar this code needs to
24+
// keep in sync with: 7741191.
25+
26+
#ifndef _LIBCPP_HAS_NO_THREADS
27+
static constinit __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
28+
static constinit __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
29+
#endif
30+
31+
void __call_once(volatile once_flag::_State_type& flag, void* arg,
32+
void (*func)(void*))
33+
{
34+
#if defined(_LIBCPP_HAS_NO_THREADS)
35+
36+
if (flag == once_flag::_Unset) {
37+
auto guard = std::__make_exception_guard([&flag] { flag = once_flag::_Unset; });
38+
flag = once_flag::_Pending;
39+
func(arg);
40+
flag = once_flag::_Complete;
41+
guard.__complete();
42+
}
43+
44+
#else // !_LIBCPP_HAS_NO_THREADS
45+
46+
__libcpp_mutex_lock(&mut);
47+
while (flag == once_flag::_Pending)
48+
__libcpp_condvar_wait(&cv, &mut);
49+
if (flag == once_flag::_Unset) {
50+
auto guard = std::__make_exception_guard([&flag] {
51+
__libcpp_mutex_lock(&mut);
52+
__libcpp_relaxed_store(&flag, once_flag::_Unset);
53+
__libcpp_mutex_unlock(&mut);
54+
__libcpp_condvar_broadcast(&cv);
55+
});
56+
57+
__libcpp_relaxed_store(&flag, once_flag::_Pending);
58+
__libcpp_mutex_unlock(&mut);
59+
func(arg);
60+
__libcpp_mutex_lock(&mut);
61+
__libcpp_atomic_store(&flag, once_flag::_Complete, _AO_Release);
62+
__libcpp_mutex_unlock(&mut);
63+
__libcpp_condvar_broadcast(&cv);
64+
guard.__complete();
65+
} else {
66+
__libcpp_mutex_unlock(&mut);
67+
}
68+
69+
#endif // !_LIBCPP_HAS_NO_THREADS
70+
}
71+
72+
_LIBCPP_END_NAMESPACE_STD

libcxx/src/condition_variable.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <__config>
10-
11-
#ifndef _LIBCPP_HAS_NO_THREADS
12-
139
#include <condition_variable>
1410
#include <thread>
1511

@@ -92,5 +88,3 @@ notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk)
9288
_LIBCPP_END_NAMESPACE_STD
9389

9490
_LIBCPP_POP_MACROS
95-
96-
#endif // !_LIBCPP_HAS_NO_THREADS

libcxx/src/condition_variable_destructor.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
#include <__config>
1515
#include <__threading_support>
1616

17-
#if !defined(_LIBCPP_HAS_NO_THREADS)
18-
# if _LIBCPP_ABI_VERSION == 1 || !defined(_LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION)
19-
# define NEEDS_CONDVAR_DESTRUCTOR
20-
# endif
17+
#if _LIBCPP_ABI_VERSION == 1 || !defined(_LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION)
18+
# define NEEDS_CONDVAR_DESTRUCTOR
2119
#endif
2220

2321
_LIBCPP_BEGIN_NAMESPACE_STD

libcxx/src/future.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <__config>
10-
11-
#ifndef _LIBCPP_HAS_NO_THREADS
12-
139
#include <future>
1410
#include <string>
1511

@@ -268,5 +264,3 @@ shared_future<void>::operator=(const shared_future& __rhs)
268264
}
269265

270266
_LIBCPP_END_NAMESPACE_STD
271-
272-
#endif // !_LIBCPP_HAS_NO_THREADS

libcxx/src/mutex.cpp

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,15 @@
1414

1515
#include "include/atomic_support.h"
1616

17-
#ifndef _LIBCPP_HAS_NO_THREADS
18-
# if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
19-
# pragma comment(lib, "pthread")
20-
# endif
17+
#if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
18+
# pragma comment(lib, "pthread")
2119
#endif
2220

2321
_LIBCPP_PUSH_MACROS
2422
#include <__undef_macros>
2523

2624
_LIBCPP_BEGIN_NAMESPACE_STD
2725

28-
#ifndef _LIBCPP_HAS_NO_THREADS
29-
3026
// ~mutex is defined elsewhere
3127

3228
void
@@ -189,60 +185,6 @@ recursive_timed_mutex::unlock() noexcept
189185
}
190186
}
191187

192-
#endif // !_LIBCPP_HAS_NO_THREADS
193-
194-
// If dispatch_once_f ever handles C++ exceptions, and if one can get to it
195-
// without illegal macros (unexpected macros not beginning with _UpperCase or
196-
// __lowercase), and if it stops spinning waiting threads, then call_once should
197-
// call into dispatch_once_f instead of here. Relevant radar this code needs to
198-
// keep in sync with: 7741191.
199-
200-
#ifndef _LIBCPP_HAS_NO_THREADS
201-
static constinit __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
202-
static constinit __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
203-
#endif
204-
205-
void __call_once(volatile once_flag::_State_type& flag, void* arg,
206-
void (*func)(void*))
207-
{
208-
#if defined(_LIBCPP_HAS_NO_THREADS)
209-
210-
if (flag == once_flag::_Unset) {
211-
auto guard = std::__make_exception_guard([&flag] { flag = once_flag::_Unset; });
212-
flag = once_flag::_Pending;
213-
func(arg);
214-
flag = once_flag::_Complete;
215-
guard.__complete();
216-
}
217-
218-
#else // !_LIBCPP_HAS_NO_THREADS
219-
220-
__libcpp_mutex_lock(&mut);
221-
while (flag == once_flag::_Pending)
222-
__libcpp_condvar_wait(&cv, &mut);
223-
if (flag == once_flag::_Unset) {
224-
auto guard = std::__make_exception_guard([&flag] {
225-
__libcpp_mutex_lock(&mut);
226-
__libcpp_relaxed_store(&flag, once_flag::_Unset);
227-
__libcpp_mutex_unlock(&mut);
228-
__libcpp_condvar_broadcast(&cv);
229-
});
230-
231-
__libcpp_relaxed_store(&flag, once_flag::_Pending);
232-
__libcpp_mutex_unlock(&mut);
233-
func(arg);
234-
__libcpp_mutex_lock(&mut);
235-
__libcpp_atomic_store(&flag, once_flag::_Complete, _AO_Release);
236-
__libcpp_mutex_unlock(&mut);
237-
__libcpp_condvar_broadcast(&cv);
238-
guard.__complete();
239-
} else {
240-
__libcpp_mutex_unlock(&mut);
241-
}
242-
243-
#endif // !_LIBCPP_HAS_NO_THREADS
244-
}
245-
246188
_LIBCPP_END_NAMESPACE_STD
247189

248190
_LIBCPP_POP_MACROS

libcxx/src/mutex_destructor.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
#include <__config>
2020
#include <__threading_support>
2121

22-
#if !defined(_LIBCPP_HAS_NO_THREADS)
23-
# if _LIBCPP_ABI_VERSION == 1 || !defined(_LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION)
24-
# define NEEDS_MUTEX_DESTRUCTOR
25-
# endif
22+
#if _LIBCPP_ABI_VERSION == 1 || !defined(_LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION)
23+
# define NEEDS_MUTEX_DESTRUCTOR
2624
#endif
2725

2826
_LIBCPP_BEGIN_NAMESPACE_STD
@@ -45,6 +43,6 @@ mutex::~mutex() noexcept
4543
{
4644
__libcpp_mutex_destroy(&__m_);
4745
}
46+
#endif // !NEEDS_MUTEX_DESTRUCTOR
4847

49-
#endif // !_LIBCPP_HAS_NO_THREADS
5048
_LIBCPP_END_NAMESPACE_STD

libcxx/src/shared_mutex.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <__config>
10-
11-
#ifndef _LIBCPP_HAS_NO_THREADS
12-
13-
# include <mutex>
14-
# include <shared_mutex>
15-
# if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
16-
# pragma comment(lib, "pthread")
17-
# endif
9+
#include <mutex>
10+
#include <shared_mutex>
11+
#if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
12+
# pragma comment(lib, "pthread")
13+
#endif
1814

1915
_LIBCPP_BEGIN_NAMESPACE_STD
2016

@@ -95,5 +91,3 @@ bool shared_timed_mutex::try_lock_shared() { return __base_.try_lock_shared(); }
9591
void shared_timed_mutex::unlock_shared() { return __base_.unlock_shared(); }
9692

9793
_LIBCPP_END_NAMESPACE_STD
98-
99-
#endif // !_LIBCPP_HAS_NO_THREADS

libcxx/src/thread.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <__config>
10-
11-
#ifndef _LIBCPP_HAS_NO_THREADS
12-
139
#include <__thread/poll_with_backoff.h>
1410
#include <__thread/timed_backoff_policy.h>
1511
#include <exception>
@@ -215,5 +211,3 @@ __thread_struct::__make_ready_at_thread_exit(__assoc_sub_state* __s)
215211
}
216212

217213
_LIBCPP_END_NAMESPACE_STD
218-
219-
#endif // !_LIBCPP_HAS_NO_THREADS

libcxx/utils/data/ignore_format.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ libcxx/src/any.cpp
492492
libcxx/src/atomic.cpp
493493
libcxx/src/barrier.cpp
494494
libcxx/src/bind.cpp
495+
libcxx/src/call_once.cpp
495496
libcxx/src/charconv.cpp
496497
libcxx/src/chrono.cpp
497498
libcxx/src/condition_variable.cpp

0 commit comments

Comments
 (0)