From e5dc50b0dd7e029ef4972c4f11e630326e324634 Mon Sep 17 00:00:00 2001 From: q66 Date: Tue, 26 Jan 2016 18:55:35 +0000 Subject: [PATCH] support hardware_concurrency static method like c++ std::thread (also replaces cpu_count_get) --- ostd/platform.hh | 24 +----------------------- ostd/thread.hh | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/ostd/platform.hh b/ostd/platform.hh index 7c4714e..972d6a8 100644 --- a/ostd/platform.hh +++ b/ostd/platform.hh @@ -6,6 +6,7 @@ #ifndef OSTD_PLATFORM_HH #define OSTD_PLATFORM_HH +#include #include #if defined(WIN32) || defined(_WIN32) || (defined(__WIN32) && !defined(__CYGWIN__)) @@ -71,13 +72,6 @@ # endif #endif -#ifndef OSTD_PLATFORM_WIN32 -#include -#else -#define WIN32_LEAN_AND_MEAN -#include -#endif - #ifdef OSTD_PLATFORM_WIN32 # ifdef OSTD_LIBRARY_DLL # ifdef OSTD_TOOLCHAIN_GNU @@ -147,22 +141,6 @@ inline uint64_t endian_swap64(uint64_t x) { #endif -inline int cpu_count_get() { - static int count = 0; - if (count <= 0) { -#ifdef OSTD_PLATFORM_WIN32 - SYSTEM_INFO info; - GetSystemInfo(&info); - count = info.dwNumberOfProcessors; -#elif defined(_SC_NPROCESSORS_ONLN) - count = int(sysconf(_SC_NPROCESSORS_ONLN)); -#endif - if (count <= 0) - count = 1; - } - return count; -} - } #endif \ No newline at end of file diff --git a/ostd/thread.hh b/ostd/thread.hh index 4ce7f78..28f705a 100644 --- a/ostd/thread.hh +++ b/ostd/thread.hh @@ -9,6 +9,13 @@ #include #include +#ifndef OSTD_PLATFORM_WIN32 +#include +#else +#define WIN32_LEAN_AND_MEAN +#include +#endif + #include "ostd/memory.hh" #include "ostd/platform.hh" #include "ostd/type_traits.hh" @@ -158,6 +165,22 @@ struct Thread { other.p_thread = cur; } + static ostd::uint hardware_concurrency() { + static ostd::uint count = 0; + if (count <= 0) { +#ifdef OSTD_PLATFORM_WIN32 + SYSTEM_INFO info; + GetSystemInfo(&info); + count = info.dwNumberOfProcessors; +#elif defined(_SC_NPROCESSORS_ONLN) + count = ostd::uint(sysconf(_SC_NPROCESSORS_ONLN)); +#endif + if (count <= 0) + count = 1; + } + return count; + } + private: pthread_t p_thread; };