diff --git a/modules/core/util/func_tests/util_tests.cpp b/modules/core/util/func_tests/util_tests.cpp index 2b7ca797..35901076 100644 --- a/modules/core/util/func_tests/util_tests.cpp +++ b/modules/core/util/func_tests/util_tests.cpp @@ -6,8 +6,15 @@ #include "core/util/include/util.hpp" +#ifdef _WIN32 +#include +inline int unsetenv(const char* name) { return SetEnvironmentVariableA(name, NULL) ? 0 : -1; } +inline int setenv(const char* name, const char* value, int /*overwrite*/) { + return SetEnvironmentVariableA(name, value) ? 0 : -1; +} +#endif + TEST(util_tests, check_unset_env) { -#ifndef _WIN32 int save_var = ppc::util::GetPPCNumThreads(); unsetenv("PPC_NUM_THREADS"); // NOLINT(concurrency-mt-unsafe) @@ -15,13 +22,9 @@ TEST(util_tests, check_unset_env) { EXPECT_EQ(ppc::util::GetPPCNumThreads(), 1); setenv("PPC_NUM_THREADS", std::to_string(save_var).c_str(), 1); // NOLINT(concurrency-mt-unsafe) -#else - GTEST_SKIP(); -#endif } TEST(util_tests, check_set_env) { -#ifndef _WIN32 int save_var = ppc::util::GetPPCNumThreads(); const int num_threads = static_cast(std::thread::hardware_concurrency()); @@ -30,7 +33,4 @@ TEST(util_tests, check_set_env) { EXPECT_EQ(ppc::util::GetPPCNumThreads(), num_threads); setenv("PPC_NUM_THREADS", std::to_string(save_var).c_str(), 1); // NOLINT(concurrency-mt-unsafe) -#else - GTEST_SKIP(); -#endif } diff --git a/modules/core/util/src/util.cpp b/modules/core/util/src/util.cpp index a1516e8d..64d8854b 100644 --- a/modules/core/util/src/util.cpp +++ b/modules/core/util/src/util.cpp @@ -1,13 +1,7 @@ +#define _CRT_SECURE_NO_WARNINGS #include "core/util/include/util.hpp" #include -#ifdef _WIN32 -#include -#include -#include -#include -#endif - #include #include @@ -17,17 +11,13 @@ std::string ppc::util::GetAbsolutePath(const std::string &relative_path) { } int ppc::util::GetPPCNumThreads() { -#ifdef _WIN32 - size_t len; - char omp_env[100]; - errno_t err = getenv_s(&len, omp_env, sizeof(omp_env), "PPC_NUM_THREADS"); - if (err != 0 || len == 0) { - omp_env[0] = '\0'; + const char *env = std::getenv("PPC_NUM_THREADS"); // NOLINT(concurrency-mt-unsafe) + if ((env != nullptr) && (*env != 0)) { + char *endptr = nullptr; + long val = std::strtol(env, &endptr, 10); + if (endptr != env && val > 0) { + return static_cast(val); + } } - int num_threads = std::atoi(omp_env); -#else - const char *omp_env = std::getenv("PPC_NUM_THREADS"); // NOLINT(concurrency-mt-unsafe) - int num_threads = (omp_env != nullptr) ? std::atoi(omp_env) : 1; -#endif - return num_threads; + return 1; }