blob: 04b1e1da73b7787bc31c2ae9609dfda2ac7142d4 [file] [log] [blame]
Jeff Thompson3d613fd2013-10-15 15:39:04 -07001#ifndef NDNBOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_X86_HPP_INCLUDED
2#define NDNBOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_X86_HPP_INCLUDED
Jeff Thompsonf7d49942013-08-01 16:47:40 -07003
4//
Jeff Thompson9939dcd2013-10-15 15:12:24 -07005// ndnboost/detail/atomic_count_gcc_x86.hpp
Jeff Thompsonf7d49942013-08-01 16:47:40 -07006//
7// atomic_count for g++ on 486+/AMD64
8//
9// Copyright 2007 Peter Dimov
10//
11// Distributed under the Boost Software License, Version 1.0. (See
12// accompanying file LICENSE_1_0.txt or copy at
13// http://www.boost.org/LICENSE_1_0.txt)
14//
15
16namespace ndnboost
17{
18
19namespace detail
20{
21
22class atomic_count
23{
24public:
25
26 explicit atomic_count( long v ) : value_( static_cast< int >( v ) ) {}
27
28 long operator++()
29 {
30 return atomic_exchange_and_add( &value_, +1 ) + 1;
31 }
32
33 long operator--()
34 {
35 return atomic_exchange_and_add( &value_, -1 ) - 1;
36 }
37
38 operator long() const
39 {
40 return atomic_exchange_and_add( &value_, 0 );
41 }
42
43private:
44
45 atomic_count(atomic_count const &);
46 atomic_count & operator=(atomic_count const &);
47
48 mutable int value_;
49
50private:
51
52 static int atomic_exchange_and_add( int * pw, int dv )
53 {
54 // int r = *pw;
55 // *pw += dv;
56 // return r;
57
58 int r;
59
60 __asm__ __volatile__
61 (
62 "lock\n\t"
63 "xadd %1, %0":
64 "+m"( *pw ), "=r"( r ): // outputs (%0, %1)
65 "1"( dv ): // inputs (%2 == %1)
66 "memory", "cc" // clobbers
67 );
68
69 return r;
70 }
71};
72
73} // namespace detail
74
75} // namespace ndnboost
76
Jeff Thompson3d613fd2013-10-15 15:39:04 -070077#endif // #ifndef NDNBOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_X86_HPP_INCLUDED