Skip to content

[libc++] Split the monolithic __threading_support header #79654

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 4 commits into from
Jan 30, 2024
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
14 changes: 9 additions & 5 deletions libcxx/docs/DesignDocs/ThreadingSupportAPI.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ These different models provide entirely different interfaces from each
other. To address this libc++ wraps the underlying threading API in a new and
consistent API, which it uses internally to implement threading primitives.

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

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

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

External Threading Library
==========================
Expand Down Expand Up @@ -58,6 +58,10 @@ Threading Configuration Macros
This macro is defined when libc++ should use POSIX threads to implement the
internal threading API.

**_LIBCPP_HAS_THREAD_API_C11**
This macro is defined when libc++ should use C11 threads to implement the
internal threading API.

**_LIBCPP_HAS_THREAD_API_WIN32**
This macro is defined when libc++ should use Win32 threads to implement the
internal threading API.
6 changes: 5 additions & 1 deletion libcxx/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -688,10 +688,14 @@ set(files
__thread/id.h
__thread/jthread.h
__thread/poll_with_backoff.h
__thread/support.h
__thread/support/c11.h
__thread/support/external.h
__thread/support/pthread.h
__thread/support/windows.h
__thread/this_thread.h
__thread/thread.h
__thread/timed_backoff_policy.h
__threading_support
__tree
__tuple/make_tuple_types.h
__tuple/pair_like.h
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__atomic/atomic_flag.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <__atomic/memory_order.h>
#include <__chrono/duration.h>
#include <__config>
#include <__threading_support>
#include <__thread/support.h>
#include <cstdint>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__atomic/atomic_sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <__config>
#include <__memory/addressof.h>
#include <__thread/poll_with_backoff.h>
#include <__threading_support>
#include <__thread/support.h>
#include <__type_traits/decay.h>
#include <cstring>

Expand Down
3 changes: 2 additions & 1 deletion libcxx/include/__condition_variable/condition_variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
#ifndef _LIBCPP___CONDITION_VARIABLE_CONDITION_VARIABLE_H
#define _LIBCPP___CONDITION_VARIABLE_CONDITION_VARIABLE_H

#include <__chrono/duration.h>
#include <__chrono/steady_clock.h>
#include <__chrono/system_clock.h>
#include <__chrono/time_point.h>
#include <__config>
#include <__mutex/mutex.h>
#include <__mutex/unique_lock.h>
#include <__system_error/system_error.h>
#include <__threading_support>
#include <__thread/support.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_floating_point.h>
#include <__utility/move.h>
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__mutex/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#define _LIBCPP___MUTEX_MUTEX_H

#include <__config>
#include <__threading_support>
#include <__thread/support.h>
#include <__type_traits/is_nothrow_default_constructible.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__thread/formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct _LIBCPP_TEMPLATE_VIS formatter<__thread_id, _CharT> {

template <class _FormatContext>
_LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(__thread_id __id, _FormatContext& __ctx) const {
// In __threading_support __libcpp_thread_id is either a
// In __thread/support/pthread.h, __libcpp_thread_id is either a
// unsigned long long or a pthread_t.
//
// The type of pthread_t is left unspecified in POSIX so it can be any
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__thread/id.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <__config>
#include <__fwd/hash.h>
#include <__fwd/ostream.h>
#include <__threading_support>
#include <__thread/support.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__thread/jthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#include <__functional/invoke.h>
#include <__stop_token/stop_source.h>
#include <__stop_token/stop_token.h>
#include <__thread/support.h>
#include <__thread/thread.h>
#include <__threading_support>
#include <__type_traits/decay.h>
#include <__type_traits/is_constructible.h>
#include <__type_traits/is_same.h>
Expand Down
119 changes: 119 additions & 0 deletions libcxx/include/__thread/support.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// -*- 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_SUPPORT_H
#define _LIBCPP___THREAD_SUPPORT_H

#include <__config>

#ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
# pragma GCC system_header
#endif

/*

//
// The library supports multiple implementations of the basic threading functionality.
// The following functionality must be provided by any implementation:
//

using __libcpp_timespec_t = ...;

//
// Mutex
//
using __libcpp_mutex_t = ...;
#define _LIBCPP_MUTEX_INITIALIZER ...

using __libcpp_recursive_mutex_t = ...;

int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t*);
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t*);
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t*);
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t*);
int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t*);

_LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_mutex_lock(__libcpp_mutex_t*);
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS bool __libcpp_mutex_trylock(__libcpp_mutex_t*);
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_mutex_unlock(__libcpp_mutex_t*);
int __libcpp_mutex_destroy(__libcpp_mutex_t*);

//
// Condition Variable
//
using __libcpp_condvar_t = ...;
#define _LIBCPP_CONDVAR_INITIALIZER ...

int __libcpp_condvar_signal(__libcpp_condvar_t*);
int __libcpp_condvar_broadcast(__libcpp_condvar_t*);
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_condvar_wait(__libcpp_condvar_t*, __libcpp_mutex_t*);
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS
int __libcpp_condvar_timedwait(__libcpp_condvar_t*, __libcpp_mutex_t*, __libcpp_timespec_t*);
int __libcpp_condvar_destroy(__libcpp_condvar_t*);

//
// Execute once
//
using __libcpp_exec_once_flag = ...;
#define _LIBCPP_EXEC_ONCE_INITIALIZER ...

int __libcpp_execute_once(__libcpp_exec_once_flag*, void (*__init_routine)());

//
// Thread id
//
using __libcpp_thread_id = ...;

bool __libcpp_thread_id_equal(__libcpp_thread_id, __libcpp_thread_id);
bool __libcpp_thread_id_less(__libcpp_thread_id, __libcpp_thread_id);

//
// Thread
//
#define _LIBCPP_NULL_THREAD ...
using __libcpp_thread_t = ...;

bool __libcpp_thread_isnull(const __libcpp_thread_t*);
int __libcpp_thread_create(__libcpp_thread_t*, void* (*__func)(void*), void* __arg);
__libcpp_thread_id __libcpp_thread_get_current_id();
__libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t*);
int __libcpp_thread_join(__libcpp_thread_t*);
int __libcpp_thread_detach(__libcpp_thread_t*);
void __libcpp_thread_yield();
void __libcpp_thread_sleep_for(const chrono::nanoseconds&);

//
// Thread local storage
//
#define _LIBCPP_TLS_DESTRUCTOR_CC ...
using __libcpp_tls_key = ...;

int __libcpp_tls_create(__libcpp_tls_key*, void (*__at_exit)(void*));
void* __libcpp_tls_get(__libcpp_tls_key);
int __libcpp_tls_set(__libcpp_tls_key, void*);

*/

#if !defined(_LIBCPP_HAS_NO_THREADS)

# if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
# include <__thread/support/external.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just include the <__external_threading> header directly? There doesn't seem to be much reason not to.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am tempted to leave it as-is just cause the CI is passing and there are other changes I want to land on top. I can do a follow-up patch.

# elif defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
# include <__thread/support/pthread.h>
# elif defined(_LIBCPP_HAS_THREAD_API_C11)
# include <__thread/support/c11.h>
# elif defined(_LIBCPP_HAS_THREAD_API_WIN32)
# include <__thread/support/windows.h>
# else
# error "No threading API was selected"
# endif

#endif // !_LIBCPP_HAS_NO_THREADS

#endif // _LIBCPP___THREAD_SUPPORT_H
Loading