thread_primitives.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. #ifndef BOOST_WIN32_THREAD_PRIMITIVES_HPP
  2. #define BOOST_WIN32_THREAD_PRIMITIVES_HPP
  3. // win32_thread_primitives.hpp
  4. //
  5. // (C) Copyright 2005-7 Anthony Williams
  6. // (C) Copyright 2007 David Deakins
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See
  9. // accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #include <boost/thread/detail/config.hpp>
  12. #include <boost/predef/platform.h>
  13. #include <boost/throw_exception.hpp>
  14. #include <boost/assert.hpp>
  15. #include <boost/thread/exceptions.hpp>
  16. #include <boost/detail/interlocked.hpp>
  17. #include <boost/winapi/config.hpp>
  18. #include <boost/winapi/basic_types.hpp>
  19. #include <boost/winapi/semaphore.hpp>
  20. #include <boost/winapi/system.hpp>
  21. #include <boost/winapi/event.hpp>
  22. #include <boost/winapi/thread.hpp>
  23. #include <boost/winapi/get_current_thread.hpp>
  24. #include <boost/winapi/get_current_thread_id.hpp>
  25. #include <boost/winapi/get_current_process.hpp>
  26. #include <boost/winapi/get_current_process_id.hpp>
  27. #include <boost/winapi/wait.hpp>
  28. #include <boost/winapi/handles.hpp>
  29. #include <boost/winapi/access_rights.hpp>
  30. //#include <boost/winapi/synchronization.hpp>
  31. #include <boost/thread/win32/interlocked_read.hpp>
  32. #include <algorithm>
  33. #if BOOST_PLAT_WINDOWS_RUNTIME
  34. #include <thread>
  35. #endif
  36. namespace boost
  37. {
  38. namespace detail
  39. {
  40. namespace win32
  41. {
  42. typedef ::boost::winapi::HANDLE_ handle;
  43. typedef ::boost::winapi::SYSTEM_INFO_ system_info;
  44. typedef ::boost::winapi::ULONGLONG_ ticks_type;
  45. unsigned const infinite=::boost::winapi::INFINITE_;
  46. unsigned const timeout=::boost::winapi::WAIT_TIMEOUT_;
  47. handle const invalid_handle_value=::boost::winapi::INVALID_HANDLE_VALUE_;
  48. unsigned const event_modify_state=::boost::winapi::EVENT_MODIFY_STATE_;
  49. unsigned const synchronize=::boost::winapi::SYNCHRONIZE_;
  50. unsigned const wait_abandoned=::boost::winapi::WAIT_ABANDONED_;
  51. unsigned const create_event_initial_set = 0x00000002;
  52. unsigned const create_event_manual_reset = 0x00000001;
  53. unsigned const event_all_access = ::boost::winapi::EVENT_ALL_ACCESS_;
  54. unsigned const semaphore_all_access = boost::winapi::SEMAPHORE_ALL_ACCESS_;
  55. }
  56. }
  57. }
  58. #include <boost/config/abi_prefix.hpp>
  59. namespace boost
  60. {
  61. namespace detail
  62. {
  63. namespace win32
  64. {
  65. namespace detail { typedef ticks_type (BOOST_WINAPI_WINAPI_CC *gettickcount64_t)(); }
  66. extern BOOST_THREAD_DECL boost::detail::win32::detail::gettickcount64_t gettickcount64;
  67. enum event_type
  68. {
  69. auto_reset_event=false,
  70. manual_reset_event=true
  71. };
  72. enum initial_event_state
  73. {
  74. event_initially_reset=false,
  75. event_initially_set=true
  76. };
  77. inline handle create_event(
  78. #if !defined(BOOST_NO_ANSI_APIS)
  79. const char *mutex_name,
  80. #else
  81. const wchar_t *mutex_name,
  82. #endif
  83. event_type type,
  84. initial_event_state state)
  85. {
  86. #if !defined(BOOST_NO_ANSI_APIS)
  87. handle const res = ::boost::winapi::CreateEventA(0, type, state, mutex_name);
  88. #elif BOOST_USE_WINAPI_VERSION < BOOST_WINAPI_VERSION_VISTA
  89. handle const res = ::boost::winapi::CreateEventW(0, type, state, mutex_name);
  90. #else
  91. handle const res = ::boost::winapi::CreateEventExW(
  92. 0,
  93. mutex_name,
  94. (type ? create_event_manual_reset : 0) | (state ? create_event_initial_set : 0),
  95. event_all_access);
  96. #endif
  97. return res;
  98. }
  99. inline handle create_anonymous_event(event_type type,initial_event_state state)
  100. {
  101. handle const res = create_event(0, type, state);
  102. if(!res)
  103. {
  104. boost::throw_exception(thread_resource_error());
  105. }
  106. return res;
  107. }
  108. inline handle create_anonymous_semaphore_nothrow(long initial_count,long max_count)
  109. {
  110. #if !defined(BOOST_NO_ANSI_APIS)
  111. handle const res=::boost::winapi::CreateSemaphoreA(0,initial_count,max_count,0);
  112. #else
  113. #if BOOST_USE_WINAPI_VERSION < BOOST_WINAPI_VERSION_VISTA
  114. handle const res=::boost::winapi::CreateSemaphoreEx(0,initial_count,max_count,0,0);
  115. #else
  116. handle const res=::boost::winapi::CreateSemaphoreExW(0,initial_count,max_count,0,0,semaphore_all_access);
  117. #endif
  118. #endif
  119. return res;
  120. }
  121. inline handle create_anonymous_semaphore(long initial_count,long max_count)
  122. {
  123. handle const res=create_anonymous_semaphore_nothrow(initial_count,max_count);
  124. if(!res)
  125. {
  126. boost::throw_exception(thread_resource_error());
  127. }
  128. return res;
  129. }
  130. inline handle duplicate_handle(handle source)
  131. {
  132. handle const current_process=::boost::winapi::GetCurrentProcess();
  133. long const same_access_flag=2;
  134. handle new_handle=0;
  135. bool const success=::boost::winapi::DuplicateHandle(current_process,source,current_process,&new_handle,0,false,same_access_flag)!=0;
  136. if(!success)
  137. {
  138. boost::throw_exception(thread_resource_error());
  139. }
  140. return new_handle;
  141. }
  142. inline void release_semaphore(handle semaphore,long count)
  143. {
  144. BOOST_VERIFY(::boost::winapi::ReleaseSemaphore(semaphore,count,0)!=0);
  145. }
  146. inline void get_system_info(system_info *info)
  147. {
  148. #if BOOST_PLAT_WINDOWS_RUNTIME
  149. ::boost::winapi::GetNativeSystemInfo(info);
  150. #else
  151. ::boost::winapi::GetSystemInfo(info);
  152. #endif
  153. }
  154. inline void sleep(unsigned long milliseconds)
  155. {
  156. if(milliseconds == 0)
  157. {
  158. #if BOOST_PLAT_WINDOWS_RUNTIME
  159. std::this_thread::yield();
  160. #else
  161. ::boost::winapi::Sleep(0);
  162. #endif
  163. }
  164. else
  165. {
  166. #if BOOST_PLAT_WINDOWS_RUNTIME
  167. ::boost::winapi::WaitForSingleObjectEx(::boost::winapi::GetCurrentThread(), milliseconds, 0);
  168. #else
  169. ::boost::winapi::Sleep(milliseconds);
  170. #endif
  171. }
  172. }
  173. #if BOOST_PLAT_WINDOWS_RUNTIME
  174. class BOOST_THREAD_DECL scoped_winrt_thread
  175. {
  176. public:
  177. scoped_winrt_thread() : m_completionHandle(invalid_handle_value)
  178. {}
  179. ~scoped_winrt_thread()
  180. {
  181. if (m_completionHandle != ::boost::detail::win32::invalid_handle_value)
  182. {
  183. ::boost::winapi::CloseHandle(m_completionHandle);
  184. }
  185. }
  186. typedef unsigned(__stdcall * thread_func)(void *);
  187. bool start(thread_func address, void *parameter, unsigned int *thrdId);
  188. handle waitable_handle() const
  189. {
  190. BOOST_ASSERT(m_completionHandle != ::boost::detail::win32::invalid_handle_value);
  191. return m_completionHandle;
  192. }
  193. private:
  194. handle m_completionHandle;
  195. };
  196. #endif
  197. class BOOST_THREAD_DECL handle_manager
  198. {
  199. private:
  200. handle handle_to_manage;
  201. handle_manager(handle_manager&);
  202. handle_manager& operator=(handle_manager&);
  203. void cleanup()
  204. {
  205. if(handle_to_manage && handle_to_manage!=invalid_handle_value)
  206. {
  207. BOOST_VERIFY(::boost::winapi::CloseHandle(handle_to_manage));
  208. }
  209. }
  210. public:
  211. explicit handle_manager(handle handle_to_manage_):
  212. handle_to_manage(handle_to_manage_)
  213. {}
  214. handle_manager():
  215. handle_to_manage(0)
  216. {}
  217. handle_manager& operator=(handle new_handle)
  218. {
  219. cleanup();
  220. handle_to_manage=new_handle;
  221. return *this;
  222. }
  223. operator handle() const
  224. {
  225. return handle_to_manage;
  226. }
  227. handle duplicate() const
  228. {
  229. return duplicate_handle(handle_to_manage);
  230. }
  231. void swap(handle_manager& other)
  232. {
  233. std::swap(handle_to_manage,other.handle_to_manage);
  234. }
  235. handle release()
  236. {
  237. handle const res=handle_to_manage;
  238. handle_to_manage=0;
  239. return res;
  240. }
  241. bool operator!() const
  242. {
  243. return !handle_to_manage;
  244. }
  245. ~handle_manager()
  246. {
  247. cleanup();
  248. }
  249. };
  250. }
  251. }
  252. }
  253. #if defined(BOOST_MSVC) && (_MSC_VER>=1400) && !defined(UNDER_CE)
  254. namespace boost
  255. {
  256. namespace detail
  257. {
  258. namespace win32
  259. {
  260. #if _MSC_VER==1400
  261. extern "C" unsigned char _interlockedbittestandset(long *a,long b);
  262. extern "C" unsigned char _interlockedbittestandreset(long *a,long b);
  263. #else
  264. extern "C" unsigned char _interlockedbittestandset(volatile long *a,long b);
  265. extern "C" unsigned char _interlockedbittestandreset(volatile long *a,long b);
  266. #endif
  267. #pragma intrinsic(_interlockedbittestandset)
  268. #pragma intrinsic(_interlockedbittestandreset)
  269. inline bool interlocked_bit_test_and_set(long* x,long bit)
  270. {
  271. return _interlockedbittestandset(x,bit)!=0;
  272. }
  273. inline bool interlocked_bit_test_and_reset(long* x,long bit)
  274. {
  275. return _interlockedbittestandreset(x,bit)!=0;
  276. }
  277. }
  278. }
  279. }
  280. #define BOOST_THREAD_BTS_DEFINED
  281. #elif (defined(BOOST_MSVC) || defined(BOOST_INTEL_WIN)) && defined(_M_IX86)
  282. namespace boost
  283. {
  284. namespace detail
  285. {
  286. namespace win32
  287. {
  288. inline bool interlocked_bit_test_and_set(long* x,long bit)
  289. {
  290. #ifndef BOOST_INTEL_CXX_VERSION
  291. __asm {
  292. mov eax,bit;
  293. mov edx,x;
  294. lock bts [edx],eax;
  295. setc al;
  296. };
  297. #else
  298. bool ret;
  299. __asm {
  300. mov eax,bit
  301. mov edx,x
  302. lock bts [edx],eax
  303. setc al
  304. mov ret, al
  305. };
  306. return ret;
  307. #endif
  308. }
  309. inline bool interlocked_bit_test_and_reset(long* x,long bit)
  310. {
  311. #ifndef BOOST_INTEL_CXX_VERSION
  312. __asm {
  313. mov eax,bit;
  314. mov edx,x;
  315. lock btr [edx],eax;
  316. setc al;
  317. };
  318. #else
  319. bool ret;
  320. __asm {
  321. mov eax,bit
  322. mov edx,x
  323. lock btr [edx],eax
  324. setc al
  325. mov ret, al
  326. };
  327. return ret;
  328. #endif
  329. }
  330. }
  331. }
  332. }
  333. #define BOOST_THREAD_BTS_DEFINED
  334. #endif
  335. #ifndef BOOST_THREAD_BTS_DEFINED
  336. namespace boost
  337. {
  338. namespace detail
  339. {
  340. namespace win32
  341. {
  342. inline bool interlocked_bit_test_and_set(long* x,long bit)
  343. {
  344. long const value=1<<bit;
  345. long old=*x;
  346. do
  347. {
  348. long const current=BOOST_INTERLOCKED_COMPARE_EXCHANGE(x,old|value,old);
  349. if(current==old)
  350. {
  351. break;
  352. }
  353. old=current;
  354. }
  355. while(true) ;
  356. return (old&value)!=0;
  357. }
  358. inline bool interlocked_bit_test_and_reset(long* x,long bit)
  359. {
  360. long const value=1<<bit;
  361. long old=*x;
  362. do
  363. {
  364. long const current=BOOST_INTERLOCKED_COMPARE_EXCHANGE(x,old&~value,old);
  365. if(current==old)
  366. {
  367. break;
  368. }
  369. old=current;
  370. }
  371. while(true) ;
  372. return (old&value)!=0;
  373. }
  374. }
  375. }
  376. }
  377. #endif
  378. #include <boost/config/abi_suffix.hpp>
  379. #endif