From 390c5246d2ef178dc66af416b5acf13eba268c8e Mon Sep 17 00:00:00 2001 From: Lucian Petrut Date: Tue, 29 Jun 2021 06:51:11 -0700 Subject: [PATCH] Allow using WindowsThread with Mingw (#8108) Summary: Allow using WindowsThread with Mingw Most Mingw builds require Posix threads in order to use std::thread. As per https://github.com/facebook/rocksdb/issues/7764, this is not always the case. That being considered, we're going to improve the Mingw thread model checks. Closes: https://github.com/facebook/rocksdb/issues/7764 Signed-off-by: Lucian Petrut Pull Request resolved: https://github.com/facebook/rocksdb/pull/8108 Reviewed By: jay-zhuang Differential Revision: D27365778 Pulled By: mrambacher fbshipit-source-id: 2c15b1f04ae90e1e3a25a33e86ceb779224a9529 --- CMakeLists.txt | 9 ++------- port/win/env_win.cc | 3 +-- port/win/env_win.h | 4 ++-- port/win/win_thread.cc | 8 +++++++- port/win/win_thread.h | 3 +++ 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ced8eeef..b0bf022bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -934,13 +934,8 @@ if(WIN32) port/win/env_win.cc port/win/env_default.cc port/win/port_win.cc - port/win/win_logger.cc) - if(NOT MINGW) - # Mingw only supports std::thread when using - # posix threads. - list(APPEND SOURCES - port/win/win_thread.cc) - endif() + port/win/win_logger.cc + port/win/win_thread.cc) if(WITH_XPRESS) list(APPEND SOURCES port/win/xpress_win.cc) diff --git a/port/win/env_win.cc b/port/win/env_win.cc index cc337c1f8..1cab4cbe9 100644 --- a/port/win/env_win.cc +++ b/port/win/env_win.cc @@ -30,7 +30,6 @@ #include "port/win/env_win.h" #include "port/win/io_win.h" #include "port/win/win_logger.h" -#include "port/win/win_thread.h" #include "rocksdb/env.h" #include "rocksdb/slice.h" #include "strsafe.h" @@ -1291,7 +1290,7 @@ void WinEnvThreads::StartThread(void (*function)(void* arg), void* arg) { state->user_function = function; state->arg = arg; try { - ROCKSDB_NAMESPACE::port::WindowsThread th(&StartThreadWrapper, state.get()); + Thread th(&StartThreadWrapper, state.get()); state.release(); std::lock_guard lg(mu_); diff --git a/port/win/env_win.h b/port/win/env_win.h index 54d3e7dbf..d0fd78ebb 100644 --- a/port/win/env_win.h +++ b/port/win/env_win.h @@ -23,7 +23,7 @@ #include #include "env/composite_env_wrapper.h" -#include "port/win/win_thread.h" +#include "port/port.h" #include "rocksdb/env.h" #include "rocksdb/file_system.h" #include "rocksdb/system_clock.h" @@ -71,7 +71,7 @@ class WinEnvThreads { Env* hosted_env_; mutable std::mutex mu_; std::vector thread_pools_; - std::vector threads_to_join_; + std::vector threads_to_join_; }; class WinClock : public SystemClock { diff --git a/port/win/win_thread.cc b/port/win/win_thread.cc index 2bc05e8da..56685792b 100644 --- a/port/win/win_thread.cc +++ b/port/win/win_thread.cc @@ -8,6 +8,11 @@ // found in the LICENSE file. See the AUTHORS file for names of contributors. #if defined(OS_WIN) +// Most Mingw builds support std::thread only when using posix threads. +// In that case, some of these functions will be unavailable. +// Note that we're using either WindowsThread or std::thread, depending on +// which one is available. +#ifndef _POSIX_THREADS #include "port/win/win_thread.h" @@ -180,4 +185,5 @@ unsigned int __stdcall WindowsThread::Data::ThreadProc(void* arg) { } // namespace port } // namespace ROCKSDB_NAMESPACE -#endif +#endif // OS_WIN +#endif // !_POSIX_THREADS diff --git a/port/win/win_thread.h b/port/win/win_thread.h index 472b29955..ffdfdc11b 100644 --- a/port/win/win_thread.h +++ b/port/win/win_thread.h @@ -9,6 +9,8 @@ #pragma once +#ifndef _POSIX_THREADS + #include #include #include @@ -120,3 +122,4 @@ inline void swap(ROCKSDB_NAMESPACE::port::WindowsThread& th1, } } // namespace std +#endif // !_POSIX_THREADS