diff options
author | Jack Lloyd <[email protected]> | 2019-07-15 20:11:12 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2019-07-15 20:11:12 -0400 |
commit | 0b88165c9ac417b5aad8f63f9a3bfdb1ba99abaa (patch) | |
tree | 6bc0cf1ec61a215e41e3adea5c70fd38502d73f1 | |
parent | 9df40cce5756f8109ca5659c4c92459fd35d3380 (diff) |
Restructure OS::get_cpu_xxx functions a bit
In particular if sysconf fails, fall back to std::thread if
that is available instead of falling back to return 1 right away.
-rw-r--r-- | src/lib/utils/os_utils.cpp | 34 |
1 files changed, 15 insertions, 19 deletions
diff --git a/src/lib/utils/os_utils.cpp b/src/lib/utils/os_utils.cpp index 098edaaf4..ec5b4c108 100644 --- a/src/lib/utils/os_utils.cpp +++ b/src/lib/utils/os_utils.cpp @@ -206,32 +206,28 @@ uint64_t OS::get_cpu_cycle_counter() size_t OS::get_cpu_total() { - size_t tt = 1; #if defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(_SC_NPROCESSORS_CONF) - long res; - if ((res = ::sysconf(_SC_NPROCESSORS_CONF)) <= 0) - { - return 1; - } - tt = static_cast<size_t>(res); -#elif defined(BOTAN_TARGET_OS_HAS_THREADS) - tt = static_cast<size_t>(std::thread::hardware_concurrency()); -#endif - return tt; + const long res = ::sysconf(_SC_NPROCESSORS_CONF); + if(res > 0) + return static_cast<size_t>(res); +#endif + +#if defined(BOTAN_TARGET_OS_HAS_THREADS) + return static_cast<size_t>(std::thread::hardware_concurrency()); +#else + return 1; +#endif } size_t OS::get_cpu_available() { #if defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(_SC_NPROCESSORS_ONLN) - long res; - if ((res = ::sysconf(_SC_NPROCESSORS_ONLN)) <= 0) - { - return 1; - } - return static_cast<size_t>(res); -#else - return get_cpu_total(); + const long res = ::sysconf(_SC_NPROCESSORS_ONLN); + if(res > 0) + return static_cast<size_t>(res); #endif + + return OS::get_cpu_total(); } uint64_t OS::get_high_resolution_clock() |