Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 1 | #ifndef NDNBOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_HPP_INCLUDED |
| 2 | #define NDNBOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_HPP_INCLUDED |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 3 | |
| 4 | // |
Jeff Thompson | 9939dcd | 2013-10-15 15:12:24 -0700 | [diff] [blame] | 5 | // ndnboost/detail/atomic_count_gcc.hpp |
Jeff Thompson | f7d4994 | 2013-08-01 16:47:40 -0700 | [diff] [blame] | 6 | // |
| 7 | // atomic_count for GNU libstdc++ v3 |
| 8 | // |
| 9 | // http://gcc.gnu.org/onlinedocs/porting/Thread-safety.html |
| 10 | // |
| 11 | // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. |
| 12 | // Copyright (c) 2002 Lars Gullik Bjønnes <larsbj@lyx.org> |
| 13 | // Copyright 2003-2005 Peter Dimov |
| 14 | // |
| 15 | // Distributed under the Boost Software License, Version 1.0. (See |
| 16 | // accompanying file LICENSE_1_0.txt or copy at |
| 17 | // http://www.boost.org/LICENSE_1_0.txt) |
| 18 | // |
| 19 | |
| 20 | #if __GNUC__ * 100 + __GNUC_MINOR__ >= 402 |
| 21 | # include <ext/atomicity.h> |
| 22 | #else |
| 23 | # include <bits/atomicity.h> |
| 24 | #endif |
| 25 | |
| 26 | namespace ndnboost |
| 27 | { |
| 28 | |
| 29 | namespace detail |
| 30 | { |
| 31 | |
| 32 | #if defined(__GLIBCXX__) // g++ 3.4+ |
| 33 | |
| 34 | using __gnu_cxx::__atomic_add; |
| 35 | using __gnu_cxx::__exchange_and_add; |
| 36 | |
| 37 | #endif |
| 38 | |
| 39 | class atomic_count |
| 40 | { |
| 41 | public: |
| 42 | |
| 43 | explicit atomic_count( long v ) : value_( v ) {} |
| 44 | |
| 45 | long operator++() |
| 46 | { |
| 47 | return __exchange_and_add( &value_, +1 ) + 1; |
| 48 | } |
| 49 | |
| 50 | long operator--() |
| 51 | { |
| 52 | return __exchange_and_add( &value_, -1 ) - 1; |
| 53 | } |
| 54 | |
| 55 | operator long() const |
| 56 | { |
| 57 | return __exchange_and_add( &value_, 0 ); |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | |
| 62 | atomic_count(atomic_count const &); |
| 63 | atomic_count & operator=(atomic_count const &); |
| 64 | |
| 65 | mutable _Atomic_word value_; |
| 66 | }; |
| 67 | |
| 68 | } // namespace detail |
| 69 | |
| 70 | } // namespace ndnboost |
| 71 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 72 | #endif // #ifndef NDNBOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_HPP_INCLUDED |