Chris@102: #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_STD_ATOMIC_HPP_INCLUDED Chris@102: #define BOOST_SMART_PTR_DETAIL_SPINLOCK_STD_ATOMIC_HPP_INCLUDED Chris@102: Chris@102: // MS compatible compilers support #pragma once Chris@102: Chris@102: #if defined(_MSC_VER) && (_MSC_VER >= 1020) Chris@102: # pragma once Chris@102: #endif Chris@102: Chris@102: // Chris@102: // Copyright (c) 2014 Peter Dimov Chris@102: // Chris@102: // Distributed under the Boost Software License, Version 1.0. Chris@102: // See accompanying file LICENSE_1_0.txt or copy at Chris@102: // http://www.boost.org/LICENSE_1_0.txt) Chris@102: // Chris@102: Chris@102: #include Chris@102: #include Chris@102: Chris@102: namespace boost Chris@102: { Chris@102: Chris@102: namespace detail Chris@102: { Chris@102: Chris@102: class spinlock Chris@102: { Chris@102: public: Chris@102: Chris@102: std::atomic_flag v_; Chris@102: Chris@102: public: Chris@102: Chris@102: bool try_lock() Chris@102: { Chris@102: return !v_.test_and_set( std::memory_order_acquire ); Chris@102: } Chris@102: Chris@102: void lock() Chris@102: { Chris@102: for( unsigned k = 0; !try_lock(); ++k ) Chris@102: { Chris@102: boost::detail::yield( k ); Chris@102: } Chris@102: } Chris@102: Chris@102: void unlock() Chris@102: { Chris@102: v_ .clear( std::memory_order_release ); Chris@102: } Chris@102: Chris@102: public: Chris@102: Chris@102: class scoped_lock Chris@102: { Chris@102: private: Chris@102: Chris@102: spinlock & sp_; Chris@102: Chris@102: scoped_lock( scoped_lock const & ); Chris@102: scoped_lock & operator=( scoped_lock const & ); Chris@102: Chris@102: public: Chris@102: Chris@102: explicit scoped_lock( spinlock & sp ): sp_( sp ) Chris@102: { Chris@102: sp.lock(); Chris@102: } Chris@102: Chris@102: ~scoped_lock() Chris@102: { Chris@102: sp_.unlock(); Chris@102: } Chris@102: }; Chris@102: }; Chris@102: Chris@102: } // namespace detail Chris@102: } // namespace boost Chris@102: Chris@102: #define BOOST_DETAIL_SPINLOCK_INIT { ATOMIC_FLAG_INIT } Chris@102: Chris@102: #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_STD_ATOMIC_HPP_INCLUDED