| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088 |
- #ifndef BOOST_THREAD_WIN32_ONCE_HPP
- #define BOOST_THREAD_WIN32_ONCE_HPP
- // once.hpp
- //
- // (C) Copyright 2005-7 Anthony Williams
- // (C) Copyright 2005 John Maddock
- // (C) Copyright 2011-2013 Vicente J. Botet Escriba
- //
- // Distributed under the Boost Software License, Version 1.0. (See
- // accompanying file LICENSE_1_0.txt or copy at
- // http://www.boost.org/LICENSE_1_0.txt)
- #include <cstring>
- #include <cstddef>
- #include <boost/assert.hpp>
- #include <boost/static_assert.hpp>
- #include <boost/detail/interlocked.hpp>
- #include <boost/thread/win32/thread_primitives.hpp>
- #include <boost/thread/win32/interlocked_read.hpp>
- #include <boost/core/no_exceptions_support.hpp>
- #include <boost/thread/detail/move.hpp>
- #include <boost/thread/detail/invoke.hpp>
- #include <boost/bind.hpp>
- #include <boost/config/abi_prefix.hpp>
- #ifdef BOOST_NO_STDC_NAMESPACE
- namespace std
- {
- using ::memcpy;
- using ::ptrdiff_t;
- }
- #endif
- namespace boost
- {
- struct once_flag;
- namespace detail
- {
- struct once_context;
- inline bool enter_once_region(once_flag& flag, once_context& ctx) BOOST_NOEXCEPT;
- inline void commit_once_region(once_flag& flag, once_context& ctx) BOOST_NOEXCEPT;
- inline void rollback_once_region(once_flag& flag, once_context& ctx) BOOST_NOEXCEPT;
- }
- #ifdef BOOST_THREAD_PROVIDES_ONCE_CXX11
- struct once_flag
- {
- BOOST_THREAD_NO_COPYABLE(once_flag)
- BOOST_CONSTEXPR once_flag() BOOST_NOEXCEPT
- : status(0), count(0)
- {}
- long status;
- long count;
- private:
- friend inline bool enter_once_region(once_flag& flag, detail::once_context& ctx) BOOST_NOEXCEPT;
- friend inline void commit_once_region(once_flag& flag, detail::once_context& ctx) BOOST_NOEXCEPT;
- friend inline void rollback_once_region(once_flag& flag, detail::once_context& ctx) BOOST_NOEXCEPT;
- };
- #define BOOST_ONCE_INIT once_flag()
- #else // BOOST_THREAD_PROVIDES_ONCE_CXX11
- struct once_flag
- {
- long status;
- long count;
- };
- #define BOOST_ONCE_INIT {0,0}
- #endif // BOOST_THREAD_PROVIDES_ONCE_CXX11
- #if defined BOOST_THREAD_PROVIDES_INVOKE
- #define BOOST_THREAD_INVOKE_RET_VOID detail::invoke
- #define BOOST_THREAD_INVOKE_RET_VOID_CALL
- #elif defined BOOST_THREAD_PROVIDES_INVOKE_RET
- #define BOOST_THREAD_INVOKE_RET_VOID detail::invoke<void>
- #define BOOST_THREAD_INVOKE_RET_VOID_CALL
- #else
- #define BOOST_THREAD_INVOKE_RET_VOID boost::bind
- #define BOOST_THREAD_INVOKE_RET_VOID_CALL ()
- #endif
- namespace detail
- {
- #ifdef BOOST_NO_ANSI_APIS
- typedef wchar_t once_char_type;
- #else
- typedef char once_char_type;
- #endif
- unsigned const once_mutex_name_fixed_length=54;
- unsigned const once_mutex_name_length=once_mutex_name_fixed_length+
- sizeof(void*)*2+sizeof(unsigned long)*2+1;
- template <class I>
- void int_to_string(I p, once_char_type* buf)
- {
- for(unsigned i=0; i < sizeof(I)*2; ++i,++buf)
- {
- #ifdef BOOST_NO_ANSI_APIS
- once_char_type const a=L'A';
- #else
- once_char_type const a='A';
- #endif
- *buf = a + static_cast<once_char_type>((p >> (i*4)) & 0x0f);
- }
- *buf = 0;
- }
- inline void name_once_mutex(once_char_type* mutex_name,void* flag_address)
- {
- #ifdef BOOST_NO_ANSI_APIS
- static const once_char_type fixed_mutex_name[]=L"Local\\{C15730E2-145C-4c5e-B005-3BC753F42475}-once-flag";
- #else
- static const once_char_type fixed_mutex_name[]="Local\\{C15730E2-145C-4c5e-B005-3BC753F42475}-once-flag";
- #endif
- BOOST_STATIC_ASSERT(sizeof(fixed_mutex_name) ==
- (sizeof(once_char_type)*(once_mutex_name_fixed_length+1)));
- std::memcpy(mutex_name,fixed_mutex_name,sizeof(fixed_mutex_name));
- detail::int_to_string(reinterpret_cast<std::ptrdiff_t>(flag_address),
- mutex_name + once_mutex_name_fixed_length);
- detail::int_to_string(winapi::GetCurrentProcessId(),
- mutex_name + once_mutex_name_fixed_length + sizeof(void*)*2);
- }
- inline void* open_once_event(once_char_type* mutex_name,void* flag_address)
- {
- if(!*mutex_name)
- {
- name_once_mutex(mutex_name,flag_address);
- }
- #ifdef BOOST_NO_ANSI_APIS
- return ::boost::winapi::OpenEventW(
- #else
- return ::boost::winapi::OpenEventA(
- #endif
- ::boost::detail::win32::synchronize |
- ::boost::detail::win32::event_modify_state,
- false,
- mutex_name);
- }
- inline void* create_once_event(once_char_type* mutex_name,void* flag_address)
- {
- if(!*mutex_name)
- {
- name_once_mutex(mutex_name,flag_address);
- }
-
- return ::boost::detail::win32::create_event(
- mutex_name,
- ::boost::detail::win32::manual_reset_event,
- ::boost::detail::win32::event_initially_reset);
- }
- struct once_context {
- long const function_complete_flag_value;
- long const running_value;
- bool counted;
- detail::win32::handle_manager event_handle;
- detail::once_char_type mutex_name[once_mutex_name_length];
- once_context() :
- function_complete_flag_value(0xc15730e2),
- running_value(0x7f0725e3),
- counted(false)
- {
- mutex_name[0]=0;
- }
- };
- enum once_action {try_, break_, continue_};
- inline bool enter_once_region(once_flag& flag, once_context& ctx) BOOST_NOEXCEPT
- {
- long status=BOOST_INTERLOCKED_COMPARE_EXCHANGE(&flag.status,ctx.running_value,0);
- if(!status)
- {
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::open_once_event(ctx.mutex_name,&flag);
- }
- if(ctx.event_handle)
- {
- ::boost::winapi::ResetEvent(ctx.event_handle);
- }
- return true;
- }
- return false;
- }
- inline void commit_once_region(once_flag& flag, once_context& ctx) BOOST_NOEXCEPT
- {
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- }
- BOOST_INTERLOCKED_EXCHANGE(&flag.status,ctx.function_complete_flag_value);
- if(!ctx.event_handle &&
- (::boost::detail::interlocked_read_acquire(&flag.count)>1))
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- }
- if(ctx.event_handle)
- {
- ::boost::winapi::SetEvent(ctx.event_handle);
- }
- }
- inline void rollback_once_region(once_flag& flag, once_context& ctx) BOOST_NOEXCEPT
- {
- BOOST_INTERLOCKED_EXCHANGE(&flag.status,0);
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::open_once_event(ctx.mutex_name,&flag);
- }
- if(ctx.event_handle)
- {
- ::boost::winapi::SetEvent(ctx.event_handle);
- }
- }
- }
- #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
- //#if defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNCTION_PTR)
- inline void call_once(once_flag& flag, void (*f)())
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- f();
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite, 0));
- }
- }
- //#endif
- template<typename Function>
- inline void call_once(once_flag& flag, BOOST_THREAD_RV_REF(Function) f)
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- f();
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- template<typename Function, class A, class ...ArgTypes>
- 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)
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- BOOST_THREAD_INVOKE_RET_VOID(
- thread_detail::decay_copy(boost::forward<Function>(f)),
- thread_detail::decay_copy(boost::forward<A>(a)),
- thread_detail::decay_copy(boost::forward<ArgTypes>(args))...
- ) BOOST_THREAD_INVOKE_RET_VOID_CALL;
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- #else
- #if ! defined(BOOST_MSVC) && ! defined(BOOST_INTEL)
- template<typename Function>
- void call_once(once_flag& flag,Function f)
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- f();
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- template<typename Function, typename T1>
- void call_once(once_flag& flag,Function f, T1 p1)
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- BOOST_THREAD_INVOKE_RET_VOID(f,p1) BOOST_THREAD_INVOKE_RET_VOID_CALL;
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- template<typename Function, typename T1, typename T2>
- void call_once(once_flag& flag,Function f, T1 p1, T2 p2)
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- BOOST_THREAD_INVOKE_RET_VOID(f,p1,p2) BOOST_THREAD_INVOKE_RET_VOID_CALL;
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- template<typename Function, typename T1, typename T2, typename T3>
- void call_once(once_flag& flag,Function f, T1 p1, T2 p2, T3 p3)
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- BOOST_THREAD_INVOKE_RET_VOID(f,p1,p2,p3) BOOST_THREAD_INVOKE_RET_VOID_CALL;
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- #elif defined BOOST_NO_CXX11_RVALUE_REFERENCES
- template<typename Function>
- void call_once(once_flag& flag,Function const&f)
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- f();
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- template<typename Function, typename T1>
- void call_once(once_flag& flag,Function const&f, T1 const&p1)
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- BOOST_THREAD_INVOKE_RET_VOID(f,p1) BOOST_THREAD_INVOKE_RET_VOID_CALL;
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- template<typename Function, typename T1, typename T2>
- void call_once(once_flag& flag,Function const&f, T1 const&p1, T2 const&p2)
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- BOOST_THREAD_INVOKE_RET_VOID(f,p1,p2) BOOST_THREAD_INVOKE_RET_VOID_CALL;
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- template<typename Function, typename T1, typename T2, typename T3>
- void call_once(once_flag& flag,Function const&f, T1 const&p1, T2 const&p2, T3 const&p3)
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- BOOST_THREAD_INVOKE_RET_VOID(f,p1,p2,p3) BOOST_THREAD_INVOKE_RET_VOID_CALL;
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- #endif
- #if 1
- #if defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNCTION_PTR)
- inline void call_once(once_flag& flag, void (*f)())
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- f();
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- template<typename T1>
- void call_once(once_flag& flag,void (*f)(BOOST_THREAD_RV_REF(T1)), BOOST_THREAD_RV_REF(T1) p1)
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- f(
- thread_detail::decay_copy(boost::forward<T1>(p1))
- );
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- template<typename Function, typename T1, typename T2>
- 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)
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- f(
- thread_detail::decay_copy(boost::forward<T1>(p1)),
- thread_detail::decay_copy(boost::forward<T2>(p2))
- );
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- template<typename Function, typename T1, typename T2, typename T3>
- 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)
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- f(
- thread_detail::decay_copy(boost::forward<T1>(p1)),
- thread_detail::decay_copy(boost::forward<T2>(p2)),
- thread_detail::decay_copy(boost::forward<T3>(p3))
- );
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- #endif
- template<typename Function>
- void call_once(once_flag& flag,BOOST_THREAD_RV_REF(Function) f)
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- f();
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- template<typename Function, typename T1>
- void call_once(once_flag& flag,BOOST_THREAD_RV_REF(Function) f, BOOST_THREAD_RV_REF(T1) p1)
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- BOOST_THREAD_INVOKE_RET_VOID(
- thread_detail::decay_copy(boost::forward<Function>(f)),
- thread_detail::decay_copy(boost::forward<T1>(p1))
- ) BOOST_THREAD_INVOKE_RET_VOID_CALL;
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- template<typename Function, typename T1, typename T2>
- void call_once(once_flag& flag,BOOST_THREAD_RV_REF(Function) f, BOOST_THREAD_RV_REF(T1) p1, BOOST_THREAD_RV_REF(T2) p2)
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- BOOST_THREAD_INVOKE_RET_VOID(
- thread_detail::decay_copy(boost::forward<Function>(f)),
- thread_detail::decay_copy(boost::forward<T1>(p1)),
- thread_detail::decay_copy(boost::forward<T2>(p2))
- ) BOOST_THREAD_INVOKE_RET_VOID_CALL;
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- template<typename Function, typename T1, typename T2, typename T3>
- 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)
- {
- // Try for a quick win: if the procedure has already been called
- // just skip through:
- detail::once_context ctx;
- while(::boost::detail::interlocked_read_acquire(&flag.status)
- !=ctx.function_complete_flag_value)
- {
- if(detail::enter_once_region(flag, ctx))
- {
- BOOST_TRY
- {
- BOOST_THREAD_INVOKE_RET_VOID(
- thread_detail::decay_copy(boost::forward<Function>(f)),
- thread_detail::decay_copy(boost::forward<T1>(p1)),
- thread_detail::decay_copy(boost::forward<T2>(p2)),
- thread_detail::decay_copy(boost::forward<T3>(p3))
- ) BOOST_THREAD_INVOKE_RET_VOID_CALL;
- }
- BOOST_CATCH(...)
- {
- detail::rollback_once_region(flag, ctx);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- detail::commit_once_region(flag, ctx);
- break;
- }
- if(!ctx.counted)
- {
- BOOST_INTERLOCKED_INCREMENT(&flag.count);
- ctx.counted=true;
- long status=::boost::detail::interlocked_read_acquire(&flag.status);
- if(status==ctx.function_complete_flag_value)
- {
- break;
- }
- if(!ctx.event_handle)
- {
- ctx.event_handle=detail::create_once_event(ctx.mutex_name,&flag);
- continue;
- }
- }
- BOOST_VERIFY(!::boost::winapi::WaitForSingleObjectEx(
- ctx.event_handle,::boost::detail::win32::infinite,0));
- }
- }
- #endif
- #endif
- }
- #include <boost/config/abi_suffix.hpp>
- #endif
|