once.hpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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/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