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.
This commit is contained in:
Mahmoud Al-Qudsi 2018-03-04 21:13:31 -06:00
parent cf8850a33f
commit 000892e315
4 changed files with 18 additions and 21 deletions

View file

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

View file

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

View file

@ -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 <typename CharType_t>
static CharType_t **make_null_terminated_array_helper(
const std::vector<std::basic_string<CharType_t> > &argv) {

View file

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