spinlock_sync.hpp
Go to the documentation of this file.
00001 #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_SYNC_HPP_INCLUDED
00002 #define BOOST_SMART_PTR_DETAIL_SPINLOCK_SYNC_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/smart_ptr/detail/yield_k.hpp>
00019 
00020 #if defined( __ia64__ ) && defined( __INTEL_COMPILER )
00021 # include <ia64intrin.h>
00022 #endif
00023 
00024 namespace boost
00025 {
00026 
00027 namespace detail
00028 {
00029 
00030 class spinlock
00031 {
00032 public:
00033 
00034     int v_;
00035 
00036 public:
00037 
00038     bool try_lock()
00039     {
00040         int r = __sync_lock_test_and_set( &v_, 1 );
00041         return r == 0;
00042     }
00043 
00044     void lock()
00045     {
00046         for( unsigned k = 0; !try_lock(); ++k )
00047         {
00048             boost::detail::yield( k );
00049         }
00050     }
00051 
00052     void unlock()
00053     {
00054         __sync_lock_release( &v_ );
00055     }
00056 
00057 public:
00058 
00059     class scoped_lock
00060     {
00061     private:
00062 
00063         spinlock & sp_;
00064 
00065         scoped_lock( scoped_lock const & );
00066         scoped_lock & operator=( scoped_lock const & );
00067 
00068     public:
00069 
00070         explicit scoped_lock( spinlock & sp ): sp_( sp )
00071         {
00072             sp.lock();
00073         }
00074 
00075         ~scoped_lock()
00076         {
00077             sp_.unlock();
00078         }
00079     };
00080 };
00081 
00082 } // namespace detail
00083 } // namespace boost
00084 
00085 #define BOOST_DETAIL_SPINLOCK_INIT {0}
00086 
00087 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_SYNC_HPP_INCLUDED


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