Path: blob/master/thirdparty/mingw-std-threads/mingw.thread.h
9905 views
/**1* @file mingw.thread.h2* @brief std::thread implementation for MinGW3* (c) 2013-2016 by Mega Limited, Auckland, New Zealand4* @author Alexander Vassilev5*6* @copyright Simplified (2-clause) BSD License.7* You should have received a copy of the license along with this8* program.9*10* This code is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.13* @note14* This file may become part of the mingw-w64 runtime package. If/when this happens,15* the appropriate license will be added, i.e. this code will become dual-licensed,16* and the current BSD 2-clause license will stay.17*/1819#ifndef WIN32STDTHREAD_H20#define WIN32STDTHREAD_H2122#if !defined(__cplusplus) || (__cplusplus < 201103L)23#error A C++11 compiler is required!24#endif2526// Use the standard classes for std::, if available.27#include <thread>2829#include <cstddef> // For std::size_t30#include <cerrno> // Detect error type.31#include <exception> // For std::terminate32#include <system_error> // For std::system_error33#include <functional> // For std::hash34#include <tuple> // For std::tuple35#include <chrono> // For sleep timing.36#include <memory> // For std::unique_ptr37#include <iosfwd> // Stream output for thread ids.38#include <utility> // For std::swap, std::forward3940#include "mingw.invoke.h"4142#if (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))43#pragma message "The Windows API that MinGW-w32 provides is not fully compatible\44with Microsoft's API. We'll try to work around this, but we can make no\45guarantees. This problem does not exist in MinGW-w64."46#include <windows.h> // No further granularity can be expected.47#else48#include <synchapi.h> // For WaitForSingleObject49#include <handleapi.h> // For CloseHandle, etc.50#include <sysinfoapi.h> // For GetNativeSystemInfo51#include <processthreadsapi.h> // For GetCurrentThreadId52#endif53#include <process.h> // For _beginthreadex5455#ifndef NDEBUG56#include <cstdio>57#endif5859#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0501)60#error To use the MinGW-std-threads library, you will need to define the macro _WIN32_WINNT to be 0x0501 (Windows XP) or higher.61#endif6263// Instead of INVALID_HANDLE_VALUE, _beginthreadex returns 0.64namespace mingw_stdthread65{66namespace detail67{68template<std::size_t...>69struct IntSeq {};7071template<std::size_t N, std::size_t... S>72struct GenIntSeq : GenIntSeq<N-1, N-1, S...> { };7374template<std::size_t... S>75struct GenIntSeq<0, S...> { typedef IntSeq<S...> type; };7677// Use a template specialization to avoid relying on compiler optimization78// when determining the parameter integer sequence.79template<class Func, class T, typename... Args>80class ThreadFuncCall;81// We can't define the Call struct in the function - the standard forbids template methods in that case82template<class Func, std::size_t... S, typename... Args>83class ThreadFuncCall<Func, detail::IntSeq<S...>, Args...>84{85static_assert(sizeof...(S) == sizeof...(Args), "Args must match.");86using Tuple = std::tuple<typename std::decay<Args>::type...>;87typename std::decay<Func>::type mFunc;88Tuple mArgs;8990public:91ThreadFuncCall(Func&& aFunc, Args&&... aArgs)92: mFunc(std::forward<Func>(aFunc)),93mArgs(std::forward<Args>(aArgs)...)94{95}9697void callFunc()98{99detail::invoke(std::move(mFunc), std::move(std::get<S>(mArgs)) ...);100}101};102103// Allow construction of threads without exposing implementation.104class ThreadIdTool;105} // Namespace "detail"106107class thread108{109public:110class id111{112DWORD mId = 0;113friend class thread;114friend class std::hash<id>;115friend class detail::ThreadIdTool;116explicit id(DWORD aId) noexcept : mId(aId){}117public:118id (void) noexcept = default;119friend bool operator==(id x, id y) noexcept {return x.mId == y.mId; }120friend bool operator!=(id x, id y) noexcept {return x.mId != y.mId; }121friend bool operator< (id x, id y) noexcept {return x.mId < y.mId; }122friend bool operator<=(id x, id y) noexcept {return x.mId <= y.mId; }123friend bool operator> (id x, id y) noexcept {return x.mId > y.mId; }124friend bool operator>=(id x, id y) noexcept {return x.mId >= y.mId; }125126template<class _CharT, class _Traits>127friend std::basic_ostream<_CharT, _Traits>&128operator<<(std::basic_ostream<_CharT, _Traits>& __out, id __id)129{130if (__id.mId == 0)131{132return __out << "(invalid std::thread::id)";133}134else135{136return __out << __id.mId;137}138}139};140private:141static constexpr HANDLE kInvalidHandle = nullptr;142static constexpr DWORD kInfinite = 0xffffffffl;143HANDLE mHandle;144id mThreadId;145146template <class Call>147static unsigned __stdcall threadfunc(void* arg)148{149std::unique_ptr<Call> call(static_cast<Call*>(arg));150call->callFunc();151return 0;152}153154static unsigned int _hardware_concurrency_helper() noexcept155{156SYSTEM_INFO sysinfo;157// This is one of the few functions used by the library which has a nearly-158// equivalent function defined in earlier versions of Windows. Include the159// workaround, just as a reminder that it does exist.160#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0501)161::GetNativeSystemInfo(&sysinfo);162#else163::GetSystemInfo(&sysinfo);164#endif165return sysinfo.dwNumberOfProcessors;166}167public:168typedef HANDLE native_handle_type;169id get_id() const noexcept {return mThreadId;}170native_handle_type native_handle() const {return mHandle;}171thread(): mHandle(kInvalidHandle), mThreadId(){}172173thread(thread&& other)174:mHandle(other.mHandle), mThreadId(other.mThreadId)175{176other.mHandle = kInvalidHandle;177other.mThreadId = id{};178}179180thread(const thread &other)=delete;181182template<class Func, typename... Args>183explicit thread(Func&& func, Args&&... args) : mHandle(), mThreadId()184{185using ArgSequence = typename detail::GenIntSeq<sizeof...(Args)>::type;186using Call = detail::ThreadFuncCall<Func, ArgSequence, Args...>;187auto call = new Call(188std::forward<Func>(func), std::forward<Args>(args)...);189unsigned id_receiver;190auto int_handle = _beginthreadex(NULL, 0, threadfunc<Call>,191static_cast<LPVOID>(call), 0, &id_receiver);192if (int_handle == 0)193{194mHandle = kInvalidHandle;195delete call;196// Note: Should only throw EINVAL, EAGAIN, EACCES197__builtin_trap();198} else {199mThreadId.mId = id_receiver;200mHandle = reinterpret_cast<HANDLE>(int_handle);201}202}203204bool joinable() const {return mHandle != kInvalidHandle;}205206// Note: Due to lack of synchronization, this function has a race condition207// if called concurrently, which leads to undefined behavior. The same applies208// to all other member functions of this class, but this one is mentioned209// explicitly.210void join()211{212using namespace std;213if (get_id() == id(GetCurrentThreadId()))214__builtin_trap();215if (mHandle == kInvalidHandle)216__builtin_trap();217if (!joinable())218__builtin_trap();219WaitForSingleObject(mHandle, kInfinite);220CloseHandle(mHandle);221mHandle = kInvalidHandle;222mThreadId = id{};223}224225~thread()226{227if (joinable())228{229#ifndef NDEBUG230std::printf("Error: Must join() or detach() a thread before \231destroying it.\n");232#endif233std::terminate();234}235}236thread& operator=(const thread&) = delete;237thread& operator=(thread&& other) noexcept238{239if (joinable())240{241#ifndef NDEBUG242std::printf("Error: Must join() or detach() a thread before \243moving another thread to it.\n");244#endif245std::terminate();246}247swap(std::forward<thread>(other));248return *this;249}250void swap(thread&& other) noexcept251{252std::swap(mHandle, other.mHandle);253std::swap(mThreadId.mId, other.mThreadId.mId);254}255256static unsigned int hardware_concurrency() noexcept257{258static unsigned int cached = _hardware_concurrency_helper();259return cached;260}261262void detach()263{264if (!joinable())265{266using namespace std;267__builtin_trap();268}269if (mHandle != kInvalidHandle)270{271CloseHandle(mHandle);272mHandle = kInvalidHandle;273}274mThreadId = id{};275}276};277278namespace detail279{280class ThreadIdTool281{282public:283static thread::id make_id (DWORD base_id) noexcept284{285return thread::id(base_id);286}287};288} // Namespace "detail"289290namespace this_thread291{292inline thread::id get_id() noexcept293{294return detail::ThreadIdTool::make_id(GetCurrentThreadId());295}296inline void yield() noexcept {Sleep(0);}297template< class Rep, class Period >298void sleep_for( const std::chrono::duration<Rep,Period>& sleep_duration)299{300static constexpr DWORD kInfinite = 0xffffffffl;301using namespace std::chrono;302using rep = milliseconds::rep;303rep ms = duration_cast<milliseconds>(sleep_duration).count();304while (ms > 0)305{306constexpr rep kMaxRep = static_cast<rep>(kInfinite - 1);307auto sleepTime = (ms < kMaxRep) ? ms : kMaxRep;308Sleep(static_cast<DWORD>(sleepTime));309ms -= sleepTime;310}311}312template <class Clock, class Duration>313void sleep_until(const std::chrono::time_point<Clock,Duration>& sleep_time)314{315sleep_for(sleep_time-Clock::now());316}317}318} // Namespace mingw_stdthread319320namespace std321{322// Because of quirks of the compiler, the common "using namespace std;"323// directive would flatten the namespaces and introduce ambiguity where there324// was none. Direct specification (std::), however, would be unaffected.325// Take the safe option, and include only in the presence of MinGW's win32326// implementation.327#if defined(__MINGW32__ ) && !defined(_GLIBCXX_HAS_GTHREADS) && !defined(__clang__)328using mingw_stdthread::thread;329// Remove ambiguity immediately, to avoid problems arising from the above.330//using std::thread;331namespace this_thread332{333using namespace mingw_stdthread::this_thread;334}335#elif !defined(MINGW_STDTHREAD_REDUNDANCY_WARNING) // Skip repetition336#define MINGW_STDTHREAD_REDUNDANCY_WARNING337#pragma message "This version of MinGW seems to include a win32 port of\338pthreads, and probably already has C++11 std threading classes implemented,\339based on pthreads. These classes, found in namespace std, are not overridden\340by the mingw-std-thread library. If you would still like to use this\341implementation (as it is more lightweight), use the classes provided in\342namespace mingw_stdthread."343#endif344345// Specialize hash for this implementation's thread::id, even if the346// std::thread::id already has a hash.347template<>348struct hash<mingw_stdthread::thread::id>349{350typedef mingw_stdthread::thread::id argument_type;351typedef size_t result_type;352size_t operator() (const argument_type & i) const noexcept353{354return i.mId;355}356};357}358#endif // WIN32STDTHREAD_H359360361