Proton  1.1.1
Make porting easy from Python to C++11
atomic.hpp
1 #ifndef PROTON_ACTOMIC_HEADER
2 #define PROTON_ACTOMIC_HEADER
3 
4 namespace proton
5 {
6 
7 namespace detail
8 {
9 
10 class atomic_count
11 {
12 public:
13 
14  explicit atomic_count( long v ) : value_( v ) {}
15 
16  long operator++()
17  {
18  return atomic_exchange_and_add( &value_, +1 ) + 1;
19  }
20 
21  long operator--()
22  {
23  return atomic_exchange_and_add( &value_, -1 ) - 1;
24  }
25 
26  operator long() const
27  {
28  return atomic_exchange_and_add( &value_, 0 );
29  }
30 
31 private:
32 
33  atomic_count(atomic_count const &);
34  atomic_count & operator=(atomic_count const &);
35 
36  mutable long value_;
37 
38 private:
39 
40  static long atomic_inc( long * pw, long dv )
41  {
42  // int r = *pw;
43  // *pw += dv;
44  // return r;
45 
46  long r;
47 
48  __asm__ __volatile__
49  (
50  "lock\n\t"
51  "xadd %1, %0":
52  "+m"( *pw ), "=r"( r ): // outputs (%0, %1)
53  "1"( dv ): // inputs (%2 == %1)
54  "memory", "cc" // clobbers
55  );
56 
57  return r;
58  }
59 };
60 
61 } // namespace detail
62 
63 } // namespace proton
64 
65 #endif // PROTON_ACTOMIC_HEADER