Guarantee C locale

*  Remove all calls to setlocale to ensure that the global
   locale is always C.

*  Also replace beast::SystemStats::getNumCpus() with
   std:🧵:hardware_concurrency()
This commit is contained in:
Howard Hinnant
2014-10-13 19:20:53 -04:00
committed by Tom Ritchford
parent 7847ac3144
commit e965b7c0da
7 changed files with 4 additions and 113 deletions

View File

@@ -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 <const char*> (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;
}
//==============================================================================

View File

@@ -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;
}
//==============================================================================

View File

@@ -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
{

View File

@@ -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

View File

@@ -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; }

View File

@@ -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...)

View File

@@ -24,6 +24,8 @@
#include <beast/threads/Thread.h>
#include <beast/threads/semaphore.h>
#include <thread>
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 ();