AtomicCount.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 
3 /****************************************************************************
4 ** Copyright (c) 2001-2014
5 **
6 ** This file is part of the QuickFIX FIX Engine
7 **
8 ** This file may be distributed under the terms of the quickfixengine.org
9 ** license as defined by quickfixengine.org and appearing in the file
10 ** LICENSE included in the packaging of this file.
11 **
12 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
13 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14 **
15 ** See http://www.quickfixengine.org/LICENSE for licensing information.
16 **
17 ** Contact ask@quickfixengine.org if any conditions of this licensing are
18 ** not clear to you.
19 **
20 ****************************************************************************/
21 
22 #ifndef ATOMIC_COUNT
23 #define ATOMIC_COUNT
24 
25 #include "Utility.h"
26 
27 #if defined(__SUNPRO_CC) || defined(__TOS_AIX__)
28 #include "Mutex.h"
29 #endif
30 
31 namespace FIX
32 {
34 
35 #ifdef ENABLE_BOOST_ATOMIC_COUNT
36 
37 #include <boost/smart_ptr/detail/atomic_count.hpp>
38 typedef boost::detail::atomic_count atomic_count;
39 
40 #elif _MSC_VER
41 
42  //atomic counter based on interlocked functions for Win32
43  class atomic_count
44  {
45  public:
46  explicit atomic_count( long v ): m_counter( v )
47  {
48  }
49 
50  long operator++()
51  {
52  return ::InterlockedIncrement( &m_counter );
53  }
54 
55  long operator--()
56  {
57  return ::InterlockedDecrement( &m_counter );
58  }
59 
60  operator long() const
61  {
62  return ::InterlockedExchangeAdd(const_cast<long volatile *>( &m_counter ), 0 );
63  }
64 
65  private:
66 
67  atomic_count( atomic_count const & );
68  atomic_count & operator=( atomic_count const & );
69 
70  long volatile m_counter;
71  };
72 
73 #elif defined(__SUNPRO_CC) || defined(__TOS_AIX__)
74 
75 // general purpose atomic counter using mutexes
76 class atomic_count
77 {
78 public:
79  explicit atomic_count( long v ): m_counter( v )
80  {
81  }
82 
83  long operator++()
84  {
85  Locker _lock(m_mutex);
86  return ++m_counter;
87  }
88 
89  long operator--()
90  {
91  Locker _lock(m_mutex);
92  return --m_counter;
93  }
94 
95  operator long() const
96  {
97  return static_cast<long const volatile &>( m_counter );
98  }
99 
100 private:
101 
102  atomic_count( atomic_count const & );
103  atomic_count & operator=( atomic_count const & );
104 
105  Mutex m_mutex;
106  long m_counter;
107 };
108 
109 #else
110 
111  //
112  // boost/detail/atomic_count_gcc_x86.hpp
113  //
114  // atomic_count for g++ on 486+/AMD64
115  //
116  // Copyright 2007 Peter Dimov
117  //
118  // Distributed under the Boost Software License, Version 1.0. (See
119  // accompanying file LICENSE_1_0.txt or copy at
120  // http://www.boost.org/LICENSE_1_0.txt)
121  //
122 
123  class atomic_count
124  {
125  public:
126 
127  explicit atomic_count( long v ) : value_(static_cast<int>(v)) {}
128 
129  long operator++()
130  {
131  return atomic_exchange_and_add( &value_, 1 ) + 1;
132  }
133 
134  long operator--()
135  {
136  return atomic_exchange_and_add( &value_, -1 ) - 1;
137  }
138 
139  operator long() const
140  {
141  return atomic_exchange_and_add( &value_, 0 );
142  }
143 
144  private:
145 
146  atomic_count( atomic_count const & );
147  atomic_count & operator=( atomic_count const & );
148 
149  mutable int value_;
150 
151  private:
152 
153  static int atomic_exchange_and_add(int * pw, int dv)
154  {
155  // int r = *pw;
156  // *pw += dv;
157  // return r;
158 
159  int r;
160 
161  __asm__ __volatile__
162  (
163  "lock\n\t"
164  "xadd %1, %0":
165  "+m"(*pw), "=r"(r) : // outputs (%0, %1)
166  "1"(dv) : // inputs (%2 == %1)
167  "memory", "cc" // clobbers
168  );
169 
170  return r;
171  }
172  };
173 
174 #endif
175 
176 }
177 
178 #endif
179 
atomic_count(long v)
Definition: AtomicCount.h:127
static int atomic_exchange_and_add(int *pw, int dv)
Definition: AtomicCount.h:153
Definition: Acceptor.cpp:34
atomic_count & operator=(atomic_count const &)

Generated on Sun Apr 15 2018 21:44:00 for QuickFIX by doxygen 1.8.13 written by Dimitri van Heesch, © 1997-2001