| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef WIN32STDTHREAD_H |
| 21 |
#define WIN32STDTHREAD_H |
| 22 |
|
| 23 |
#if !defined(__cplusplus) || (__cplusplus < 201103L) |
| 24 |
#error A C++11 compiler is required! |
| 25 |
#endif |
| 26 |
|
| 27 |
|
| 28 |
#include <thread> |
| 29 |
|
| 30 |
#include <cstddef> |
| 31 |
#include <cerrno> |
| 32 |
#include <exception> |
| 33 |
#include <system_error> |
| 34 |
#include <functional> |
| 35 |
#include <tuple> |
| 36 |
#include <chrono> |
| 37 |
#include <memory> |
| 38 |
#include <iosfwd> |
| 39 |
#include <utility> |
| 40 |
|
| 41 |
#include "mingw.invoke.h" |
| 42 |
|
| 43 |
#if (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) |
| 44 |
#pragma message "The Windows API that MinGW-w32 provides is not fully compatible\ |
| 45 |
with Microsoft's API. We'll try to work around this, but we can make no\ |
| 46 |
guarantees. This problem does not exist in MinGW-w64." |
| 47 |
#include <windows.h> |
| 48 |
#else |
| 49 |
#include <synchapi.h> |
| 50 |
#include <handleapi.h> |
| 51 |
#include <sysinfoapi.h> |
| 52 |
#include <processthreadsapi.h> |
| 53 |
#endif |
| 54 |
#include <process.h> |
| 55 |
|
| 56 |
#ifndef NDEBUG |
| 57 |
#include <cstdio> |
| 58 |
#endif |
| 59 |
|
| 60 |
#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0501) |
| 61 |
#error To use the MinGW-std-threads library, you will need to define the macro _WIN32_WINNT to be 0x0501 (Windows XP) or higher. |
| 62 |
#endif |
| 63 |
|
| 64 |
|
| 65 |
namespace mingw_stdthread |
| 66 |
{ |
| 67 |
namespace detail |
| 68 |
{ |
| 69 |
template<std::size_t...> |
| 70 |
struct IntSeq {}; |
| 71 |
|
| 72 |
template<std::size_t N, std::size_t... S> |
| 73 |
struct GenIntSeq : GenIntSeq<N-1, N-1, S...> { }; |
| 74 |
|
| 75 |
template<std::size_t... S> |
| 76 |
struct GenIntSeq<0, S...> { typedef IntSeq<S...> type; }; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
template<class Func, class T, typename... Args> |
| 81 |
class ThreadFuncCall; |
| 82 |
|
| 83 |
template<class Func, std::size_t... S, typename... Args> |
| 84 |
class ThreadFuncCall<Func, detail::IntSeq<S...>, Args...> |
| 85 |
{ |
| 86 |
static_assert(sizeof...(S) == sizeof...(Args), "Args must match."); |
| 87 |
using Tuple = std::tuple<typename std::decay<Args>::type...>; |
| 88 |
typename std::decay<Func>::type mFunc; |
| 89 |
Tuple mArgs; |
| 90 |
|
| 91 |
public: |
| 92 |
ThreadFuncCall(Func&& aFunc, Args&&... aArgs) |
| 93 |
: mFunc(std::forward<Func>(aFunc)), |
| 94 |
mArgs(std::forward<Args>(aArgs)...) |
| 95 |
{ |
| 96 |
} |
| 97 |
|
| 98 |
void callFunc() |
| 99 |
{ |
| 100 |
detail::invoke(std::move(mFunc), std::move(std::get<S>(mArgs)) ...); |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
class ThreadIdTool; |
| 106 |
} |
| 107 |
|
| 108 |
class thread |
| 109 |
{ |
| 110 |
public: |
| 111 |
class id |
| 112 |
{ |
| 113 |
DWORD mId = 0; |
| 114 |
friend class thread; |
| 115 |
friend class std::hash<id>; |
| 116 |
friend class detail::ThreadIdTool; |
| 117 |
explicit id(DWORD aId) noexcept : mId(aId){} |
| 118 |
public: |
| 119 |
id (void) noexcept = default; |
| 120 |
friend bool operator==(id x, id y) noexcept {return x.mId == y.mId; } |
| 121 |
friend bool operator!=(id x, id y) noexcept {return x.mId != y.mId; } |
| 122 |
friend bool operator< (id x, id y) noexcept {return x.mId < y.mId; } |
| 123 |
friend bool operator<=(id x, id y) noexcept {return x.mId <= y.mId; } |
| 124 |
friend bool operator> (id x, id y) noexcept {return x.mId > y.mId; } |
| 125 |
friend bool operator>=(id x, id y) noexcept {return x.mId >= y.mId; } |
| 126 |
|
| 127 |
template<class _CharT, class _Traits> |
| 128 |
friend std::basic_ostream<_CharT, _Traits>& |
| 129 |
operator<<(std::basic_ostream<_CharT, _Traits>& __out, id __id) |
| 130 |
{ |
| 131 |
if (__id.mId == 0) |
| 132 |
{ |
| 133 |
return __out << "(invalid std::thread::id)"; |
| 134 |
} |
| 135 |
else |
| 136 |
{ |
| 137 |
return __out << __id.mId; |
| 138 |
} |
| 139 |
} |
| 140 |
}; |
| 141 |
private: |
| 142 |
static constexpr HANDLE kInvalidHandle = nullptr; |
| 143 |
static constexpr DWORD kInfinite = 0xffffffffl; |
| 144 |
HANDLE mHandle; |
| 145 |
id mThreadId; |
| 146 |
|
| 147 |
template <class Call> |
| 148 |
static unsigned __stdcall threadfunc(void* arg) |
| 149 |
{ |
| 150 |
std::unique_ptr<Call> call(static_cast<Call*>(arg)); |
| 151 |
call->callFunc(); |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
static unsigned int _hardware_concurrency_helper() noexcept |
| 156 |
{ |
| 157 |
SYSTEM_INFO sysinfo; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0501) |
| 162 |
::GetNativeSystemInfo(&sysinfo); |
| 163 |
#else |
| 164 |
::GetSystemInfo(&sysinfo); |
| 165 |
#endif |
| 166 |
return sysinfo.dwNumberOfProcessors; |
| 167 |
} |
| 168 |
public: |
| 169 |
typedef HANDLE native_handle_type; |
| 170 |
id get_id() const noexcept {return mThreadId;} |
| 171 |
native_handle_type native_handle() const {return mHandle;} |
| 172 |
thread(): mHandle(kInvalidHandle), mThreadId(){} |
| 173 |
|
| 174 |
thread(thread&& other) |
| 175 |
:mHandle(other.mHandle), mThreadId(other.mThreadId) |
| 176 |
{ |
| 177 |
other.mHandle = kInvalidHandle; |
| 178 |
other.mThreadId = id{}; |
| 179 |
} |
| 180 |
|
| 181 |
thread(const thread &other)=delete; |
| 182 |
|
| 183 |
template<class Func, typename... Args> |
| 184 |
explicit thread(Func&& func, Args&&... args) : mHandle(), mThreadId() |
| 185 |
{ |
| 186 |
using ArgSequence = typename detail::GenIntSeq<sizeof...(Args)>::type; |
| 187 |
using Call = detail::ThreadFuncCall<Func, ArgSequence, Args...>; |
| 188 |
auto call = new Call( |
| 189 |
std::forward<Func>(func), std::forward<Args>(args)...); |
| 190 |
unsigned id_receiver; |
| 191 |
auto int_handle = _beginthreadex(NULL, 0, threadfunc<Call>, |
| 192 |
static_cast<LPVOID>(call), 0, &id_receiver); |
| 193 |
if (int_handle == 0) |
| 194 |
{ |
| 195 |
mHandle = kInvalidHandle; |
| 196 |
delete call; |
| 197 |
|
| 198 |
__builtin_trap(); |
| 199 |
} else { |
| 200 |
mThreadId.mId = id_receiver; |
| 201 |
mHandle = reinterpret_cast<HANDLE>(int_handle); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
bool joinable() const {return mHandle != kInvalidHandle;} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
void join() |
| 212 |
{ |
| 213 |
using namespace std; |
| 214 |
if (get_id() == id(GetCurrentThreadId())) |
| 215 |
__builtin_trap(); |
| 216 |
if (mHandle == kInvalidHandle) |
| 217 |
__builtin_trap(); |
| 218 |
if (!joinable()) |
| 219 |
__builtin_trap(); |
| 220 |
WaitForSingleObject(mHandle, kInfinite); |
| 221 |
CloseHandle(mHandle); |
| 222 |
mHandle = kInvalidHandle; |
| 223 |
mThreadId = id{}; |
| 224 |
} |
| 225 |
|
| 226 |
~thread() |
| 227 |
{ |
| 228 |
if (joinable()) |
| 229 |
{ |
| 230 |
#ifndef NDEBUG |
| 231 |
std::printf("Error: Must join() or detach() a thread before \ |
| 232 |
destroying it.\n"); |
| 233 |
#endif |
| 234 |
std::terminate(); |
| 235 |
} |
| 236 |
} |
| 237 |
thread& operator=(const thread&) = delete; |
| 238 |
thread& operator=(thread&& other) noexcept |
| 239 |
{ |
| 240 |
if (joinable()) |
| 241 |
{ |
| 242 |
#ifndef NDEBUG |
| 243 |
std::printf("Error: Must join() or detach() a thread before \ |
| 244 |
moving another thread to it.\n"); |
| 245 |
#endif |
| 246 |
std::terminate(); |
| 247 |
} |
| 248 |
swap(std::forward<thread>(other)); |
| 249 |
return *this; |
| 250 |
} |
| 251 |
void swap(thread&& other) noexcept |
| 252 |
{ |
| 253 |
std::swap(mHandle, other.mHandle); |
| 254 |
std::swap(mThreadId.mId, other.mThreadId.mId); |
| 255 |
} |
| 256 |
|
| 257 |
static unsigned int hardware_concurrency() noexcept |
| 258 |
{ |
| 259 |
static unsigned int cached = _hardware_concurrency_helper(); |
| 260 |
return cached; |
| 261 |
} |
| 262 |
|
| 263 |
void detach() |
| 264 |
{ |
| 265 |
if (!joinable()) |
| 266 |
{ |
| 267 |
using namespace std; |
| 268 |
__builtin_trap(); |
| 269 |
} |
| 270 |
if (mHandle != kInvalidHandle) |
| 271 |
{ |
| 272 |
CloseHandle(mHandle); |
| 273 |
mHandle = kInvalidHandle; |
| 274 |
} |
| 275 |
mThreadId = id{}; |
| 276 |
} |
| 277 |
}; |
| 278 |
|
| 279 |
namespace detail |
| 280 |
{ |
| 281 |
class ThreadIdTool |
| 282 |
{ |
| 283 |
public: |
| 284 |
static thread::id make_id (DWORD base_id) noexcept |
| 285 |
{ |
| 286 |
return thread::id(base_id); |
| 287 |
} |
| 288 |
}; |
| 289 |
} |
| 290 |
|
| 291 |
namespace this_thread |
| 292 |
{ |
| 293 |
inline thread::id get_id() noexcept |
| 294 |
{ |
| 295 |
return detail::ThreadIdTool::make_id(GetCurrentThreadId()); |
| 296 |
} |
| 297 |
inline void yield() noexcept {Sleep(0);} |
| 298 |
template< class Rep, class Period > |
| 299 |
void sleep_for( const std::chrono::duration<Rep,Period>& sleep_duration) |
| 300 |
{ |
| 301 |
static constexpr DWORD kInfinite = 0xffffffffl; |
| 302 |
using namespace std::chrono; |
| 303 |
using rep = milliseconds::rep; |
| 304 |
rep ms = duration_cast<milliseconds>(sleep_duration).count(); |
| 305 |
while (ms > 0) |
| 306 |
{ |
| 307 |
constexpr rep kMaxRep = static_cast<rep>(kInfinite - 1); |
| 308 |
auto sleepTime = (ms < kMaxRep) ? ms : kMaxRep; |
| 309 |
Sleep(static_cast<DWORD>(sleepTime)); |
| 310 |
ms -= sleepTime; |
| 311 |
} |
| 312 |
} |
| 313 |
template <class Clock, class Duration> |
| 314 |
void sleep_until(const std::chrono::time_point<Clock,Duration>& sleep_time) |
| 315 |
{ |
| 316 |
sleep_for(sleep_time-Clock::now()); |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
namespace std |
| 322 |
{ |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
#if defined(__MINGW32__ ) && !defined(_GLIBCXX_HAS_GTHREADS) && !defined(__clang__) |
| 329 |
using mingw_stdthread::thread; |
| 330 |
|
| 331 |
|
| 332 |
namespace this_thread |
| 333 |
{ |
| 334 |
using namespace mingw_stdthread::this_thread; |
| 335 |
} |
| 336 |
#elif !defined(MINGW_STDTHREAD_REDUNDANCY_WARNING) |
| 337 |
#define MINGW_STDTHREAD_REDUNDANCY_WARNING |
| 338 |
#pragma message "This version of MinGW seems to include a win32 port of\ |
| 339 |
pthreads, and probably already has C++11 std threading classes implemented,\ |
| 340 |
based on pthreads. These classes, found in namespace std, are not overridden\ |
| 341 |
by the mingw-std-thread library. If you would still like to use this\ |
| 342 |
implementation (as it is more lightweight), use the classes provided in\ |
| 343 |
namespace mingw_stdthread." |
| 344 |
#endif |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
template<> |
| 349 |
struct hash<mingw_stdthread::thread::id> |
| 350 |
{ |
| 351 |
typedef mingw_stdthread::thread::id argument_type; |
| 352 |
typedef size_t result_type; |
| 353 |
size_t operator() (const argument_type & i) const noexcept |
| 354 |
{ |
| 355 |
return i.mId; |
| 356 |
} |
| 357 |
}; |
| 358 |
} |
| 359 |
#endif |