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