File indexing completed on 2026-05-03 08:13:43
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___CXX03___THREAD_SUPPORT_C11_H
0011 #define _LIBCPP___CXX03___THREAD_SUPPORT_C11_H
0012
0013 #include <__cxx03/__chrono/convert_to_timespec.h>
0014 #include <__cxx03/__chrono/duration.h>
0015 #include <__cxx03/__config>
0016 #include <__cxx03/ctime>
0017 #include <__cxx03/errno.h>
0018 #include <__cxx03/threads.h>
0019
0020 #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
0021 # pragma GCC system_header
0022 #endif
0023
0024 _LIBCPP_BEGIN_NAMESPACE_STD
0025
0026 using __libcpp_timespec_t = ::timespec;
0027
0028
0029
0030
0031 typedef mtx_t __libcpp_mutex_t;
0032
0033 #define _LIBCPP_MUTEX_INITIALIZER \
0034 {}
0035
0036 typedef mtx_t __libcpp_recursive_mutex_t;
0037
0038 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t* __m) {
0039 return mtx_init(__m, mtx_plain | mtx_recursive) == thrd_success ? 0 : EINVAL;
0040 }
0041
0042 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int
0043 __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t* __m) {
0044 return mtx_lock(__m) == thrd_success ? 0 : EINVAL;
0045 }
0046
0047 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS bool
0048 __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t* __m) {
0049 return mtx_trylock(__m) == thrd_success;
0050 }
0051
0052 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int
0053 __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t* __m) {
0054 return mtx_unlock(__m) == thrd_success ? 0 : EINVAL;
0055 }
0056
0057 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t* __m) {
0058 mtx_destroy(__m);
0059 return 0;
0060 }
0061
0062 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_mutex_lock(__libcpp_mutex_t* __m) {
0063 return mtx_lock(__m) == thrd_success ? 0 : EINVAL;
0064 }
0065
0066 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS bool __libcpp_mutex_trylock(__libcpp_mutex_t* __m) {
0067 return mtx_trylock(__m) == thrd_success;
0068 }
0069
0070 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_mutex_unlock(__libcpp_mutex_t* __m) {
0071 return mtx_unlock(__m) == thrd_success ? 0 : EINVAL;
0072 }
0073
0074 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_mutex_destroy(__libcpp_mutex_t* __m) {
0075 mtx_destroy(__m);
0076 return 0;
0077 }
0078
0079
0080
0081
0082 typedef cnd_t __libcpp_condvar_t;
0083
0084 #define _LIBCPP_CONDVAR_INITIALIZER \
0085 {}
0086
0087 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_condvar_signal(__libcpp_condvar_t* __cv) {
0088 return cnd_signal(__cv) == thrd_success ? 0 : EINVAL;
0089 }
0090
0091 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv) {
0092 return cnd_broadcast(__cv) == thrd_success ? 0 : EINVAL;
0093 }
0094
0095 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int
0096 __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m) {
0097 return cnd_wait(__cv, __m) == thrd_success ? 0 : EINVAL;
0098 }
0099
0100 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int
0101 __libcpp_condvar_timedwait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m, timespec* __ts) {
0102 int __ec = cnd_timedwait(__cv, __m, __ts);
0103 return __ec == thrd_timedout ? ETIMEDOUT : __ec;
0104 }
0105
0106 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv) {
0107 cnd_destroy(__cv);
0108 return 0;
0109 }
0110
0111
0112
0113
0114 typedef ::once_flag __libcpp_exec_once_flag;
0115 #define _LIBCPP_EXEC_ONCE_INITIALIZER ONCE_FLAG_INIT
0116
0117 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_execute_once(__libcpp_exec_once_flag* flag, void (*init_routine)(void)) {
0118 ::call_once(flag, init_routine);
0119 return 0;
0120 }
0121
0122
0123
0124
0125 typedef thrd_t __libcpp_thread_id;
0126
0127
0128 inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2) {
0129 return thrd_equal(t1, t2) != 0;
0130 }
0131
0132
0133 inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2) {
0134 return t1 < t2;
0135 }
0136
0137
0138
0139
0140 #define _LIBCPP_NULL_THREAD 0U
0141
0142 typedef thrd_t __libcpp_thread_t;
0143
0144 inline _LIBCPP_HIDE_FROM_ABI __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t* __t) { return *__t; }
0145
0146 inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_isnull(const __libcpp_thread_t* __t) {
0147 return __libcpp_thread_get_id(__t) == 0;
0148 }
0149
0150 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_thread_create(__libcpp_thread_t* __t, void* (*__func)(void*), void* __arg) {
0151 int __ec = thrd_create(__t, reinterpret_cast<thrd_start_t>(__func), __arg);
0152 return __ec == thrd_nomem ? ENOMEM : __ec;
0153 }
0154
0155 inline _LIBCPP_HIDE_FROM_ABI __libcpp_thread_id __libcpp_thread_get_current_id() { return thrd_current(); }
0156
0157 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_thread_join(__libcpp_thread_t* __t) {
0158 return thrd_join(*__t, nullptr) == thrd_success ? 0 : EINVAL;
0159 }
0160
0161 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_thread_detach(__libcpp_thread_t* __t) {
0162 return thrd_detach(*__t) == thrd_success ? 0 : EINVAL;
0163 }
0164
0165 inline _LIBCPP_HIDE_FROM_ABI void __libcpp_thread_yield() { thrd_yield(); }
0166
0167 inline _LIBCPP_HIDE_FROM_ABI void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns) {
0168 __libcpp_timespec_t __ts = std::__convert_to_timespec<__libcpp_timespec_t>(__ns);
0169 thrd_sleep(&__ts, nullptr);
0170 }
0171
0172
0173
0174
0175 #define _LIBCPP_TLS_DESTRUCTOR_CC
0176
0177 typedef tss_t __libcpp_tls_key;
0178
0179 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_tls_create(__libcpp_tls_key* __key, void (*__at_exit)(void*)) {
0180 return tss_create(__key, __at_exit) == thrd_success ? 0 : EINVAL;
0181 }
0182
0183 inline _LIBCPP_HIDE_FROM_ABI void* __libcpp_tls_get(__libcpp_tls_key __key) { return tss_get(__key); }
0184
0185 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_tls_set(__libcpp_tls_key __key, void* __p) {
0186 return tss_set(__key, __p) == thrd_success ? 0 : EINVAL;
0187 }
0188
0189 _LIBCPP_END_NAMESPACE_STD
0190
0191 #endif