once.hpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. #ifndef BOOST_THREAD_WIN32_ONCE_HPP
  2. #define BOOST_THREAD_WIN32_ONCE_HPP
  3. // once.hpp
  4. //
  5. // (C) Copyright 2005-7 Anthony Williams
  6. // (C) Copyright 2005 John Maddock
  7. // (C) Copyright 2011-2013 Vicente J. Botet Escriba
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #include <cstring>
  13. #include <cstddef>
  14. #include <boost/assert.hpp>
  15. #include <boost/static_assert.hpp>
  16. #include <boost/detail/interlocked.hpp>
  17. #include <boost/thread/win32/thread_primitives.hpp>
  18. #include <boost/thread/win32/interlocked_read.hpp>
  19. #include <boost/core/no_exceptions_support.hpp>
  20. #include <boost/thread/detail/move.hpp>
  21. #include <boost/thread/detail/invoke.hpp>
  22. #include <boost/bind.hpp>
  23. #include <boost/config/abi_prefix.hpp>
  24. #ifdef BOOST_NO_STDC_NAMESPACE
  25. namespace std
  26. {
  27. using ::memcpy;
  28. using ::ptrdiff_t;
  29. }
  30. #endif
  31. namespace boost
  32. {
  33. struct once_flag;
  34. namespace detail
  35. {
  36. struct once_context;
  37. inline bool enter_once_region(once_flag& flag, once_context& ctx) BOOST_NOEXCEPT;
  38. inline void commit_once_region(once_flag& flag, once_context& ctx) BOOST_NOEXCEPT;
  39. inline void rollback_once_region(once_flag& flag, once_context& ctx) BOOST_NOEXCEPT;
  40. }
  41. #ifdef BOOST_THREAD_PROVIDES_ONCE_CXX11
  42. struct once_flag
  43. {
  44. BOOST_THREAD_NO_COPYABLE(once_flag)
  45. BOOST_CONSTEXPR once_flag() BOOST_NOEXCEPT
  46. : status(0), count(0)
  47. {}
  48. long status;
  49. long count;
  50. private:
  51. friend inline bool enter_once_region(once_flag& flag, detail::once_context& ctx) BOOST_NOEXCEPT;
  52. friend inline void commit_once_region(once_flag& flag, detail::once_context& ctx) BOOST_NOEXCEPT;
  53. friend inline void rollback_once_region(once_flag& flag, detail::once_context& ctx) BOOST_NOEXCEPT;
  54. };
  55. #define BOOST_ONCE_INIT once_flag()
  56. #else // BOOST_THREAD_PROVIDES_ONCE_CXX11
  57. struct once_flag
  58. {
  59. long status;
  60. long count;
  61. };
  62. #define BOOST_ONCE_INIT {0,0}
  63. #endif // BOOST_THREAD_PROVIDES_ONCE_CXX11
  64. #if defined BOOST_THREAD_PROVIDES_INVOKE
  65. #define BOOST_THREAD_INVOKE_RET_VOID detail::invoke
  66. #define BOOST_THREAD_INVOKE_RET_VOID_CALL
  67. #elif defined BOOST_THREAD_PROVIDES_INVOKE_RET
  68. #define BOOST_THREAD_INVOKE_RET_VOID detail::invoke<void>
  69. #define BOOST_THREAD_INVOKE_RET_VOID_CALL
  70. #else
  71. #define BOOST_THREAD_INVOKE_RET_VOID boost::bind
  72. #define BOOST_THREAD_INVOKE_RET_VOID_CALL ()
  73. #endif
  74. namespace detail
  75. {
  76. #ifdef BOOST_NO_ANSI_APIS
  77. typedef wchar_t once_char_type;
  78. #else
  79. typedef char once_char_type;
  80. #endif
  81. unsigned const once_mutex_name_fixed_length=54;
  82. unsigned const once_mutex_name_length=once_mutex_name_fixed_length+
  83. sizeof(void*)*2+sizeof(unsigned long)*2+1;
  84. template <class I>
  85. void int_to_string(I p, once_char_type* buf)
  86. {
  87. for(unsigned i=0; i < sizeof(I)*2; ++i,++buf)
  88. {
  89. #ifdef BOOST_NO_ANSI_APIS
  90. once_char_type const a=L'A';
  91. #else
  92. once_char_type const a='A';
  93. #endif
  94. *buf = a + static_cast<once_char_type>((p >> (i*4)) & 0x0f);
  95. }
  96. *buf = 0;
  97. }
  98. inline void name_once_mutex(once_char_type* mutex_name,void* flag_address)
  99. {
  100. #ifdef BOOST_NO_ANSI_APIS
  101. static const once_char_type fixed_mutex_name[]=L"Local\\{C15730E2-145C-4c5e-B005-3BC753F42475}-once-flag";
  102. #else
  103. static const once_char_type fixed_mutex_name[]="Local\\{C15730E2-145C-4c5e-B005-3BC753F42475}-once-flag";
  104. #endif
  105. BOOST_STATIC_ASSERT(sizeof(fixed_mutex_name) ==
  106. (sizeof(once_char_type)*(once_mutex_name_fixed_length+1)));
  107. std::memcpy(mutex_name,fixed_mutex_name,sizeof(fixed_mutex_name));
  108. detail::int_to_string(reinterpret_cast<std::ptrdiff_t>(flag_address),
  109. mutex_name + once_mutex_name_fixed_length);
  110. detail::int_to_string(winapi::GetCurrentProcessId(),
  111. mutex_name + once_mutex_name_fixed_length + sizeof(void*)*2);
  112. }
  113. inline void* open_once_event(once_char_type* mutex_name,void* flag_address)
  114. {
  115. if(!*mutex_name)
  116. {
  117. name_once_mutex(mutex_name,flag_address);
  118. }
  119. #ifdef BOOST_NO_ANSI_APIS
  120. return ::boost::winapi::OpenEventW(
  121. #else
  122. return ::boost::winapi::OpenEventA(
  123. #endif
  124. ::boost::detail::win32::synchronize |
  125. ::boost::detail::win32::event_modify_state,
  126. false,
  127. mutex_name);
  128. }
  129. inline void* create_once_event(once_char_type* mutex_name,void* flag_address)
  130. {
  131. if(!*mutex_name)
  132. {
  133. name_once_mutex(mutex_name,flag_address);
  134. }
  135. return ::boost::detail::win32::create_event(
  136. mutex_name,
  137. ::boost::detail::win32::manual_reset_event,
  138. ::boost::detail::win32::event_initially_reset);
  139. }
  140. struct once_context {
  141. long const function_complete_flag_value;
  142. long const running_value;
  143. bool counted;
  144. detail::win32::handle_manager event_handle;
  145. detail::once_char_type mutex_name[once_mutex_name_length];
  146. once_context() :
  147. function_complete_flag_value(0xc15730e2),
  148. running_value(0x7f0725e3),
  149. counted(false)
  150. {
  151. mutex_name[0]=0;
  152. }
  153. };
  154. enum once_action {try_, break_, continue_};
  155. inline bool enter_once_region(once_flag& flag, once_context& ctx) BOOST_NOEXCEPT
  156. {
  157. long status=BOOST_INTERLOCKED_COMPARE_EXCHANGE(&flag.status,ctx.running_value,0);
  158. if(!status)
  159. {
  160. if(!ctx.event_handle)
  161. {
  162. ctx.event_handle=detail::open_once_event(ctx.mutex_name,&flag);
  163. }
  164. if(ctx.event_handle)
  165. {
  166. ::boost::winapi::ResetEvent(ctx.event_handle);
  167. }
  168. return true;
  169. }
  170. return false;
  171. }
  172. inline void commit_once_region(once_flag& flag, once_context& ctx) BOOST_NOEXCEPT
  173. {
  174. if(!ctx.counted)
  175. {
  176. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  177. ctx.counted=true;
  178. }
  179. BOOST_INTERLOCKED_EXCHANGE(&flag.status,ctx.function_complete_flag_value);
  180. if(!ctx.event_handle &&
  181. (::boost::detail::interlocked_read_acquire(&flag.count)>1))
  182. {
  183. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  184. }
  185. if(ctx.event_handle)
  186. {
  187. ::boost::winapi::SetEvent(ctx.event_handle);
  188. }
  189. }
  190. inline void rollback_once_region(once_flag& flag, once_context& ctx) BOOST_NOEXCEPT
  191. {
  192. BOOST_INTERLOCKED_EXCHANGE(&flag.status,0);
  193. if(!ctx.event_handle)
  194. {
  195. ctx.event_handle=detail::open_once_event(ctx.mutex_name,&flag);
  196. }
  197. if(ctx.event_handle)
  198. {
  199. ::boost::winapi::SetEvent(ctx.event_handle);
  200. }
  201. }
  202. }
  203. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  204. //#if defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNCTION_PTR)
  205. inline void call_once(once_flag& flag, void (*f)())
  206. {
  207. // Try for a quick win: if the procedure has already been called
  208. // just skip through:
  209. detail::once_context ctx;
  210. while(::boost::detail::interlocked_read_acquire(&flag.status)
  211. !=ctx.function_complete_flag_value)
  212. {
  213. if(detail::enter_once_region(flag, ctx))
  214. {
  215. BOOST_TRY
  216. {
  217. f();
  218. }
  219. BOOST_CATCH(...)
  220. {
  221. detail::rollback_once_region(flag, ctx);
  222. BOOST_RETHROW
  223. }
  224. BOOST_CATCH_END
  225. detail::commit_once_region(flag, ctx);
  226. break;
  227. }
  228. if(!ctx.counted)
  229. {
  230. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  231. ctx.counted=true;
  232. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  233. if(status==ctx.function_complete_flag_value)
  234. {
  235. break;
  236. }
  237. if(!ctx.event_handle)
  238. {
  239. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  240. continue;
  241. }
  242. }
  243. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  244. ctx.event_handle,::boost::detail::win32::infinite, 0));
  245. }
  246. }
  247. //#endif
  248. template<typename Function>
  249. inline void call_once(once_flag& flag, BOOST_THREAD_RV_REF(Function) f)
  250. {
  251. // Try for a quick win: if the procedure has already been called
  252. // just skip through:
  253. detail::once_context ctx;
  254. while(::boost::detail::interlocked_read_acquire(&flag.status)
  255. !=ctx.function_complete_flag_value)
  256. {
  257. if(detail::enter_once_region(flag, ctx))
  258. {
  259. BOOST_TRY
  260. {
  261. f();
  262. }
  263. BOOST_CATCH(...)
  264. {
  265. detail::rollback_once_region(flag, ctx);
  266. BOOST_RETHROW
  267. }
  268. BOOST_CATCH_END
  269. detail::commit_once_region(flag, ctx);
  270. break;
  271. }
  272. if(!ctx.counted)
  273. {
  274. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  275. ctx.counted=true;
  276. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  277. if(status==ctx.function_complete_flag_value)
  278. {
  279. break;
  280. }
  281. if(!ctx.event_handle)
  282. {
  283. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  284. continue;
  285. }
  286. }
  287. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  288. ctx.event_handle,::boost::detail::win32::infinite,0));
  289. }
  290. }
  291. template<typename Function, class A, class ...ArgTypes>
  292. inline void call_once(once_flag& flag, BOOST_THREAD_RV_REF(Function) f, BOOST_THREAD_RV_REF(A) a, BOOST_THREAD_RV_REF(ArgTypes)... args)
  293. {
  294. // Try for a quick win: if the procedure has already been called
  295. // just skip through:
  296. detail::once_context ctx;
  297. while(::boost::detail::interlocked_read_acquire(&flag.status)
  298. !=ctx.function_complete_flag_value)
  299. {
  300. if(detail::enter_once_region(flag, ctx))
  301. {
  302. BOOST_TRY
  303. {
  304. BOOST_THREAD_INVOKE_RET_VOID(
  305. thread_detail::decay_copy(boost::forward<Function>(f)),
  306. thread_detail::decay_copy(boost::forward<A>(a)),
  307. thread_detail::decay_copy(boost::forward<ArgTypes>(args))...
  308. ) BOOST_THREAD_INVOKE_RET_VOID_CALL;
  309. }
  310. BOOST_CATCH(...)
  311. {
  312. detail::rollback_once_region(flag, ctx);
  313. BOOST_RETHROW
  314. }
  315. BOOST_CATCH_END
  316. detail::commit_once_region(flag, ctx);
  317. break;
  318. }
  319. if(!ctx.counted)
  320. {
  321. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  322. ctx.counted=true;
  323. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  324. if(status==ctx.function_complete_flag_value)
  325. {
  326. break;
  327. }
  328. if(!ctx.event_handle)
  329. {
  330. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  331. continue;
  332. }
  333. }
  334. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  335. ctx.event_handle,::boost::detail::win32::infinite,0));
  336. }
  337. }
  338. #else
  339. #if ! defined(BOOST_MSVC) && ! defined(BOOST_INTEL)
  340. template<typename Function>
  341. void call_once(once_flag& flag,Function f)
  342. {
  343. // Try for a quick win: if the procedure has already been called
  344. // just skip through:
  345. detail::once_context ctx;
  346. while(::boost::detail::interlocked_read_acquire(&flag.status)
  347. !=ctx.function_complete_flag_value)
  348. {
  349. if(detail::enter_once_region(flag, ctx))
  350. {
  351. BOOST_TRY
  352. {
  353. f();
  354. }
  355. BOOST_CATCH(...)
  356. {
  357. detail::rollback_once_region(flag, ctx);
  358. BOOST_RETHROW
  359. }
  360. BOOST_CATCH_END
  361. detail::commit_once_region(flag, ctx);
  362. break;
  363. }
  364. if(!ctx.counted)
  365. {
  366. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  367. ctx.counted=true;
  368. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  369. if(status==ctx.function_complete_flag_value)
  370. {
  371. break;
  372. }
  373. if(!ctx.event_handle)
  374. {
  375. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  376. continue;
  377. }
  378. }
  379. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  380. ctx.event_handle,::boost::detail::win32::infinite,0));
  381. }
  382. }
  383. template<typename Function, typename T1>
  384. void call_once(once_flag& flag,Function f, T1 p1)
  385. {
  386. // Try for a quick win: if the procedure has already been called
  387. // just skip through:
  388. detail::once_context ctx;
  389. while(::boost::detail::interlocked_read_acquire(&flag.status)
  390. !=ctx.function_complete_flag_value)
  391. {
  392. if(detail::enter_once_region(flag, ctx))
  393. {
  394. BOOST_TRY
  395. {
  396. BOOST_THREAD_INVOKE_RET_VOID(f,p1) BOOST_THREAD_INVOKE_RET_VOID_CALL;
  397. }
  398. BOOST_CATCH(...)
  399. {
  400. detail::rollback_once_region(flag, ctx);
  401. BOOST_RETHROW
  402. }
  403. BOOST_CATCH_END
  404. detail::commit_once_region(flag, ctx);
  405. break;
  406. }
  407. if(!ctx.counted)
  408. {
  409. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  410. ctx.counted=true;
  411. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  412. if(status==ctx.function_complete_flag_value)
  413. {
  414. break;
  415. }
  416. if(!ctx.event_handle)
  417. {
  418. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  419. continue;
  420. }
  421. }
  422. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  423. ctx.event_handle,::boost::detail::win32::infinite,0));
  424. }
  425. }
  426. template<typename Function, typename T1, typename T2>
  427. void call_once(once_flag& flag,Function f, T1 p1, T2 p2)
  428. {
  429. // Try for a quick win: if the procedure has already been called
  430. // just skip through:
  431. detail::once_context ctx;
  432. while(::boost::detail::interlocked_read_acquire(&flag.status)
  433. !=ctx.function_complete_flag_value)
  434. {
  435. if(detail::enter_once_region(flag, ctx))
  436. {
  437. BOOST_TRY
  438. {
  439. BOOST_THREAD_INVOKE_RET_VOID(f,p1,p2) BOOST_THREAD_INVOKE_RET_VOID_CALL;
  440. }
  441. BOOST_CATCH(...)
  442. {
  443. detail::rollback_once_region(flag, ctx);
  444. BOOST_RETHROW
  445. }
  446. BOOST_CATCH_END
  447. detail::commit_once_region(flag, ctx);
  448. break;
  449. }
  450. if(!ctx.counted)
  451. {
  452. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  453. ctx.counted=true;
  454. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  455. if(status==ctx.function_complete_flag_value)
  456. {
  457. break;
  458. }
  459. if(!ctx.event_handle)
  460. {
  461. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  462. continue;
  463. }
  464. }
  465. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  466. ctx.event_handle,::boost::detail::win32::infinite,0));
  467. }
  468. }
  469. template<typename Function, typename T1, typename T2, typename T3>
  470. void call_once(once_flag& flag,Function f, T1 p1, T2 p2, T3 p3)
  471. {
  472. // Try for a quick win: if the procedure has already been called
  473. // just skip through:
  474. detail::once_context ctx;
  475. while(::boost::detail::interlocked_read_acquire(&flag.status)
  476. !=ctx.function_complete_flag_value)
  477. {
  478. if(detail::enter_once_region(flag, ctx))
  479. {
  480. BOOST_TRY
  481. {
  482. BOOST_THREAD_INVOKE_RET_VOID(f,p1,p2,p3) BOOST_THREAD_INVOKE_RET_VOID_CALL;
  483. }
  484. BOOST_CATCH(...)
  485. {
  486. detail::rollback_once_region(flag, ctx);
  487. BOOST_RETHROW
  488. }
  489. BOOST_CATCH_END
  490. detail::commit_once_region(flag, ctx);
  491. break;
  492. }
  493. if(!ctx.counted)
  494. {
  495. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  496. ctx.counted=true;
  497. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  498. if(status==ctx.function_complete_flag_value)
  499. {
  500. break;
  501. }
  502. if(!ctx.event_handle)
  503. {
  504. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  505. continue;
  506. }
  507. }
  508. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  509. ctx.event_handle,::boost::detail::win32::infinite,0));
  510. }
  511. }
  512. #elif defined BOOST_NO_CXX11_RVALUE_REFERENCES
  513. template<typename Function>
  514. void call_once(once_flag& flag,Function const&f)
  515. {
  516. // Try for a quick win: if the procedure has already been called
  517. // just skip through:
  518. detail::once_context ctx;
  519. while(::boost::detail::interlocked_read_acquire(&flag.status)
  520. !=ctx.function_complete_flag_value)
  521. {
  522. if(detail::enter_once_region(flag, ctx))
  523. {
  524. BOOST_TRY
  525. {
  526. f();
  527. }
  528. BOOST_CATCH(...)
  529. {
  530. detail::rollback_once_region(flag, ctx);
  531. BOOST_RETHROW
  532. }
  533. BOOST_CATCH_END
  534. detail::commit_once_region(flag, ctx);
  535. break;
  536. }
  537. if(!ctx.counted)
  538. {
  539. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  540. ctx.counted=true;
  541. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  542. if(status==ctx.function_complete_flag_value)
  543. {
  544. break;
  545. }
  546. if(!ctx.event_handle)
  547. {
  548. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  549. continue;
  550. }
  551. }
  552. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  553. ctx.event_handle,::boost::detail::win32::infinite,0));
  554. }
  555. }
  556. template<typename Function, typename T1>
  557. void call_once(once_flag& flag,Function const&f, T1 const&p1)
  558. {
  559. // Try for a quick win: if the procedure has already been called
  560. // just skip through:
  561. detail::once_context ctx;
  562. while(::boost::detail::interlocked_read_acquire(&flag.status)
  563. !=ctx.function_complete_flag_value)
  564. {
  565. if(detail::enter_once_region(flag, ctx))
  566. {
  567. BOOST_TRY
  568. {
  569. BOOST_THREAD_INVOKE_RET_VOID(f,p1) BOOST_THREAD_INVOKE_RET_VOID_CALL;
  570. }
  571. BOOST_CATCH(...)
  572. {
  573. detail::rollback_once_region(flag, ctx);
  574. BOOST_RETHROW
  575. }
  576. BOOST_CATCH_END
  577. detail::commit_once_region(flag, ctx);
  578. break;
  579. }
  580. if(!ctx.counted)
  581. {
  582. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  583. ctx.counted=true;
  584. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  585. if(status==ctx.function_complete_flag_value)
  586. {
  587. break;
  588. }
  589. if(!ctx.event_handle)
  590. {
  591. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  592. continue;
  593. }
  594. }
  595. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  596. ctx.event_handle,::boost::detail::win32::infinite,0));
  597. }
  598. }
  599. template<typename Function, typename T1, typename T2>
  600. void call_once(once_flag& flag,Function const&f, T1 const&p1, T2 const&p2)
  601. {
  602. // Try for a quick win: if the procedure has already been called
  603. // just skip through:
  604. detail::once_context ctx;
  605. while(::boost::detail::interlocked_read_acquire(&flag.status)
  606. !=ctx.function_complete_flag_value)
  607. {
  608. if(detail::enter_once_region(flag, ctx))
  609. {
  610. BOOST_TRY
  611. {
  612. BOOST_THREAD_INVOKE_RET_VOID(f,p1,p2) BOOST_THREAD_INVOKE_RET_VOID_CALL;
  613. }
  614. BOOST_CATCH(...)
  615. {
  616. detail::rollback_once_region(flag, ctx);
  617. BOOST_RETHROW
  618. }
  619. BOOST_CATCH_END
  620. detail::commit_once_region(flag, ctx);
  621. break;
  622. }
  623. if(!ctx.counted)
  624. {
  625. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  626. ctx.counted=true;
  627. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  628. if(status==ctx.function_complete_flag_value)
  629. {
  630. break;
  631. }
  632. if(!ctx.event_handle)
  633. {
  634. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  635. continue;
  636. }
  637. }
  638. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  639. ctx.event_handle,::boost::detail::win32::infinite,0));
  640. }
  641. }
  642. template<typename Function, typename T1, typename T2, typename T3>
  643. void call_once(once_flag& flag,Function const&f, T1 const&p1, T2 const&p2, T3 const&p3)
  644. {
  645. // Try for a quick win: if the procedure has already been called
  646. // just skip through:
  647. detail::once_context ctx;
  648. while(::boost::detail::interlocked_read_acquire(&flag.status)
  649. !=ctx.function_complete_flag_value)
  650. {
  651. if(detail::enter_once_region(flag, ctx))
  652. {
  653. BOOST_TRY
  654. {
  655. BOOST_THREAD_INVOKE_RET_VOID(f,p1,p2,p3) BOOST_THREAD_INVOKE_RET_VOID_CALL;
  656. }
  657. BOOST_CATCH(...)
  658. {
  659. detail::rollback_once_region(flag, ctx);
  660. BOOST_RETHROW
  661. }
  662. BOOST_CATCH_END
  663. detail::commit_once_region(flag, ctx);
  664. break;
  665. }
  666. if(!ctx.counted)
  667. {
  668. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  669. ctx.counted=true;
  670. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  671. if(status==ctx.function_complete_flag_value)
  672. {
  673. break;
  674. }
  675. if(!ctx.event_handle)
  676. {
  677. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  678. continue;
  679. }
  680. }
  681. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  682. ctx.event_handle,::boost::detail::win32::infinite,0));
  683. }
  684. }
  685. #endif
  686. #if 1
  687. #if defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNCTION_PTR)
  688. inline void call_once(once_flag& flag, void (*f)())
  689. {
  690. // Try for a quick win: if the procedure has already been called
  691. // just skip through:
  692. detail::once_context ctx;
  693. while(::boost::detail::interlocked_read_acquire(&flag.status)
  694. !=ctx.function_complete_flag_value)
  695. {
  696. if(detail::enter_once_region(flag, ctx))
  697. {
  698. BOOST_TRY
  699. {
  700. f();
  701. }
  702. BOOST_CATCH(...)
  703. {
  704. detail::rollback_once_region(flag, ctx);
  705. BOOST_RETHROW
  706. }
  707. BOOST_CATCH_END
  708. detail::commit_once_region(flag, ctx);
  709. break;
  710. }
  711. if(!ctx.counted)
  712. {
  713. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  714. ctx.counted=true;
  715. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  716. if(status==ctx.function_complete_flag_value)
  717. {
  718. break;
  719. }
  720. if(!ctx.event_handle)
  721. {
  722. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  723. continue;
  724. }
  725. }
  726. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  727. ctx.event_handle,::boost::detail::win32::infinite,0));
  728. }
  729. }
  730. template<typename T1>
  731. void call_once(once_flag& flag,void (*f)(BOOST_THREAD_RV_REF(T1)), BOOST_THREAD_RV_REF(T1) p1)
  732. {
  733. // Try for a quick win: if the procedure has already been called
  734. // just skip through:
  735. detail::once_context ctx;
  736. while(::boost::detail::interlocked_read_acquire(&flag.status)
  737. !=ctx.function_complete_flag_value)
  738. {
  739. if(detail::enter_once_region(flag, ctx))
  740. {
  741. BOOST_TRY
  742. {
  743. f(
  744. thread_detail::decay_copy(boost::forward<T1>(p1))
  745. );
  746. }
  747. BOOST_CATCH(...)
  748. {
  749. detail::rollback_once_region(flag, ctx);
  750. BOOST_RETHROW
  751. }
  752. BOOST_CATCH_END
  753. detail::commit_once_region(flag, ctx);
  754. break;
  755. }
  756. if(!ctx.counted)
  757. {
  758. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  759. ctx.counted=true;
  760. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  761. if(status==ctx.function_complete_flag_value)
  762. {
  763. break;
  764. }
  765. if(!ctx.event_handle)
  766. {
  767. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  768. continue;
  769. }
  770. }
  771. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  772. ctx.event_handle,::boost::detail::win32::infinite,0));
  773. }
  774. }
  775. template<typename Function, typename T1, typename T2>
  776. void call_once(once_flag& flag,void (*f)(BOOST_THREAD_RV_REF(T1),BOOST_THREAD_RV_REF(T2)), BOOST_THREAD_RV_REF(T1) p1, BOOST_THREAD_RV_REF(T2) p2)
  777. {
  778. // Try for a quick win: if the procedure has already been called
  779. // just skip through:
  780. detail::once_context ctx;
  781. while(::boost::detail::interlocked_read_acquire(&flag.status)
  782. !=ctx.function_complete_flag_value)
  783. {
  784. if(detail::enter_once_region(flag, ctx))
  785. {
  786. BOOST_TRY
  787. {
  788. f(
  789. thread_detail::decay_copy(boost::forward<T1>(p1)),
  790. thread_detail::decay_copy(boost::forward<T2>(p2))
  791. );
  792. }
  793. BOOST_CATCH(...)
  794. {
  795. detail::rollback_once_region(flag, ctx);
  796. BOOST_RETHROW
  797. }
  798. BOOST_CATCH_END
  799. detail::commit_once_region(flag, ctx);
  800. break;
  801. }
  802. if(!ctx.counted)
  803. {
  804. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  805. ctx.counted=true;
  806. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  807. if(status==ctx.function_complete_flag_value)
  808. {
  809. break;
  810. }
  811. if(!ctx.event_handle)
  812. {
  813. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  814. continue;
  815. }
  816. }
  817. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  818. ctx.event_handle,::boost::detail::win32::infinite,0));
  819. }
  820. }
  821. template<typename Function, typename T1, typename T2, typename T3>
  822. void call_once(once_flag& flag,void (*f)(BOOST_THREAD_RV_REF(T1),BOOST_THREAD_RV_REF(T2)), BOOST_THREAD_RV_REF(T1) p1, BOOST_THREAD_RV_REF(T2) p2, BOOST_THREAD_RV_REF(T3) p3)
  823. {
  824. // Try for a quick win: if the procedure has already been called
  825. // just skip through:
  826. detail::once_context ctx;
  827. while(::boost::detail::interlocked_read_acquire(&flag.status)
  828. !=ctx.function_complete_flag_value)
  829. {
  830. if(detail::enter_once_region(flag, ctx))
  831. {
  832. BOOST_TRY
  833. {
  834. f(
  835. thread_detail::decay_copy(boost::forward<T1>(p1)),
  836. thread_detail::decay_copy(boost::forward<T2>(p2)),
  837. thread_detail::decay_copy(boost::forward<T3>(p3))
  838. );
  839. }
  840. BOOST_CATCH(...)
  841. {
  842. detail::rollback_once_region(flag, ctx);
  843. BOOST_RETHROW
  844. }
  845. BOOST_CATCH_END
  846. detail::commit_once_region(flag, ctx);
  847. break;
  848. }
  849. if(!ctx.counted)
  850. {
  851. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  852. ctx.counted=true;
  853. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  854. if(status==ctx.function_complete_flag_value)
  855. {
  856. break;
  857. }
  858. if(!ctx.event_handle)
  859. {
  860. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  861. continue;
  862. }
  863. }
  864. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  865. ctx.event_handle,::boost::detail::win32::infinite,0));
  866. }
  867. }
  868. #endif
  869. template<typename Function>
  870. void call_once(once_flag& flag,BOOST_THREAD_RV_REF(Function) f)
  871. {
  872. // Try for a quick win: if the procedure has already been called
  873. // just skip through:
  874. detail::once_context ctx;
  875. while(::boost::detail::interlocked_read_acquire(&flag.status)
  876. !=ctx.function_complete_flag_value)
  877. {
  878. if(detail::enter_once_region(flag, ctx))
  879. {
  880. BOOST_TRY
  881. {
  882. f();
  883. }
  884. BOOST_CATCH(...)
  885. {
  886. detail::rollback_once_region(flag, ctx);
  887. BOOST_RETHROW
  888. }
  889. BOOST_CATCH_END
  890. detail::commit_once_region(flag, ctx);
  891. break;
  892. }
  893. if(!ctx.counted)
  894. {
  895. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  896. ctx.counted=true;
  897. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  898. if(status==ctx.function_complete_flag_value)
  899. {
  900. break;
  901. }
  902. if(!ctx.event_handle)
  903. {
  904. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  905. continue;
  906. }
  907. }
  908. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  909. ctx.event_handle,::boost::detail::win32::infinite,0));
  910. }
  911. }
  912. template<typename Function, typename T1>
  913. void call_once(once_flag& flag,BOOST_THREAD_RV_REF(Function) f, BOOST_THREAD_RV_REF(T1) p1)
  914. {
  915. // Try for a quick win: if the procedure has already been called
  916. // just skip through:
  917. detail::once_context ctx;
  918. while(::boost::detail::interlocked_read_acquire(&flag.status)
  919. !=ctx.function_complete_flag_value)
  920. {
  921. if(detail::enter_once_region(flag, ctx))
  922. {
  923. BOOST_TRY
  924. {
  925. BOOST_THREAD_INVOKE_RET_VOID(
  926. thread_detail::decay_copy(boost::forward<Function>(f)),
  927. thread_detail::decay_copy(boost::forward<T1>(p1))
  928. ) BOOST_THREAD_INVOKE_RET_VOID_CALL;
  929. }
  930. BOOST_CATCH(...)
  931. {
  932. detail::rollback_once_region(flag, ctx);
  933. BOOST_RETHROW
  934. }
  935. BOOST_CATCH_END
  936. detail::commit_once_region(flag, ctx);
  937. break;
  938. }
  939. if(!ctx.counted)
  940. {
  941. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  942. ctx.counted=true;
  943. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  944. if(status==ctx.function_complete_flag_value)
  945. {
  946. break;
  947. }
  948. if(!ctx.event_handle)
  949. {
  950. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  951. continue;
  952. }
  953. }
  954. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  955. ctx.event_handle,::boost::detail::win32::infinite,0));
  956. }
  957. }
  958. template<typename Function, typename T1, typename T2>
  959. void call_once(once_flag& flag,BOOST_THREAD_RV_REF(Function) f, BOOST_THREAD_RV_REF(T1) p1, BOOST_THREAD_RV_REF(T2) p2)
  960. {
  961. // Try for a quick win: if the procedure has already been called
  962. // just skip through:
  963. detail::once_context ctx;
  964. while(::boost::detail::interlocked_read_acquire(&flag.status)
  965. !=ctx.function_complete_flag_value)
  966. {
  967. if(detail::enter_once_region(flag, ctx))
  968. {
  969. BOOST_TRY
  970. {
  971. BOOST_THREAD_INVOKE_RET_VOID(
  972. thread_detail::decay_copy(boost::forward<Function>(f)),
  973. thread_detail::decay_copy(boost::forward<T1>(p1)),
  974. thread_detail::decay_copy(boost::forward<T2>(p2))
  975. ) BOOST_THREAD_INVOKE_RET_VOID_CALL;
  976. }
  977. BOOST_CATCH(...)
  978. {
  979. detail::rollback_once_region(flag, ctx);
  980. BOOST_RETHROW
  981. }
  982. BOOST_CATCH_END
  983. detail::commit_once_region(flag, ctx);
  984. break;
  985. }
  986. if(!ctx.counted)
  987. {
  988. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  989. ctx.counted=true;
  990. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  991. if(status==ctx.function_complete_flag_value)
  992. {
  993. break;
  994. }
  995. if(!ctx.event_handle)
  996. {
  997. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  998. continue;
  999. }
  1000. }
  1001. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  1002. ctx.event_handle,::boost::detail::win32::infinite,0));
  1003. }
  1004. }
  1005. template<typename Function, typename T1, typename T2, typename T3>
  1006. void call_once(once_flag& flag,BOOST_THREAD_RV_REF(Function) f, BOOST_THREAD_RV_REF(T1) p1, BOOST_THREAD_RV_REF(T2) p2, BOOST_THREAD_RV_REF(T3) p3)
  1007. {
  1008. // Try for a quick win: if the procedure has already been called
  1009. // just skip through:
  1010. detail::once_context ctx;
  1011. while(::boost::detail::interlocked_read_acquire(&flag.status)
  1012. !=ctx.function_complete_flag_value)
  1013. {
  1014. if(detail::enter_once_region(flag, ctx))
  1015. {
  1016. BOOST_TRY
  1017. {
  1018. BOOST_THREAD_INVOKE_RET_VOID(
  1019. thread_detail::decay_copy(boost::forward<Function>(f)),
  1020. thread_detail::decay_copy(boost::forward<T1>(p1)),
  1021. thread_detail::decay_copy(boost::forward<T2>(p2)),
  1022. thread_detail::decay_copy(boost::forward<T3>(p3))
  1023. ) BOOST_THREAD_INVOKE_RET_VOID_CALL;
  1024. }
  1025. BOOST_CATCH(...)
  1026. {
  1027. detail::rollback_once_region(flag, ctx);
  1028. BOOST_RETHROW
  1029. }
  1030. BOOST_CATCH_END
  1031. detail::commit_once_region(flag, ctx);
  1032. break;
  1033. }
  1034. if(!ctx.counted)
  1035. {
  1036. BOOST_INTERLOCKED_INCREMENT(&flag.count);
  1037. ctx.counted=true;
  1038. long status=::boost::detail::interlocked_read_acquire(&flag.status);
  1039. if(status==ctx.function_complete_flag_value)
  1040. {
  1041. break;
  1042. }
  1043. if(!ctx.event_handle)
  1044. {
  1045. ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
  1046. continue;
  1047. }
  1048. }
  1049. BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
  1050. ctx.event_handle,::boost::detail::win32::infinite,0));
  1051. }
  1052. }
  1053. #endif
  1054. #endif
  1055. }
  1056. #include <boost/config/abi_suffix.hpp>
  1057. #endif