Add spinlock_gcc_atomic.hpp

This commit is contained in:
Peter Dimov
2020-06-07 05:00:03 +03:00
parent 914b93430a
commit 00db1e02c6
2 changed files with 96 additions and 8 deletions

View File

@ -28,21 +28,19 @@
// #define BOOST_DETAIL_SPINLOCK_INIT <unspecified>
//
#include <boost/config.hpp>
#include <boost/smart_ptr/detail/sp_has_gcc_intrinsics.hpp>
#include <boost/smart_ptr/detail/sp_has_sync_intrinsics.hpp>
#include <boost/config.hpp>
#if defined( BOOST_SP_USE_STD_ATOMIC )
# if !defined( __clang__ )
# include <boost/smart_ptr/detail/spinlock_std_atomic.hpp>
# else
// Clang (at least up to 3.4) can't compile spinlock_pool when
// using std::atomic, so substitute the __sync implementation instead.
# include <boost/smart_ptr/detail/spinlock_sync.hpp>
# endif
# include <boost/smart_ptr/detail/spinlock_std_atomic.hpp>
#elif defined( BOOST_SP_USE_PTHREADS )
# include <boost/smart_ptr/detail/spinlock_pt.hpp>
#elif defined( BOOST_SP_HAS_GCC_INTRINSICS )
# include <boost/smart_ptr/detail/spinlock_gcc_atomic.hpp>
#elif !defined( BOOST_NO_CXX11_HDR_ATOMIC )
# include <boost/smart_ptr/detail/spinlock_std_atomic.hpp>

View File

@ -0,0 +1,90 @@
#ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_GCC_ATOMIC_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SPINLOCK_GCC_ATOMIC_HPP_INCLUDED
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
//
// Copyright (c) 2008 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/smart_ptr/detail/yield_k.hpp>
#if defined(BOOST_SP_REPORT_IMPLEMENTATION)
#include <boost/config/pragma_message.hpp>
BOOST_PRAGMA_MESSAGE("Using __atomic spinlock")
#endif
namespace boost
{
namespace detail
{
class spinlock
{
public:
unsigned char v_;
public:
bool try_lock()
{
int r = __atomic_test_and_set( &v_, __ATOMIC_ACQUIRE );
return r == 0;
}
void lock()
{
for( unsigned k = 0; !try_lock(); ++k )
{
boost::detail::yield( k );
}
}
void unlock()
{
__atomic_clear( &v_, __ATOMIC_RELEASE );
}
public:
class scoped_lock
{
private:
spinlock & sp_;
scoped_lock( scoped_lock const & );
scoped_lock & operator=( scoped_lock const & );
public:
explicit scoped_lock( spinlock & sp ): sp_( sp )
{
sp.lock();
}
~scoped_lock()
{
sp_.unlock();
}
};
};
} // namespace detail
} // namespace boost
#define BOOST_DETAIL_SPINLOCK_INIT {0}
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_GCC_ATOMIC_HPP_INCLUDED