Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mingw.thread.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /**
  2. * @file mingw.thread.h
  3. * @brief std::thread implementation for MinGW
  4. * (c) 2013-2016 by Mega Limited, Auckland, New Zealand
  5. * @author Alexander Vassilev
  6. *
  7. * @copyright Simplified (2-clause) BSD License.
  8. * You should have received a copy of the license along with this
  9. * program.
  10. *
  11. * This code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * @note
  15. * This file may become part of the mingw-w64 runtime package. If/when this happens,
  16. * the appropriate license will be added, i.e. this code will become dual-licensed,
  17. * and the current BSD 2-clause license will stay.
  18. */
  19. #ifndef WIN32STDTHREAD_H
  20. #define WIN32STDTHREAD_H
  21. #if !defined(__cplusplus) || (__cplusplus < 201103L)
  22. #error A C++11 compiler is required!
  23. #endif
  24. // Use the standard classes for std::, if available.
  25. #include <thread>
  26. #include <cstddef> // For std::size_t
  27. #include <cerrno> // Detect error type.
  28. #include <exception> // For std::terminate
  29. #include <system_error> // For std::system_error
  30. #include <functional> // For std::hash
  31. #include <tuple> // For std::tuple
  32. #include <chrono> // For sleep timing.
  33. #include <memory> // For std::unique_ptr
  34. #include <iosfwd> // Stream output for thread ids.
  35. #include <utility> // For std::swap, std::forward
  36. #include "mingw.invoke.h"
  37. #include <synchapi.h> // For WaitForSingleObject
  38. #include <handleapi.h> // For CloseHandle, etc.
  39. #include <sysinfoapi.h> // For GetNativeSystemInfo
  40. #include <processthreadsapi.h> // For GetCurrentThreadId
  41. #include <process.h> // For _beginthreadex
  42. #ifndef NDEBUG
  43. #include <cstdio>
  44. #endif
  45. #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0501)
  46. #error To use the MinGW-std-threads library, you will need to define the macro _WIN32_WINNT to be 0x0501 (Windows XP) or higher.
  47. #endif
  48. // Instead of INVALID_HANDLE_VALUE, _beginthreadex returns 0.
  49. namespace mingw_stdthread
  50. {
  51. namespace detail
  52. {
  53. template<std::size_t...>
  54. struct IntSeq {};
  55. template<std::size_t N, std::size_t... S>
  56. struct GenIntSeq : GenIntSeq<N-1, N-1, S...> { };
  57. template<std::size_t... S>
  58. struct GenIntSeq<0, S...> { typedef IntSeq<S...> type; };
  59. // We can't define the Call struct in the function - the standard forbids template methods in that case
  60. template<class Func, typename... Args>
  61. class ThreadFuncCall
  62. {
  63. using Tuple = std::tuple<typename std::decay<Args>::type...>;
  64. typename std::decay<Func>::type mFunc;
  65. Tuple mArgs;
  66. template <std::size_t... S>
  67. void callFunc(detail::IntSeq<S...>)
  68. {
  69. // Note: Only called once (per thread)
  70. detail::invoke(std::move(mFunc), std::move(std::get<S>(mArgs)) ...);
  71. }
  72. public:
  73. ThreadFuncCall(Func&& aFunc, Args&&... aArgs)
  74. : mFunc(std::forward<Func>(aFunc)),
  75. mArgs(std::forward<Args>(aArgs)...)
  76. {
  77. }
  78. void callFunc()
  79. {
  80. callFunc(typename detail::GenIntSeq<sizeof...(Args)>::type());
  81. }
  82. };
  83. } // Namespace "detail"
  84. class thread
  85. {
  86. public:
  87. class id
  88. {
  89. DWORD mId;
  90. void clear() {mId = 0;}
  91. friend class thread;
  92. friend class std::hash<id>;
  93. public:
  94. explicit id(DWORD aId=0) noexcept : mId(aId){}
  95. friend bool operator==(id x, id y) noexcept {return x.mId == y.mId; }
  96. friend bool operator!=(id x, id y) noexcept {return x.mId != y.mId; }
  97. friend bool operator< (id x, id y) noexcept {return x.mId < y.mId; }
  98. friend bool operator<=(id x, id y) noexcept {return x.mId <= y.mId; }
  99. friend bool operator> (id x, id y) noexcept {return x.mId > y.mId; }
  100. friend bool operator>=(id x, id y) noexcept {return x.mId >= y.mId; }
  101. template<class _CharT, class _Traits>
  102. friend std::basic_ostream<_CharT, _Traits>&
  103. operator<<(std::basic_ostream<_CharT, _Traits>& __out, id __id)
  104. {
  105. if (__id.mId == 0)
  106. {
  107. return __out << "(invalid std::thread::id)";
  108. }
  109. else
  110. {
  111. return __out << __id.mId;
  112. }
  113. }
  114. };
  115. private:
  116. static constexpr HANDLE kInvalidHandle = nullptr;
  117. static constexpr DWORD kInfinite = 0xffffffffl;
  118. HANDLE mHandle;
  119. id mThreadId;
  120. template <class Call>
  121. static unsigned __stdcall threadfunc(void* arg)
  122. {
  123. std::unique_ptr<Call> call(static_cast<Call*>(arg));
  124. call->callFunc();
  125. return 0;
  126. }
  127. static unsigned int _hardware_concurrency_helper() noexcept
  128. {
  129. SYSTEM_INFO sysinfo;
  130. // This is one of the few functions used by the library which has a nearly-
  131. // equivalent function defined in earlier versions of Windows. Include the
  132. // workaround, just as a reminder that it does exist.
  133. #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0501)
  134. ::GetNativeSystemInfo(&sysinfo);
  135. #else
  136. ::GetSystemInfo(&sysinfo);
  137. #endif
  138. return sysinfo.dwNumberOfProcessors;
  139. }
  140. public:
  141. typedef HANDLE native_handle_type;
  142. id get_id() const noexcept {return mThreadId;}
  143. native_handle_type native_handle() const {return mHandle;}
  144. thread(): mHandle(kInvalidHandle), mThreadId(){}
  145. thread(thread&& other)
  146. :mHandle(other.mHandle), mThreadId(other.mThreadId)
  147. {
  148. other.mHandle = kInvalidHandle;
  149. other.mThreadId.clear();
  150. }
  151. thread(const thread &other)=delete;
  152. template<class Func, typename... Args>
  153. explicit thread(Func&& func, Args&&... args) : mHandle(), mThreadId()
  154. {
  155. typedef detail::ThreadFuncCall<Func, Args...> Call;
  156. auto call = new Call(
  157. std::forward<Func>(func), std::forward<Args>(args)...);
  158. auto int_handle = _beginthreadex(NULL, 0, threadfunc<Call>,
  159. static_cast<LPVOID>(call), 0,
  160. reinterpret_cast<unsigned*>(&(mThreadId.mId)));
  161. if (int_handle == 0)
  162. {
  163. mHandle = kInvalidHandle;
  164. int errnum = errno;
  165. delete call;
  166. // Note: Should only throw EINVAL, EAGAIN, EACCES
  167. throw std::system_error(errnum, std::generic_category());
  168. } else
  169. mHandle = reinterpret_cast<HANDLE>(int_handle);
  170. }
  171. bool joinable() const {return mHandle != kInvalidHandle;}
  172. // Note: Due to lack of synchronization, this function has a race condition
  173. // if called concurrently, which leads to undefined behavior. The same applies
  174. // to all other member functions of this class, but this one is mentioned
  175. // explicitly.
  176. void join()
  177. {
  178. using namespace std;
  179. if (get_id() == id(GetCurrentThreadId()))
  180. throw system_error(make_error_code(errc::resource_deadlock_would_occur));
  181. if (mHandle == kInvalidHandle)
  182. throw system_error(make_error_code(errc::no_such_process));
  183. if (!joinable())
  184. throw system_error(make_error_code(errc::invalid_argument));
  185. WaitForSingleObject(mHandle, kInfinite);
  186. CloseHandle(mHandle);
  187. mHandle = kInvalidHandle;
  188. mThreadId.clear();
  189. }
  190. ~thread()
  191. {
  192. if (joinable())
  193. {
  194. #ifndef NDEBUG
  195. std::printf("Error: Must join() or detach() a thread before \
  196. destroying it.\n");
  197. #endif
  198. std::terminate();
  199. }
  200. }
  201. thread& operator=(const thread&) = delete;
  202. thread& operator=(thread&& other) noexcept
  203. {
  204. if (joinable())
  205. {
  206. #ifndef NDEBUG
  207. std::printf("Error: Must join() or detach() a thread before \
  208. moving another thread to it.\n");
  209. #endif
  210. std::terminate();
  211. }
  212. swap(std::forward<thread>(other));
  213. return *this;
  214. }
  215. void swap(thread&& other) noexcept
  216. {
  217. std::swap(mHandle, other.mHandle);
  218. std::swap(mThreadId.mId, other.mThreadId.mId);
  219. }
  220. static unsigned int hardware_concurrency() noexcept
  221. {
  222. static unsigned int cached = _hardware_concurrency_helper();
  223. return cached;
  224. }
  225. void detach()
  226. {
  227. if (!joinable())
  228. {
  229. using namespace std;
  230. throw system_error(make_error_code(errc::invalid_argument));
  231. }
  232. if (mHandle != kInvalidHandle)
  233. {
  234. CloseHandle(mHandle);
  235. mHandle = kInvalidHandle;
  236. }
  237. mThreadId.clear();
  238. }
  239. };
  240. namespace this_thread
  241. {
  242. inline thread::id get_id() noexcept {return thread::id(GetCurrentThreadId());}
  243. inline void yield() noexcept {Sleep(0);}
  244. template< class Rep, class Period >
  245. void sleep_for( const std::chrono::duration<Rep,Period>& sleep_duration)
  246. {
  247. static constexpr DWORD kInfinite = 0xffffffffl;
  248. using namespace std::chrono;
  249. using rep = milliseconds::rep;
  250. rep ms = duration_cast<milliseconds>(sleep_duration).count();
  251. while (ms > 0)
  252. {
  253. constexpr rep kMaxRep = static_cast<rep>(kInfinite - 1);
  254. auto sleepTime = (ms < kMaxRep) ? ms : kMaxRep;
  255. Sleep(static_cast<DWORD>(sleepTime));
  256. ms -= sleepTime;
  257. }
  258. }
  259. template <class Clock, class Duration>
  260. void sleep_until(const std::chrono::time_point<Clock,Duration>& sleep_time)
  261. {
  262. sleep_for(sleep_time-Clock::now());
  263. }
  264. }
  265. } // Namespace mingw_stdthread
  266. namespace std
  267. {
  268. // Because of quirks of the compiler, the common "using namespace std;"
  269. // directive would flatten the namespaces and introduce ambiguity where there
  270. // was none. Direct specification (std::), however, would be unaffected.
  271. // Take the safe option, and include only in the presence of MinGW's win32
  272. // implementation.
  273. #if defined(__MINGW32__ ) && !defined(_GLIBCXX_HAS_GTHREADS)
  274. using mingw_stdthread::thread;
  275. // Remove ambiguity immediately, to avoid problems arising from the above.
  276. //using std::thread;
  277. namespace this_thread
  278. {
  279. using namespace mingw_stdthread::this_thread;
  280. }
  281. #elif !defined(MINGW_STDTHREAD_REDUNDANCY_WARNING) // Skip repetition
  282. #define MINGW_STDTHREAD_REDUNDANCY_WARNING
  283. #pragma message "This version of MinGW seems to include a win32 port of\
  284. pthreads, and probably already has C++11 std threading classes implemented,\
  285. based on pthreads. These classes, found in namespace std, are not overridden\
  286. by the mingw-std-thread library. If you would still like to use this\
  287. implementation (as it is more lightweight), use the classes provided in\
  288. namespace mingw_stdthread."
  289. #endif
  290. // Specialize hash for this implementation's thread::id, even if the
  291. // std::thread::id already has a hash.
  292. template<>
  293. struct hash<mingw_stdthread::thread::id>
  294. {
  295. typedef mingw_stdthread::thread::id argument_type;
  296. typedef size_t result_type;
  297. size_t operator() (const argument_type & i) const noexcept
  298. {
  299. return i.mId;
  300. }
  301. };
  302. }
  303. #endif // WIN32STDTHREAD_H