1 #ifndef BOOST_THREAD_PTHREAD_ONCE_ATOMIC_HPP
2 #define BOOST_THREAD_PTHREAD_ONCE_ATOMIC_HPP
6 // (C) Copyright 2013 Andrey Semashev
7 // (C) Copyright 2013 Vicente J. Botet Escriba
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 #include <boost/thread/detail/config.hpp>
15 #include <boost/cstdint.hpp>
16 #include <boost/thread/detail/move.hpp>
17 #include <boost/thread/detail/invoke.hpp>
18 #include <boost/core/no_exceptions_support.hpp>
19 #include <boost/bind/bind.hpp>
20 #include <boost/atomic.hpp>
22 #include <boost/config/abi_prefix.hpp>
29 namespace thread_detail
32 #if BOOST_ATOMIC_INT_LOCK_FREE == 2
33 typedef unsigned int atomic_int_type;
34 #elif BOOST_ATOMIC_SHORT_LOCK_FREE == 2
35 typedef unsigned short atomic_int_type;
36 #elif BOOST_ATOMIC_CHAR_LOCK_FREE == 2
37 typedef unsigned char atomic_int_type;
38 #elif BOOST_ATOMIC_LONG_LOCK_FREE == 2
39 typedef unsigned long atomic_int_type;
40 #elif defined(BOOST_HAS_LONG_LONG) && BOOST_ATOMIC_LLONG_LOCK_FREE == 2
41 typedef ulong_long_type atomic_int_type;
43 // All tested integer types are not atomic, the spinlock pool will be used
44 typedef unsigned int atomic_int_type;
47 typedef boost::atomic<atomic_int_type> atomic_type;
49 BOOST_THREAD_DECL bool enter_once_region(once_flag& flag) BOOST_NOEXCEPT;
50 BOOST_THREAD_DECL void commit_once_region(once_flag& flag) BOOST_NOEXCEPT;
51 BOOST_THREAD_DECL void rollback_once_region(once_flag& flag) BOOST_NOEXCEPT;
52 inline atomic_type& get_atomic_storage(once_flag& flag) BOOST_NOEXCEPT;
55 #ifdef BOOST_THREAD_PROVIDES_ONCE_CXX11
59 BOOST_THREAD_NO_COPYABLE(once_flag)
60 BOOST_CONSTEXPR once_flag() BOOST_NOEXCEPT : storage(0)
65 thread_detail::atomic_type storage;
67 friend BOOST_THREAD_DECL bool thread_detail::enter_once_region(once_flag& flag) BOOST_NOEXCEPT;
68 friend BOOST_THREAD_DECL void thread_detail::commit_once_region(once_flag& flag) BOOST_NOEXCEPT;
69 friend BOOST_THREAD_DECL void thread_detail::rollback_once_region(once_flag& flag) BOOST_NOEXCEPT;
70 friend thread_detail::atomic_type& thread_detail::get_atomic_storage(once_flag& flag) BOOST_NOEXCEPT;
73 #define BOOST_ONCE_INIT boost::once_flag()
75 namespace thread_detail
77 inline atomic_type& get_atomic_storage(once_flag& flag) BOOST_NOEXCEPT
79 //return reinterpret_cast< atomic_type& >(flag.storage);
84 #else // BOOST_THREAD_PROVIDES_ONCE_CXX11
87 // The thread_detail::atomic_int_type storage is marked
88 // with this attribute in order to let the compiler know that it will alias this member
89 // and silence compilation warnings.
90 BOOST_THREAD_ATTRIBUTE_MAY_ALIAS thread_detail::atomic_int_type storage;
93 #define BOOST_ONCE_INIT {0}
95 namespace thread_detail
97 inline atomic_type& get_atomic_storage(once_flag& flag) BOOST_NOEXCEPT
99 return reinterpret_cast< atomic_type& >(flag.storage);
104 #endif // BOOST_THREAD_PROVIDES_ONCE_CXX11
106 #if defined BOOST_THREAD_PROVIDES_INVOKE
107 #define BOOST_THREAD_INVOKE_RET_VOID detail::invoke
108 #define BOOST_THREAD_INVOKE_RET_VOID_CALL
109 #elif defined BOOST_THREAD_PROVIDES_INVOKE_RET
110 #define BOOST_THREAD_INVOKE_RET_VOID detail::invoke<void>
111 #define BOOST_THREAD_INVOKE_RET_VOID_CALL
113 #define BOOST_THREAD_INVOKE_RET_VOID boost::bind
114 #define BOOST_THREAD_INVOKE_RET_VOID_CALL ()
118 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
120 template<typename Function, class ...ArgTypes>
121 inline void call_once(once_flag& flag, BOOST_THREAD_RV_REF(Function) f, BOOST_THREAD_RV_REF(ArgTypes)... args)
123 if (thread_detail::enter_once_region(flag))
127 BOOST_THREAD_INVOKE_RET_VOID(
128 thread_detail::decay_copy(boost::forward<Function>(f)),
129 thread_detail::decay_copy(boost::forward<ArgTypes>(args))...
130 ) BOOST_THREAD_INVOKE_RET_VOID_CALL;
134 thread_detail::rollback_once_region(flag);
138 thread_detail::commit_once_region(flag);
142 template<typename Function>
143 inline void call_once(once_flag& flag, Function f)
145 if (thread_detail::enter_once_region(flag))
153 thread_detail::rollback_once_region(flag);
157 thread_detail::commit_once_region(flag);
161 template<typename Function, typename T1>
162 inline void call_once(once_flag& flag, Function f, T1 p1)
164 if (thread_detail::enter_once_region(flag))
168 BOOST_THREAD_INVOKE_RET_VOID(f, p1) BOOST_THREAD_INVOKE_RET_VOID_CALL;
172 thread_detail::rollback_once_region(flag);
176 thread_detail::commit_once_region(flag);
180 template<typename Function, typename T1, typename T2>
181 inline void call_once(once_flag& flag, Function f, T1 p1, T2 p2)
183 if (thread_detail::enter_once_region(flag))
187 BOOST_THREAD_INVOKE_RET_VOID(f, p1, p2) BOOST_THREAD_INVOKE_RET_VOID_CALL;
191 thread_detail::rollback_once_region(flag);
195 thread_detail::commit_once_region(flag);
199 template<typename Function, typename T1, typename T2, typename T3>
200 inline void call_once(once_flag& flag, Function f, T1 p1, T2 p2, T3 p3)
202 if (thread_detail::enter_once_region(flag))
206 BOOST_THREAD_INVOKE_RET_VOID(f, p1, p2, p3) BOOST_THREAD_INVOKE_RET_VOID_CALL;
210 thread_detail::rollback_once_region(flag);
214 thread_detail::commit_once_region(flag);
217 #if !(defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x5130))
218 template<typename Function>
219 inline void call_once(once_flag& flag, BOOST_THREAD_RV_REF(Function) f)
221 if (thread_detail::enter_once_region(flag))
229 thread_detail::rollback_once_region(flag);
233 thread_detail::commit_once_region(flag);
237 template<typename Function, typename T1>
238 inline void call_once(once_flag& flag, BOOST_THREAD_RV_REF(Function) f, BOOST_THREAD_RV_REF(T1) p1)
240 if (thread_detail::enter_once_region(flag))
244 BOOST_THREAD_INVOKE_RET_VOID(
245 thread_detail::decay_copy(boost::forward<Function>(f)),
246 thread_detail::decay_copy(boost::forward<T1>(p1))
247 ) BOOST_THREAD_INVOKE_RET_VOID_CALL;
251 thread_detail::rollback_once_region(flag);
255 thread_detail::commit_once_region(flag);
258 template<typename Function, typename T1, typename T2>
259 inline void call_once(once_flag& flag, BOOST_THREAD_RV_REF(Function) f, BOOST_THREAD_RV_REF(T1) p1, BOOST_THREAD_RV_REF(T2) p2)
261 if (thread_detail::enter_once_region(flag))
265 BOOST_THREAD_INVOKE_RET_VOID(
266 thread_detail::decay_copy(boost::forward<Function>(f)),
267 thread_detail::decay_copy(boost::forward<T1>(p1)),
268 thread_detail::decay_copy(boost::forward<T1>(p2))
269 ) BOOST_THREAD_INVOKE_RET_VOID_CALL;
273 thread_detail::rollback_once_region(flag);
277 thread_detail::commit_once_region(flag);
280 template<typename Function, typename T1, typename T2, typename T3>
281 inline 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)
283 if (thread_detail::enter_once_region(flag))
287 BOOST_THREAD_INVOKE_RET_VOID(
288 thread_detail::decay_copy(boost::forward<Function>(f)),
289 thread_detail::decay_copy(boost::forward<T1>(p1)),
290 thread_detail::decay_copy(boost::forward<T1>(p2)),
291 thread_detail::decay_copy(boost::forward<T1>(p3))
292 ) BOOST_THREAD_INVOKE_RET_VOID_CALL;
297 thread_detail::rollback_once_region(flag);
301 thread_detail::commit_once_region(flag);
305 #endif // __SUNPRO_CC
310 #include <boost/config/abi_suffix.hpp>