File indexing completed on 2025-01-18 09:51:43
0001 #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_SYNC_HPP_INCLUDED
0002 #define BOOST_SMART_PTR_DETAIL_SPINLOCK_SYNC_HPP_INCLUDED
0003
0004
0005
0006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
0007 # pragma once
0008 #endif
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #include <boost/smart_ptr/detail/yield_k.hpp>
0019
0020 #if defined( __ia64__ ) && defined( __INTEL_COMPILER )
0021 # include <ia64intrin.h>
0022 #endif
0023
0024 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
0025
0026 #include <boost/config/pragma_message.hpp>
0027 BOOST_PRAGMA_MESSAGE("Using __sync spinlock")
0028
0029 #endif
0030
0031 namespace boost
0032 {
0033
0034 namespace detail
0035 {
0036
0037 class spinlock
0038 {
0039 public:
0040
0041 unsigned char v_;
0042
0043 public:
0044
0045 bool try_lock()
0046 {
0047 int r = __sync_lock_test_and_set( &v_, 1 );
0048 return r == 0;
0049 }
0050
0051 void lock()
0052 {
0053 for( unsigned k = 0; !try_lock(); ++k )
0054 {
0055 boost::detail::yield( k );
0056 }
0057 }
0058
0059 void unlock()
0060 {
0061 __sync_lock_release( &v_ );
0062 }
0063
0064 public:
0065
0066 class scoped_lock
0067 {
0068 private:
0069
0070 spinlock & sp_;
0071
0072 scoped_lock( scoped_lock const & );
0073 scoped_lock & operator=( scoped_lock const & );
0074
0075 public:
0076
0077 explicit scoped_lock( spinlock & sp ): sp_( sp )
0078 {
0079 sp.lock();
0080 }
0081
0082 ~scoped_lock()
0083 {
0084 sp_.unlock();
0085 }
0086 };
0087 };
0088
0089 }
0090 }
0091
0092 #define BOOST_DETAIL_SPINLOCK_INIT {0}
0093
0094 #endif