comparison libcxx/src/thread.cpp @ 221:79ff65ed7e25

LLVM12 Original
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 15 Jun 2021 19:15:29 +0900
parents 0572611fdcc8
children c4bab56944e8
comparison
equal deleted inserted replaced
220:42394fc6a535 221:79ff65ed7e25
12 #include "thread" 12 #include "thread"
13 #include "exception" 13 #include "exception"
14 #include "vector" 14 #include "vector"
15 #include "future" 15 #include "future"
16 #include "limits" 16 #include "limits"
17 #include <sys/types.h>
18
19 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
20 # include <sys/param.h>
21 # if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
22 # include <sys/sysctl.h>
23 # endif
24 #endif // defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
25 17
26 #if __has_include(<unistd.h>) 18 #if __has_include(<unistd.h>)
27 #include <unistd.h> 19 # include <unistd.h> // for sysconf
28 #endif 20 #endif
29 21
30 #if defined(__NetBSD__) 22 #if defined(__NetBSD__)
31 #pragma weak pthread_create // Do not create libpthread dependency 23 #pragma weak pthread_create // Do not create libpthread dependency
32 #endif 24 #endif
76 if (ec) 68 if (ec)
77 __throw_system_error(ec, "thread::detach failed"); 69 __throw_system_error(ec, "thread::detach failed");
78 } 70 }
79 71
80 unsigned 72 unsigned
81 thread::hardware_concurrency() _NOEXCEPT 73 thread::hardware_concurrency() noexcept
82 { 74 {
83 #if defined(CTL_HW) && defined(HW_NCPU) 75 #if defined(_SC_NPROCESSORS_ONLN)
84 unsigned n;
85 int mib[2] = {CTL_HW, HW_NCPU};
86 std::size_t s = sizeof(n);
87 sysctl(mib, 2, &n, &s, 0, 0);
88 return n;
89 #elif defined(_SC_NPROCESSORS_ONLN)
90 long result = sysconf(_SC_NPROCESSORS_ONLN); 76 long result = sysconf(_SC_NPROCESSORS_ONLN);
91 // sysconf returns -1 if the name is invalid, the option does not exist or 77 // sysconf returns -1 if the name is invalid, the option does not exist or
92 // does not have a definite limit. 78 // does not have a definite limit.
93 // if sysconf returns some other negative number, we have no idea 79 // if sysconf returns some other negative number, we have no idea
94 // what is going on. Default to something safe. 80 // what is going on. Default to something safe.
106 _LIBCPP_WARNING("hardware_concurrency not yet implemented") 92 _LIBCPP_WARNING("hardware_concurrency not yet implemented")
107 # else 93 # else
108 # warning hardware_concurrency not yet implemented 94 # warning hardware_concurrency not yet implemented
109 # endif 95 # endif
110 return 0; // Means not computable [thread.thread.static] 96 return 0; // Means not computable [thread.thread.static]
111 #endif // defined(CTL_HW) && defined(HW_NCPU) 97 #endif // defined(CTL_HW) && defined(HW_NCPU)
112 } 98 }
113 99
114 namespace this_thread 100 namespace this_thread
115 { 101 {
116 102