spinlock_w32.hpp
Go to the documentation of this file.
00001 #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_W32_HPP_INCLUDED
00002 #define BOOST_SMART_PTR_DETAIL_SPINLOCK_W32_HPP_INCLUDED
00003 
00004 // MS compatible compilers support #pragma once
00005 
00006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
00007 # pragma once
00008 #endif
00009 
00010 //
00011 //  Copyright (c) 2008 Peter Dimov
00012 //
00013 //  Distributed under the Boost Software License, Version 1.0.
00014 //  See accompanying file LICENSE_1_0.txt or copy at
00015 //  http://www.boost.org/LICENSE_1_0.txt)
00016 //
00017 
00018 #include <boost/detail/interlocked.hpp>
00019 #include <boost/smart_ptr/detail/yield_k.hpp>
00020 
00021 // BOOST_COMPILER_FENCE
00022 
00023 #if defined(__INTEL_COMPILER)
00024 
00025 #define BOOST_COMPILER_FENCE __memory_barrier();
00026 
00027 #elif defined( _MSC_VER ) && _MSC_VER >= 1310
00028 
00029 extern "C" void _ReadWriteBarrier();
00030 #pragma intrinsic( _ReadWriteBarrier )
00031 
00032 #define BOOST_COMPILER_FENCE _ReadWriteBarrier();
00033 
00034 #elif defined(__GNUC__)
00035 
00036 #define BOOST_COMPILER_FENCE __asm__ __volatile__( "" : : : "memory" );
00037 
00038 #else
00039 
00040 #define BOOST_COMPILER_FENCE
00041 
00042 #endif
00043 
00044 //
00045 
00046 namespace boost
00047 {
00048 
00049 namespace detail
00050 {
00051 
00052 class spinlock
00053 {
00054 public:
00055 
00056     long v_;
00057 
00058 public:
00059 
00060     bool try_lock()
00061     {
00062         long r = BOOST_INTERLOCKED_EXCHANGE( &v_, 1 );
00063 
00064         BOOST_COMPILER_FENCE
00065 
00066         return r == 0;
00067     }
00068 
00069     void lock()
00070     {
00071         for( unsigned k = 0; !try_lock(); ++k )
00072         {
00073             boost::detail::yield( k );
00074         }
00075     }
00076 
00077     void unlock()
00078     {
00079         BOOST_COMPILER_FENCE
00080         *const_cast< long volatile* >( &v_ ) = 0;
00081     }
00082 
00083 public:
00084 
00085     class scoped_lock
00086     {
00087     private:
00088 
00089         spinlock & sp_;
00090 
00091         scoped_lock( scoped_lock const & );
00092         scoped_lock & operator=( scoped_lock const & );
00093 
00094     public:
00095 
00096         explicit scoped_lock( spinlock & sp ): sp_( sp )
00097         {
00098             sp.lock();
00099         }
00100 
00101         ~scoped_lock()
00102         {
00103             sp_.unlock();
00104         }
00105     };
00106 };
00107 
00108 } // namespace detail
00109 } // namespace boost
00110 
00111 #define BOOST_DETAIL_SPINLOCK_INIT {0}
00112 
00113 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_W32_HPP_INCLUDED


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29