From e965b7c0dac11b00df2244aee5bde49ac451b66c Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Mon, 13 Oct 2014 19:20:53 -0400 Subject: [PATCH] Guarantee C locale * Remove all calls to setlocale to ensure that the global locale is always C. * Also replace beast::SystemStats::getNumCpus() with std::thread::hardware_concurrency() --- beast/module/core/native/bsd_SystemStats.cpp | 25 ----------------- .../module/core/native/linux_SystemStats.cpp | 14 ---------- beast/module/core/native/mac_SystemStats.mm | 25 ----------------- .../module/core/native/win32_SystemStats.cpp | 27 ------------------- beast/module/core/system/SystemStats.cpp | 4 +-- beast/module/core/system/SystemStats.h | 18 ------------- beast/module/core/thread/Workers.h | 4 ++- 7 files changed, 4 insertions(+), 113 deletions(-) diff --git a/beast/module/core/native/bsd_SystemStats.cpp b/beast/module/core/native/bsd_SystemStats.cpp index 407198bac3..f8fc7656c5 100644 --- a/beast/module/core/native/bsd_SystemStats.cpp +++ b/beast/module/core/native/bsd_SystemStats.cpp @@ -279,28 +279,6 @@ String SystemStats::getComputerName() return String::empty; } -String getLocaleValue (nl_item key) -{ - const char* oldLocale = ::setlocale (LC_ALL, ""); - return String (const_cast (nl_langinfo (key))); - ::setlocale (LC_ALL, oldLocale); -} - -String SystemStats::getUserLanguage() -{ - return "Uknown user language"; -} - -String SystemStats::getUserRegion() -{ - return "Unknown user region"; -} - -String SystemStats::getDisplayLanguage() -{ - return getUserLanguage(); -} - //============================================================================== void CPUInformation::initialise() noexcept { @@ -312,9 +290,6 @@ void CPUInformation::initialise() noexcept hasSSE3 = features2.contains ("SSE3"); const String amdfeatures2 (BSDStatsHelpers::getDmesgInfo (" AMD Features2=")); has3DNow = amdfeatures2.contains ("3DNow!"); - - GETSYSCTL("hw.ncpu", numCpus); - if (numCpus == -1) numCpus = 1; } //============================================================================== diff --git a/beast/module/core/native/linux_SystemStats.cpp b/beast/module/core/native/linux_SystemStats.cpp index 88372945da..f329ab127f 100644 --- a/beast/module/core/native/linux_SystemStats.cpp +++ b/beast/module/core/native/linux_SystemStats.cpp @@ -117,18 +117,6 @@ String SystemStats::getComputerName() return String::empty; } -static String getLocaleValue (nl_item key) -{ - const char* oldLocale = ::setlocale (LC_ALL, ""); - String result (String::fromUTF8 (nl_langinfo (key))); - ::setlocale (LC_ALL, oldLocale); - return result; -} - -String SystemStats::getUserLanguage() { return getLocaleValue (_NL_IDENTIFICATION_LANGUAGE); } -String SystemStats::getUserRegion() { return getLocaleValue (_NL_IDENTIFICATION_TERRITORY); } -String SystemStats::getDisplayLanguage() { return getUserLanguage(); } - //============================================================================== void CPUInformation::initialise() noexcept { @@ -138,8 +126,6 @@ void CPUInformation::initialise() noexcept hasSSE2 = flags.contains ("sse2"); hasSSE3 = flags.contains ("sse3"); has3DNow = flags.contains ("3dnow"); - - numCpus = LinuxStatsHelpers::getCpuInfo ("processor").getIntValue() + 1; } //============================================================================== diff --git a/beast/module/core/native/mac_SystemStats.mm b/beast/module/core/native/mac_SystemStats.mm index fb900160c5..ba94883f74 100644 --- a/beast/module/core/native/mac_SystemStats.mm +++ b/beast/module/core/native/mac_SystemStats.mm @@ -79,12 +79,6 @@ void CPUInformation::initialise() noexcept has3DNow = (b & (1u << 31)) != 0; hasSSE3 = (c & (1u << 0)) != 0; #endif - - #if BEAST_IOS || (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5) - numCpus = (int) [[NSProcessInfo processInfo] activeProcessorCount]; - #else - numCpus = (int) MPProcessors(); - #endif } #if BEAST_MAC @@ -210,25 +204,6 @@ String SystemStats::getComputerName() return String::empty; } -static String getLocaleValue (CFStringRef key) -{ - CFLocaleRef cfLocale = CFLocaleCopyCurrent(); - const String result (String::fromCFString ((CFStringRef) CFLocaleGetValue (cfLocale, key))); - CFRelease (cfLocale); - return result; -} - -String SystemStats::getUserLanguage() { return getLocaleValue (kCFLocaleLanguageCode); } -String SystemStats::getUserRegion() { return getLocaleValue (kCFLocaleCountryCode); } - -String SystemStats::getDisplayLanguage() -{ - CFArrayRef cfPrefLangs = CFLocaleCopyPreferredLanguages(); - const String result (String::fromCFString ((CFStringRef) CFArrayGetValueAtIndex (cfPrefLangs, 0))); - CFRelease (cfPrefLangs); - return result; -} - //============================================================================== class HiResCounterHandler { diff --git a/beast/module/core/native/win32_SystemStats.cpp b/beast/module/core/native/win32_SystemStats.cpp index 74bb209bd1..acd7e08027 100644 --- a/beast/module/core/native/win32_SystemStats.cpp +++ b/beast/module/core/native/win32_SystemStats.cpp @@ -109,10 +109,6 @@ void CPUInformation::initialise() noexcept hasSSE2 = IsProcessorFeaturePresent (PF_XMMI64_INSTRUCTIONS_AVAILABLE) != 0; hasSSE3 = IsProcessorFeaturePresent (13 /*PF_SSE3_INSTRUCTIONS_AVAILABLE*/) != 0; has3DNow = IsProcessorFeaturePresent (7 /*PF_AMD3D_INSTRUCTIONS_AVAILABLE*/) != 0; - - SYSTEM_INFO systemInfo; - GetNativeSystemInfo (&systemInfo); - numCpus = (int) systemInfo.dwNumberOfProcessors; } //============================================================================== @@ -397,27 +393,4 @@ String SystemStats::getComputerName() return String (text, len); } -static String getLocaleValue (LCID locale, LCTYPE key, const char* defaultValue) -{ - TCHAR buffer [256] = { 0 }; - if (GetLocaleInfo (locale, key, buffer, 255) > 0) - return buffer; - - return defaultValue; -} - -String SystemStats::getUserLanguage() { return getLocaleValue (LOCALE_USER_DEFAULT, LOCALE_SISO639LANGNAME, "en"); } -String SystemStats::getUserRegion() { return getLocaleValue (LOCALE_USER_DEFAULT, LOCALE_SISO3166CTRYNAME, "US"); } - -String SystemStats::getDisplayLanguage() -{ - DynamicLibrary dll ("kernel32.dll"); - BEAST_LOAD_WINAPI_FUNCTION (dll, GetUserDefaultUILanguage, getUserDefaultUILanguage, LANGID, (void)) - - if (getUserDefaultUILanguage != nullptr) - return getLocaleValue (MAKELCID (getUserDefaultUILanguage(), SORT_DEFAULT), LOCALE_SISO639LANGNAME, "en"); - - return "en"; -} - } // beast diff --git a/beast/module/core/system/SystemStats.cpp b/beast/module/core/system/SystemStats.cpp index be7feddd0f..ab158e185b 100644 --- a/beast/module/core/system/SystemStats.cpp +++ b/beast/module/core/system/SystemStats.cpp @@ -47,7 +47,7 @@ String SystemStats::getBeastVersion() struct CPUInformation { CPUInformation() noexcept - : numCpus (0), hasMMX (false), hasSSE (false), + : hasMMX (false), hasSSE (false), hasSSE2 (false), hasSSE3 (false), has3DNow (false) { initialise(); @@ -55,7 +55,6 @@ struct CPUInformation void initialise() noexcept; - int numCpus; bool hasMMX, hasSSE, hasSSE2, hasSSE3, has3DNow; }; @@ -65,7 +64,6 @@ static const CPUInformation& getCPUInformation() noexcept return info; } -int SystemStats::getNumCpus() noexcept { return getCPUInformation().numCpus; } bool SystemStats::hasMMX() noexcept { return getCPUInformation().hasMMX; } bool SystemStats::hasSSE() noexcept { return getCPUInformation().hasSSE; } bool SystemStats::hasSSE2() noexcept { return getCPUInformation().hasSSE2; } diff --git a/beast/module/core/system/SystemStats.h b/beast/module/core/system/SystemStats.h index b70f4e0eea..9bbd7c7855 100644 --- a/beast/module/core/system/SystemStats.h +++ b/beast/module/core/system/SystemStats.h @@ -105,27 +105,9 @@ namespace SystemStats /** Returns the host-name of the computer. */ String getComputerName(); - /** Returns the language of the user's locale. - The return value is a 2 or 3 letter language code (ISO 639-1 or ISO 639-2) - */ - String getUserLanguage(); - - /** Returns the region of the user's locale. - The return value is a 2 letter country code (ISO 3166-1 alpha-2). - */ - String getUserRegion(); - - /** Returns the user's display language. - The return value is a 2 or 3 letter language code (ISO 639-1 or ISO 639-2) - */ - String getDisplayLanguage(); - //============================================================================== // CPU and memory information.. - /** Returns the number of CPU cores. */ - int getNumCpus() noexcept; - /** Returns the approximate CPU speed. @returns the speed in megahertz, e.g. 1500, 2500, 32000 (depending on what year you're reading this...) diff --git a/beast/module/core/thread/Workers.h b/beast/module/core/thread/Workers.h index 8ae56fe13f..0fb8daf155 100644 --- a/beast/module/core/thread/Workers.h +++ b/beast/module/core/thread/Workers.h @@ -24,6 +24,8 @@ #include #include +#include + namespace beast { /** A group of threads that process tasks. @@ -54,7 +56,7 @@ public: */ explicit Workers (Callback& callback, String const& threadNames = "Worker", - int numberOfThreads = SystemStats::getNumCpus ()); + int numberOfThreads = std::thread::hardware_concurrency()); ~Workers ();