1 #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_SYNC_HPP_INCLUDED 2 #define BOOST_SMART_PTR_DETAIL_SPINLOCK_SYNC_HPP_INCLUDED 3 4 // MS compatible compilers support #pragma once 5 6 #if defined(_MSC_VER) && (_MSC_VER >= 1020) 7 # pragma once 8 #endif 9 10 // 11 // Copyright (c) 2008 Peter Dimov 12 // 13 // Distributed under the Boost Software License, Version 1.0. 14 // See accompanying file LICENSE_1_0.txt or copy at 15 // http://www.boost.org/LICENSE_1_0.txt) 16 // 17 18 #include <boost/smart_ptr/detail/yield_k.hpp> 19 20 #if defined( __ia64__ ) && defined( __INTEL_COMPILER ) 21 # include <ia64intrin.h> 22 #endif 23 24 #if defined(BOOST_SP_REPORT_IMPLEMENTATION) 25 26 #include <boost/config/pragma_message.hpp> 27 BOOST_PRAGMA_MESSAGE("Using __sync spinlock") 28 29 #endif 30 31 namespace boost 32 { 33 34 namespace detail 35 { 36 37 class spinlock 38 { 39 public: 40 41 unsigned char v_; 42 43 public: 44 try_lock()45 bool try_lock() 46 { 47 int r = __sync_lock_test_and_set( &v_, 1 ); 48 return r == 0; 49 } 50 lock()51 void lock() 52 { 53 for( unsigned k = 0; !try_lock(); ++k ) 54 { 55 boost::detail::yield( k ); 56 } 57 } 58 unlock()59 void unlock() 60 { 61 __sync_lock_release( &v_ ); 62 } 63 64 public: 65 66 class scoped_lock 67 { 68 private: 69 70 spinlock & sp_; 71 72 scoped_lock( scoped_lock const & ); 73 scoped_lock & operator=( scoped_lock const & ); 74 75 public: 76 scoped_lock(spinlock & sp)77 explicit scoped_lock( spinlock & sp ): sp_( sp ) 78 { 79 sp.lock(); 80 } 81 ~scoped_lock()82 ~scoped_lock() 83 { 84 sp_.unlock(); 85 } 86 }; 87 }; 88 89 } // namespace detail 90 } // namespace boost 91 92 #define BOOST_DETAIL_SPINLOCK_INIT {0} 93 94 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_SYNC_HPP_INCLUDED 95