Skip to content

[libc++] Handle threads-related .cpp files like we do all other source files #71100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions libcxx/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@ set(LIBCXX_LIB_CMAKEFILES_DIR "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTOR
set(LIBCXX_SOURCES
algorithm.cpp
any.cpp
atomic.cpp
barrier.cpp
bind.cpp
call_once.cpp
charconv.cpp
chrono.cpp
condition_variable.cpp
condition_variable_destructor.cpp
error_category.cpp
exception.cpp
filesystem/filesystem_clock.cpp
filesystem/filesystem_error.cpp
filesystem/path_parser.h
filesystem/path.cpp
functional.cpp
future.cpp
hash.cpp
include/apple_availability.h
include/atomic_support.h
Expand All @@ -37,8 +33,6 @@ set(LIBCXX_SOURCES
legacy_pointer_safety.cpp
memory.cpp
memory_resource.cpp
mutex.cpp
mutex_destructor.cpp
new_handler.cpp
new_helpers.cpp
optional.cpp
Expand All @@ -47,7 +41,6 @@ set(LIBCXX_SOURCES
ryu/d2fixed.cpp
ryu/d2s.cpp
ryu/f2s.cpp
shared_mutex.cpp
stdexcept.cpp
string.cpp
support/runtime/exception_fallback.ipp
Expand All @@ -62,14 +55,27 @@ set(LIBCXX_SOURCES
support/runtime/stdexcept_default.ipp
support/runtime/stdexcept_vcruntime.ipp
system_error.cpp
thread.cpp
typeinfo.cpp
valarray.cpp
variant.cpp
vector.cpp
verbose_abort.cpp
)

if (LIBCXX_ENABLE_THREADS)
list(APPEND LIBCXX_SOURCES
atomic.cpp
barrier.cpp
condition_variable_destructor.cpp
condition_variable.cpp
future.cpp
mutex_destructor.cpp
mutex.cpp
shared_mutex.cpp
thread.cpp
)
endif()

if (LIBCXX_ENABLE_RANDOM_DEVICE)
list(APPEND LIBCXX_SOURCES
random.cpp
Expand Down
5 changes: 0 additions & 5 deletions libcxx/src/atomic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
//
//===----------------------------------------------------------------------===//

#include <__config>
#ifndef _LIBCPP_HAS_NO_THREADS

#include <__thread/timed_backoff_policy.h>
#include <atomic>
#include <climits>
Expand Down Expand Up @@ -219,5 +216,3 @@ void __libcpp_atomic_wait(__cxx_atomic_contention_t const volatile* __location,
}

_LIBCPP_END_NAMESPACE_STD

#endif //_LIBCPP_HAS_NO_THREADS
6 changes: 0 additions & 6 deletions libcxx/src/barrier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
//
//===----------------------------------------------------------------------===//

#include <__config>

#ifndef _LIBCPP_HAS_NO_THREADS

#include <barrier>
#include <thread>

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

_LIBCPP_END_NAMESPACE_STD

#endif //_LIBCPP_HAS_NO_THREADS
72 changes: 72 additions & 0 deletions libcxx/src/call_once.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include <__mutex/once_flag.h>
#include <__utility/exception_guard.h>

#ifndef _LIBCPP_HAS_NO_THREADS
# include <__threading_support>
#endif

#include "include/atomic_support.h"

_LIBCPP_BEGIN_NAMESPACE_STD

// If dispatch_once_f ever handles C++ exceptions, and if one can get to it
// without illegal macros (unexpected macros not beginning with _UpperCase or
// __lowercase), and if it stops spinning waiting threads, then call_once should
// call into dispatch_once_f instead of here. Relevant radar this code needs to
// keep in sync with: 7741191.

#ifndef _LIBCPP_HAS_NO_THREADS
static constinit __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
static constinit __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
#endif

void __call_once(volatile once_flag::_State_type& flag, void* arg,
void (*func)(void*))
{
#if defined(_LIBCPP_HAS_NO_THREADS)

if (flag == once_flag::_Unset) {
auto guard = std::__make_exception_guard([&flag] { flag = once_flag::_Unset; });
flag = once_flag::_Pending;
func(arg);
flag = once_flag::_Complete;
guard.__complete();
}

#else // !_LIBCPP_HAS_NO_THREADS

__libcpp_mutex_lock(&mut);
while (flag == once_flag::_Pending)
__libcpp_condvar_wait(&cv, &mut);
if (flag == once_flag::_Unset) {
auto guard = std::__make_exception_guard([&flag] {
__libcpp_mutex_lock(&mut);
__libcpp_relaxed_store(&flag, once_flag::_Unset);
__libcpp_mutex_unlock(&mut);
__libcpp_condvar_broadcast(&cv);
});

__libcpp_relaxed_store(&flag, once_flag::_Pending);
__libcpp_mutex_unlock(&mut);
func(arg);
__libcpp_mutex_lock(&mut);
__libcpp_atomic_store(&flag, once_flag::_Complete, _AO_Release);
__libcpp_mutex_unlock(&mut);
__libcpp_condvar_broadcast(&cv);
guard.__complete();
} else {
__libcpp_mutex_unlock(&mut);
}

#endif // !_LIBCPP_HAS_NO_THREADS
}

_LIBCPP_END_NAMESPACE_STD
6 changes: 0 additions & 6 deletions libcxx/src/condition_variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
//
//===----------------------------------------------------------------------===//

#include <__config>

#ifndef _LIBCPP_HAS_NO_THREADS

#include <condition_variable>
#include <thread>

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

_LIBCPP_POP_MACROS

#endif // !_LIBCPP_HAS_NO_THREADS
6 changes: 2 additions & 4 deletions libcxx/src/condition_variable_destructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
#include <__config>
#include <__threading_support>

#if !defined(_LIBCPP_HAS_NO_THREADS)
# if _LIBCPP_ABI_VERSION == 1 || !defined(_LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION)
# define NEEDS_CONDVAR_DESTRUCTOR
# endif
#if _LIBCPP_ABI_VERSION == 1 || !defined(_LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION)
# define NEEDS_CONDVAR_DESTRUCTOR
#endif

_LIBCPP_BEGIN_NAMESPACE_STD
Expand Down
6 changes: 0 additions & 6 deletions libcxx/src/future.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
//
//===----------------------------------------------------------------------===//

#include <__config>

#ifndef _LIBCPP_HAS_NO_THREADS

#include <future>
#include <string>

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

_LIBCPP_END_NAMESPACE_STD

#endif // !_LIBCPP_HAS_NO_THREADS
62 changes: 2 additions & 60 deletions libcxx/src/mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@

#include "include/atomic_support.h"

#ifndef _LIBCPP_HAS_NO_THREADS
# if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
# pragma comment(lib, "pthread")
# endif
#if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
# pragma comment(lib, "pthread")
#endif

_LIBCPP_PUSH_MACROS
#include <__undef_macros>

_LIBCPP_BEGIN_NAMESPACE_STD

#ifndef _LIBCPP_HAS_NO_THREADS

// ~mutex is defined elsewhere

void
Expand Down Expand Up @@ -189,60 +185,6 @@ recursive_timed_mutex::unlock() noexcept
}
}

#endif // !_LIBCPP_HAS_NO_THREADS

// If dispatch_once_f ever handles C++ exceptions, and if one can get to it
// without illegal macros (unexpected macros not beginning with _UpperCase or
// __lowercase), and if it stops spinning waiting threads, then call_once should
// call into dispatch_once_f instead of here. Relevant radar this code needs to
// keep in sync with: 7741191.

#ifndef _LIBCPP_HAS_NO_THREADS
static constinit __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
static constinit __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
#endif

void __call_once(volatile once_flag::_State_type& flag, void* arg,
void (*func)(void*))
{
#if defined(_LIBCPP_HAS_NO_THREADS)

if (flag == once_flag::_Unset) {
auto guard = std::__make_exception_guard([&flag] { flag = once_flag::_Unset; });
flag = once_flag::_Pending;
func(arg);
flag = once_flag::_Complete;
guard.__complete();
}

#else // !_LIBCPP_HAS_NO_THREADS

__libcpp_mutex_lock(&mut);
while (flag == once_flag::_Pending)
__libcpp_condvar_wait(&cv, &mut);
if (flag == once_flag::_Unset) {
auto guard = std::__make_exception_guard([&flag] {
__libcpp_mutex_lock(&mut);
__libcpp_relaxed_store(&flag, once_flag::_Unset);
__libcpp_mutex_unlock(&mut);
__libcpp_condvar_broadcast(&cv);
});

__libcpp_relaxed_store(&flag, once_flag::_Pending);
__libcpp_mutex_unlock(&mut);
func(arg);
__libcpp_mutex_lock(&mut);
__libcpp_atomic_store(&flag, once_flag::_Complete, _AO_Release);
__libcpp_mutex_unlock(&mut);
__libcpp_condvar_broadcast(&cv);
guard.__complete();
} else {
__libcpp_mutex_unlock(&mut);
}

#endif // !_LIBCPP_HAS_NO_THREADS
}

_LIBCPP_END_NAMESPACE_STD

_LIBCPP_POP_MACROS
8 changes: 3 additions & 5 deletions libcxx/src/mutex_destructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
#include <__config>
#include <__threading_support>

#if !defined(_LIBCPP_HAS_NO_THREADS)
# if _LIBCPP_ABI_VERSION == 1 || !defined(_LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION)
# define NEEDS_MUTEX_DESTRUCTOR
# endif
#if _LIBCPP_ABI_VERSION == 1 || !defined(_LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION)
# define NEEDS_MUTEX_DESTRUCTOR
#endif

_LIBCPP_BEGIN_NAMESPACE_STD
Expand All @@ -45,6 +43,6 @@ mutex::~mutex() noexcept
{
__libcpp_mutex_destroy(&__m_);
}
#endif // !NEEDS_MUTEX_DESTRUCTOR

#endif // !_LIBCPP_HAS_NO_THREADS
_LIBCPP_END_NAMESPACE_STD
16 changes: 5 additions & 11 deletions libcxx/src/shared_mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@
//
//===----------------------------------------------------------------------===//

#include <__config>

#ifndef _LIBCPP_HAS_NO_THREADS

# include <mutex>
# include <shared_mutex>
# if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
# pragma comment(lib, "pthread")
# endif
#include <mutex>
#include <shared_mutex>
#if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
# pragma comment(lib, "pthread")
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

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

_LIBCPP_END_NAMESPACE_STD

#endif // !_LIBCPP_HAS_NO_THREADS
6 changes: 0 additions & 6 deletions libcxx/src/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
//
//===----------------------------------------------------------------------===//

#include <__config>

#ifndef _LIBCPP_HAS_NO_THREADS

#include <__thread/poll_with_backoff.h>
#include <__thread/timed_backoff_policy.h>
#include <exception>
Expand Down Expand Up @@ -215,5 +211,3 @@ __thread_struct::__make_ready_at_thread_exit(__assoc_sub_state* __s)
}

_LIBCPP_END_NAMESPACE_STD

#endif // !_LIBCPP_HAS_NO_THREADS
1 change: 1 addition & 0 deletions libcxx/utils/data/ignore_format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ libcxx/src/any.cpp
libcxx/src/atomic.cpp
libcxx/src/barrier.cpp
libcxx/src/bind.cpp
libcxx/src/call_once.cpp
libcxx/src/charconv.cpp
libcxx/src/chrono.cpp
libcxx/src/condition_variable.cpp
Expand Down