From 000892e315abacfec7a8588b4ecdfa36e923a749 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Sun, 4 Mar 2018 21:13:31 -0600 Subject: [PATCH] Use constexpr for is_windows_subsystem_for_linux() To guarantee that at runtime there will be no branching, using a CMAKE test/define combined with a constexpr wrapper for code-friendliness. --- cmake/ConfigureChecks.cmake | 5 +++++ config_cmake.h.in | 3 +++ src/common.cpp | 19 ------------------- src/common.h | 12 ++++++++++-- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/cmake/ConfigureChecks.cmake b/cmake/ConfigureChecks.cmake index 8919749fd..052a39172 100644 --- a/cmake/ConfigureChecks.cmake +++ b/cmake/ConfigureChecks.cmake @@ -14,6 +14,11 @@ IF(APPLE) SET(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} "-Werror=unguarded-availability") ENDIF() +# Detect WSL. Does not match against native Windows/WIN32. +if (CMAKE_HOST_SYSTEM_VERSION MATCHES ".*-Microsoft") + SET(WSL 1) +endif() + # Set up the config.h file. SET(PACKAGE_NAME "fish") SET(PACKAGE_TARNAME "fish") diff --git a/config_cmake.h.in b/config_cmake.h.in index f5e186c43..bd4fe4d28 100644 --- a/config_cmake.h.in +++ b/config_cmake.h.in @@ -1,6 +1,9 @@ /* Define to 1 if you have the `backtrace_symbols' function. */ #cmakedefine HAVE_BACKTRACE_SYMBOLS 1 +/* Define to 1 if compiled on WSL */ +#cmakedefine WSL 1 + /* Define to 1 if you have the `clock_gettime' function. */ #cmakedefine HAVE_CLOCK_GETTIME 1 diff --git a/src/common.cpp b/src/common.cpp index 5de98289e..0c12b99ac 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1991,25 +1991,6 @@ void fish_mutex_t::assert_is_locked(const char *who, const char *caller) const { } } -/// Detect if we are Windows Subsystem for Linux by inspecting /proc/sys/kernel/osrelease -/// and checking if "Microsoft" is in the first line. -/// See https://github.com/Microsoft/WSL/issues/423 -bool is_windows_subsystem_for_linux() { - ASSERT_IS_NOT_FORKED_CHILD(); - static bool s_is_wsl = false; - static std::once_flag oflag; - std::call_once(oflag, []() { - // 'e' sets CLOEXEC if possible. - FILE *fp = fopen("/proc/sys/kernel/osrelease", "re"); - if (fp) { - char buff[256]; - if (fgets(buff, sizeof buff, fp)) s_is_wsl = (strstr(buff, "Microsoft") != NULL); - fclose(fp); - } - }); - return s_is_wsl; -} - template static CharType_t **make_null_terminated_array_helper( const std::vector > &argv) { diff --git a/src/common.h b/src/common.h index d3116995d..eec785c8d 100644 --- a/src/common.h +++ b/src/common.h @@ -757,8 +757,16 @@ void assert_is_not_forked_child(const char *who); #define ASSERT_IS_NOT_FORKED_CHILD_TRAMPOLINE(x) assert_is_not_forked_child(x) #define ASSERT_IS_NOT_FORKED_CHILD() ASSERT_IS_NOT_FORKED_CHILD_TRAMPOLINE(__FUNCTION__) -/// Return whether we are running in Windows Subsystem for Linux. -bool is_windows_subsystem_for_linux(); +/// Detect if we are Windows Subsystem for Linux by inspecting /proc/sys/kernel/osrelease +/// and checking if "Microsoft" is in the first line. +/// See https://github.com/Microsoft/WSL/issues/423 and Microsoft/WSL#2997 +constexpr bool is_windows_subsystem_for_linux() { +#ifdef WSL + return true; +#else + return false; +#endif +} extern "C" { __attribute__((noinline)) void debug_thread_error(void);