Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:43

0001 #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_W32_HPP_INCLUDED
0002 #define BOOST_SMART_PTR_DETAIL_SPINLOCK_W32_HPP_INCLUDED
0003 
0004 // MS compatible compilers support #pragma once
0005 
0006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
0007 # pragma once
0008 #endif
0009 
0010 //
0011 //  Copyright (c) 2008 Peter Dimov
0012 //
0013 //  Distributed under the Boost Software License, Version 1.0.
0014 //  See accompanying file LICENSE_1_0.txt or copy at
0015 //  http://www.boost.org/LICENSE_1_0.txt)
0016 //
0017 
0018 #include <boost/smart_ptr/detail/sp_interlocked.hpp>
0019 #include <boost/smart_ptr/detail/yield_k.hpp>
0020 
0021 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
0022 
0023 #include <boost/config/pragma_message.hpp>
0024 BOOST_PRAGMA_MESSAGE("Using Win32 spinlock")
0025 
0026 #endif
0027 
0028 // BOOST_COMPILER_FENCE
0029 
0030 #if defined(__INTEL_COMPILER)
0031 
0032 #define BOOST_COMPILER_FENCE __memory_barrier();
0033 
0034 #elif defined( _MSC_VER ) && _MSC_VER >= 1310
0035 
0036 extern "C" void _ReadWriteBarrier();
0037 #pragma intrinsic( _ReadWriteBarrier )
0038 
0039 #define BOOST_COMPILER_FENCE _ReadWriteBarrier();
0040 
0041 #elif defined(__GNUC__)
0042 
0043 #define BOOST_COMPILER_FENCE __asm__ __volatile__( "" : : : "memory" );
0044 
0045 #else
0046 
0047 #define BOOST_COMPILER_FENCE
0048 
0049 #endif
0050 
0051 //
0052 
0053 namespace boost
0054 {
0055 
0056 namespace detail
0057 {
0058 
0059 class spinlock
0060 {
0061 public:
0062 
0063     long v_;
0064 
0065 public:
0066 
0067     bool try_lock()
0068     {
0069         long r = BOOST_SP_INTERLOCKED_EXCHANGE( &v_, 1 );
0070 
0071         BOOST_COMPILER_FENCE
0072 
0073         return r == 0;
0074     }
0075 
0076     void lock()
0077     {
0078         for( unsigned k = 0; !try_lock(); ++k )
0079         {
0080             boost::detail::yield( k );
0081         }
0082     }
0083 
0084     void unlock()
0085     {
0086         BOOST_COMPILER_FENCE
0087         *const_cast< long volatile* >( &v_ ) = 0;
0088     }
0089 
0090 public:
0091 
0092     class scoped_lock
0093     {
0094     private:
0095 
0096         spinlock & sp_;
0097 
0098         scoped_lock( scoped_lock const & );
0099         scoped_lock & operator=( scoped_lock const & );
0100 
0101     public:
0102 
0103         explicit scoped_lock( spinlock & sp ): sp_( sp )
0104         {
0105             sp.lock();
0106         }
0107 
0108         ~scoped_lock()
0109         {
0110             sp_.unlock();
0111         }
0112     };
0113 };
0114 
0115 } // namespace detail
0116 } // namespace boost
0117 
0118 #define BOOST_DETAIL_SPINLOCK_INIT {0}
0119 
0120 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_W32_HPP_INCLUDED