From ffba68221b86c8ed1b668099dbff86d7f414b57f Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sat, 28 Apr 2007 16:15:13 +0000 Subject: [PATCH] Use __sync intrinsics on g++ 4.1+ [SVN r37527] --- include/boost/detail/atomic_count.hpp | 2 + include/boost/detail/atomic_count_sync.hpp | 57 ++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 include/boost/detail/atomic_count_sync.hpp diff --git a/include/boost/detail/atomic_count.hpp b/include/boost/detail/atomic_count.hpp index 9985b2c..823b929 100644 --- a/include/boost/detail/atomic_count.hpp +++ b/include/boost/detail/atomic_count.hpp @@ -93,6 +93,8 @@ typedef long atomic_count; # include #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) # include +#elif defined( __GNUC__ ) && ( __GNUC__ * 100 + __GNUC_MINOR__ >= 401 ) +# include #elif defined(__GLIBCPP__) || defined(__GLIBCXX__) # include #elif defined(BOOST_HAS_PTHREADS) diff --git a/include/boost/detail/atomic_count_sync.hpp b/include/boost/detail/atomic_count_sync.hpp new file mode 100644 index 0000000..e973fb7 --- /dev/null +++ b/include/boost/detail/atomic_count_sync.hpp @@ -0,0 +1,57 @@ +#ifndef BOOST_DETAIL_ATOMIC_COUNT_SYNC_HPP_INCLUDED +#define BOOST_DETAIL_ATOMIC_COUNT_SYNC_HPP_INCLUDED + +// +// boost/detail/atomic_count_sync.hpp +// +// atomic_count for g++ 4.1+ +// +// http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Atomic-Builtins.html +// +// Copyright 2007 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) +// + +namespace boost +{ + +namespace detail +{ + +class atomic_count +{ +public: + + explicit atomic_count( long v ) : value_( v ) {} + + void operator++() + { + __sync_add_and_fetch( &value_, 1 ); + } + + long operator--() + { + return __sync_add_and_fetch( &value_, -1 ); + } + + operator long() const + { + return __sync_fetch_and_add( &value_, 0 ); + } + +private: + + atomic_count(atomic_count const &); + atomic_count & operator=(atomic_count const &); + + mutable long value_; +}; + +} // namespace detail + +} // namespace boost + +#endif // #ifndef BOOST_DETAIL_ATOMIC_COUNT_SYNC_HPP_INCLUDED