123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- #ifndef BOOST_THREAD_PTHREAD_ONCE_ATOMIC_HPP
- #define BOOST_THREAD_PTHREAD_ONCE_ATOMIC_HPP
- // once.hpp
- //
- // (C) Copyright 2013 Andrey Semashev
- // (C) Copyright 2013 Vicente J. Botet Escriba
- //
- // Distributed under the Boost Software License, Version 1.0. (See
- // accompanying file LICENSE_1_0.txt or copy at
- // http://www.boost.org/LICENSE_1_0.txt)
- #include <boost/thread/detail/config.hpp>
- #include <boost/cstdint.hpp>
- #include <boost/thread/detail/move.hpp>
- #include <boost/thread/detail/invoke.hpp>
- #include <boost/core/no_exceptions_support.hpp>
- #include <boost/bind/bind.hpp>
- #include <boost/atomic.hpp>
- #include <boost/config/abi_prefix.hpp>
- namespace boost
- {
- struct once_flag;
- namespace thread_detail
- {
- #if BOOST_ATOMIC_INT_LOCK_FREE == 2
- typedef unsigned int atomic_int_type;
- #elif BOOST_ATOMIC_SHORT_LOCK_FREE == 2
- typedef unsigned short atomic_int_type;
- #elif BOOST_ATOMIC_CHAR_LOCK_FREE == 2
- typedef unsigned char atomic_int_type;
- #elif BOOST_ATOMIC_LONG_LOCK_FREE == 2
- typedef unsigned long atomic_int_type;
- #elif defined(BOOST_HAS_LONG_LONG) && BOOST_ATOMIC_LLONG_LOCK_FREE == 2
- typedef ulong_long_type atomic_int_type;
- #else
- // All tested integer types are not atomic, the spinlock pool will be used
- typedef unsigned int atomic_int_type;
- #endif
- typedef boost::atomic<atomic_int_type> atomic_type;
- BOOST_THREAD_DECL bool enter_once_region(once_flag& flag) BOOST_NOEXCEPT;
- BOOST_THREAD_DECL void commit_once_region(once_flag& flag) BOOST_NOEXCEPT;
- BOOST_THREAD_DECL void rollback_once_region(once_flag& flag) BOOST_NOEXCEPT;
- inline atomic_type& get_atomic_storage(once_flag& flag) BOOST_NOEXCEPT;
- }
- #ifdef BOOST_THREAD_PROVIDES_ONCE_CXX11
- struct once_flag
- {
- BOOST_THREAD_NO_COPYABLE(once_flag)
- BOOST_CONSTEXPR once_flag() BOOST_NOEXCEPT : storage(0)
- {
- }
- private:
- thread_detail::atomic_type storage;
- friend BOOST_THREAD_DECL bool thread_detail::enter_once_region(once_flag& flag) BOOST_NOEXCEPT;
- friend BOOST_THREAD_DECL void thread_detail::commit_once_region(once_flag& flag) BOOST_NOEXCEPT;
- friend BOOST_THREAD_DECL void thread_detail::rollback_once_region(once_flag& flag) BOOST_NOEXCEPT;
- friend thread_detail::atomic_type& thread_detail::get_atomic_storage(once_flag& flag) BOOST_NOEXCEPT;
- };
- #define BOOST_ONCE_INIT boost::once_flag()
- namespace thread_detail
- {
- inline atomic_type& get_atomic_storage(once_flag& flag) BOOST_NOEXCEPT
- {
- //return reinterpret_cast< atomic_type& >(flag.storage);
- return flag.storage;
- }
- }
- #else // BOOST_THREAD_PROVIDES_ONCE_CXX11
- struct once_flag
- {
- // The thread_detail::atomic_int_type storage is marked
- // with this attribute in order to let the compiler know that it will alias this member
- // and silence compilation warnings.
- BOOST_THREAD_ATTRIBUTE_MAY_ALIAS thread_detail::atomic_int_type storage;
- };
- #define BOOST_ONCE_INIT {0}
- namespace thread_detail
- {
- inline atomic_type& get_atomic_storage(once_flag& flag) BOOST_NOEXCEPT
- {
- return reinterpret_cast< atomic_type& >(flag.storage);
- }
- }
- #endif // BOOST_THREAD_PROVIDES_ONCE_CXX11
- #if defined BOOST_THREAD_PROVIDES_INVOKE
- #define BOOST_THREAD_INVOKE_RET_VOID detail::invoke
- #define BOOST_THREAD_INVOKE_RET_VOID_CALL
- #elif defined BOOST_THREAD_PROVIDES_INVOKE_RET
- #define BOOST_THREAD_INVOKE_RET_VOID detail::invoke<void>
- #define BOOST_THREAD_INVOKE_RET_VOID_CALL
- #else
- #define BOOST_THREAD_INVOKE_RET_VOID boost::bind
- #define BOOST_THREAD_INVOKE_RET_VOID_CALL ()
- #endif
- #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
- template<typename Function, class ...ArgTypes>
- inline void call_once(once_flag& flag, BOOST_THREAD_RV_REF(Function) f, BOOST_THREAD_RV_REF(ArgTypes)... args)
- {
- if (thread_detail::enter_once_region(flag))
- {
- BOOST_TRY
- {
- BOOST_THREAD_INVOKE_RET_VOID(
- thread_detail::decay_copy(boost::forward<Function>(f)),
- thread_detail::decay_copy(boost::forward<ArgTypes>(args))...
- ) BOOST_THREAD_INVOKE_RET_VOID_CALL;
- }
- BOOST_CATCH (...)
- {
- thread_detail::rollback_once_region(flag);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- thread_detail::commit_once_region(flag);
- }
- }
- #else
- template<typename Function>
- inline void call_once(once_flag& flag, Function f)
- {
- if (thread_detail::enter_once_region(flag))
- {
- BOOST_TRY
- {
- f();
- }
- BOOST_CATCH (...)
- {
- thread_detail::rollback_once_region(flag);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- thread_detail::commit_once_region(flag);
- }
- }
- template<typename Function, typename T1>
- inline void call_once(once_flag& flag, Function f, T1 p1)
- {
- if (thread_detail::enter_once_region(flag))
- {
- BOOST_TRY
- {
- BOOST_THREAD_INVOKE_RET_VOID(f, p1) BOOST_THREAD_INVOKE_RET_VOID_CALL;
- }
- BOOST_CATCH (...)
- {
- thread_detail::rollback_once_region(flag);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- thread_detail::commit_once_region(flag);
- }
- }
- template<typename Function, typename T1, typename T2>
- inline void call_once(once_flag& flag, Function f, T1 p1, T2 p2)
- {
- if (thread_detail::enter_once_region(flag))
- {
- BOOST_TRY
- {
- BOOST_THREAD_INVOKE_RET_VOID(f, p1, p2) BOOST_THREAD_INVOKE_RET_VOID_CALL;
- }
- BOOST_CATCH (...)
- {
- thread_detail::rollback_once_region(flag);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- thread_detail::commit_once_region(flag);
- }
- }
- template<typename Function, typename T1, typename T2, typename T3>
- inline void call_once(once_flag& flag, Function f, T1 p1, T2 p2, T3 p3)
- {
- if (thread_detail::enter_once_region(flag))
- {
- BOOST_TRY
- {
- BOOST_THREAD_INVOKE_RET_VOID(f, p1, p2, p3) BOOST_THREAD_INVOKE_RET_VOID_CALL;
- }
- BOOST_CATCH (...)
- {
- thread_detail::rollback_once_region(flag);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- thread_detail::commit_once_region(flag);
- }
- }
- #if !(defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x5130))
- template<typename Function>
- inline void call_once(once_flag& flag, BOOST_THREAD_RV_REF(Function) f)
- {
- if (thread_detail::enter_once_region(flag))
- {
- BOOST_TRY
- {
- f();
- }
- BOOST_CATCH (...)
- {
- thread_detail::rollback_once_region(flag);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- thread_detail::commit_once_region(flag);
- }
- }
- template<typename Function, typename T1>
- inline void call_once(once_flag& flag, BOOST_THREAD_RV_REF(Function) f, BOOST_THREAD_RV_REF(T1) p1)
- {
- if (thread_detail::enter_once_region(flag))
- {
- BOOST_TRY
- {
- BOOST_THREAD_INVOKE_RET_VOID(
- thread_detail::decay_copy(boost::forward<Function>(f)),
- thread_detail::decay_copy(boost::forward<T1>(p1))
- ) BOOST_THREAD_INVOKE_RET_VOID_CALL;
- }
- BOOST_CATCH (...)
- {
- thread_detail::rollback_once_region(flag);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- thread_detail::commit_once_region(flag);
- }
- }
- template<typename Function, typename T1, typename T2>
- 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)
- {
- if (thread_detail::enter_once_region(flag))
- {
- BOOST_TRY
- {
- BOOST_THREAD_INVOKE_RET_VOID(
- thread_detail::decay_copy(boost::forward<Function>(f)),
- thread_detail::decay_copy(boost::forward<T1>(p1)),
- thread_detail::decay_copy(boost::forward<T1>(p2))
- ) BOOST_THREAD_INVOKE_RET_VOID_CALL;
- }
- BOOST_CATCH (...)
- {
- thread_detail::rollback_once_region(flag);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- thread_detail::commit_once_region(flag);
- }
- }
- template<typename Function, typename T1, typename T2, typename T3>
- 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)
- {
- if (thread_detail::enter_once_region(flag))
- {
- BOOST_TRY
- {
- BOOST_THREAD_INVOKE_RET_VOID(
- thread_detail::decay_copy(boost::forward<Function>(f)),
- thread_detail::decay_copy(boost::forward<T1>(p1)),
- thread_detail::decay_copy(boost::forward<T1>(p2)),
- thread_detail::decay_copy(boost::forward<T1>(p3))
- ) BOOST_THREAD_INVOKE_RET_VOID_CALL;
- }
- BOOST_CATCH (...)
- {
- thread_detail::rollback_once_region(flag);
- BOOST_RETHROW
- }
- BOOST_CATCH_END
- thread_detail::commit_once_region(flag);
- }
- }
- #endif // __SUNPRO_CC
- #endif
- }
- #include <boost/config/abi_suffix.hpp>
- #endif
|