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