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