Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:43

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03___THREAD_SUPPORT_PTHREAD_H
0011 #define _LIBCPP___CXX03___THREAD_SUPPORT_PTHREAD_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 <pthread.h>
0019 #include <sched.h>
0020 
0021 #ifdef __MVS__
0022 #  include <__cxx03/__support/ibm/nanosleep.h>
0023 #endif
0024 
0025 // Some platforms require <bits/atomic_wide_counter.h> in order for
0026 // PTHREAD_COND_INITIALIZER to be expanded. Normally that would come
0027 // in via <pthread.h>, but it's a non-modular header on those platforms,
0028 // so libc++'s <math.h> usually absorbs atomic_wide_counter.h into the
0029 // module with <math.h> and makes atomic_wide_counter.h invisible.
0030 // Include <math.h> here to work around that.
0031 // This checks wheter a Clang module is built
0032 #if __building_module(std)
0033 #  include <__cxx03/math.h>
0034 #endif
0035 
0036 #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
0037 #  pragma GCC system_header
0038 #endif
0039 
0040 _LIBCPP_BEGIN_NAMESPACE_STD
0041 
0042 using __libcpp_timespec_t = ::timespec;
0043 
0044 //
0045 // Mutex
0046 //
0047 typedef pthread_mutex_t __libcpp_mutex_t;
0048 #define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
0049 
0050 typedef pthread_mutex_t __libcpp_recursive_mutex_t;
0051 
0052 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t* __m) {
0053   pthread_mutexattr_t __attr;
0054   int __ec = pthread_mutexattr_init(&__attr);
0055   if (__ec)
0056     return __ec;
0057   __ec = pthread_mutexattr_settype(&__attr, PTHREAD_MUTEX_RECURSIVE);
0058   if (__ec) {
0059     pthread_mutexattr_destroy(&__attr);
0060     return __ec;
0061   }
0062   __ec = pthread_mutex_init(__m, &__attr);
0063   if (__ec) {
0064     pthread_mutexattr_destroy(&__attr);
0065     return __ec;
0066   }
0067   __ec = pthread_mutexattr_destroy(&__attr);
0068   if (__ec) {
0069     pthread_mutex_destroy(__m);
0070     return __ec;
0071   }
0072   return 0;
0073 }
0074 
0075 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int
0076 __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t* __m) {
0077   return pthread_mutex_lock(__m);
0078 }
0079 
0080 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS bool
0081 __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t* __m) {
0082   return pthread_mutex_trylock(__m) == 0;
0083 }
0084 
0085 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int
0086 __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t* __m) {
0087   return pthread_mutex_unlock(__m);
0088 }
0089 
0090 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t* __m) {
0091   return pthread_mutex_destroy(__m);
0092 }
0093 
0094 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_mutex_lock(__libcpp_mutex_t* __m) {
0095   return pthread_mutex_lock(__m);
0096 }
0097 
0098 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS bool __libcpp_mutex_trylock(__libcpp_mutex_t* __m) {
0099   return pthread_mutex_trylock(__m) == 0;
0100 }
0101 
0102 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_mutex_unlock(__libcpp_mutex_t* __m) {
0103   return pthread_mutex_unlock(__m);
0104 }
0105 
0106 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_mutex_destroy(__libcpp_mutex_t* __m) { return pthread_mutex_destroy(__m); }
0107 
0108 //
0109 // Condition Variable
0110 //
0111 typedef pthread_cond_t __libcpp_condvar_t;
0112 #define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER
0113 
0114 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_condvar_signal(__libcpp_condvar_t* __cv) { return pthread_cond_signal(__cv); }
0115 
0116 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv) {
0117   return pthread_cond_broadcast(__cv);
0118 }
0119 
0120 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int
0121 __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m) {
0122   return pthread_cond_wait(__cv, __m);
0123 }
0124 
0125 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int
0126 __libcpp_condvar_timedwait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m, __libcpp_timespec_t* __ts) {
0127   return pthread_cond_timedwait(__cv, __m, __ts);
0128 }
0129 
0130 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv) {
0131   return pthread_cond_destroy(__cv);
0132 }
0133 
0134 //
0135 // Execute once
0136 //
0137 typedef pthread_once_t __libcpp_exec_once_flag;
0138 #define _LIBCPP_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT
0139 
0140 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_execute_once(__libcpp_exec_once_flag* __flag, void (*__init_routine)()) {
0141   return pthread_once(__flag, __init_routine);
0142 }
0143 
0144 //
0145 // Thread id
0146 //
0147 #if defined(__MVS__)
0148 typedef unsigned long long __libcpp_thread_id;
0149 #else
0150 typedef pthread_t __libcpp_thread_id;
0151 #endif
0152 
0153 // Returns non-zero if the thread ids are equal, otherwise 0
0154 inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_id_equal(__libcpp_thread_id __t1, __libcpp_thread_id __t2) {
0155   return __t1 == __t2;
0156 }
0157 
0158 // Returns non-zero if t1 < t2, otherwise 0
0159 inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_id_less(__libcpp_thread_id __t1, __libcpp_thread_id __t2) {
0160   return __t1 < __t2;
0161 }
0162 
0163 //
0164 // Thread
0165 //
0166 #define _LIBCPP_NULL_THREAD ((__libcpp_thread_t()))
0167 typedef pthread_t __libcpp_thread_t;
0168 
0169 inline _LIBCPP_HIDE_FROM_ABI __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t* __t) {
0170 #if defined(__MVS__)
0171   return __t->__;
0172 #else
0173   return *__t;
0174 #endif
0175 }
0176 
0177 inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_isnull(const __libcpp_thread_t* __t) {
0178   return __libcpp_thread_get_id(__t) == 0;
0179 }
0180 
0181 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_thread_create(__libcpp_thread_t* __t, void* (*__func)(void*), void* __arg) {
0182   return pthread_create(__t, nullptr, __func, __arg);
0183 }
0184 
0185 inline _LIBCPP_HIDE_FROM_ABI __libcpp_thread_id __libcpp_thread_get_current_id() {
0186   const __libcpp_thread_t __current_thread = pthread_self();
0187   return __libcpp_thread_get_id(&__current_thread);
0188 }
0189 
0190 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_thread_join(__libcpp_thread_t* __t) { return pthread_join(*__t, nullptr); }
0191 
0192 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_thread_detach(__libcpp_thread_t* __t) { return pthread_detach(*__t); }
0193 
0194 inline _LIBCPP_HIDE_FROM_ABI void __libcpp_thread_yield() { sched_yield(); }
0195 
0196 inline _LIBCPP_HIDE_FROM_ABI void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns) {
0197   __libcpp_timespec_t __ts = std::__convert_to_timespec<__libcpp_timespec_t>(__ns);
0198   while (nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
0199     ;
0200 }
0201 
0202 //
0203 // Thread local storage
0204 //
0205 #define _LIBCPP_TLS_DESTRUCTOR_CC /* nothing */
0206 
0207 typedef pthread_key_t __libcpp_tls_key;
0208 
0209 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_tls_create(__libcpp_tls_key* __key, void (*__at_exit)(void*)) {
0210   return pthread_key_create(__key, __at_exit);
0211 }
0212 
0213 inline _LIBCPP_HIDE_FROM_ABI void* __libcpp_tls_get(__libcpp_tls_key __key) { return pthread_getspecific(__key); }
0214 
0215 inline _LIBCPP_HIDE_FROM_ABI int __libcpp_tls_set(__libcpp_tls_key __key, void* __p) {
0216   return pthread_setspecific(__key, __p);
0217 }
0218 
0219 _LIBCPP_END_NAMESPACE_STD
0220 
0221 #endif // _LIBCPP___CXX03___THREAD_SUPPORT_PTHREAD_H