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 #ifdef ENABLE_BOOST_ATOMIC_COUNT
32 #include <boost/smart_ptr/detail/atomic_count.hpp>
33 #endif
34 
35 namespace FIX
36 {
38 
39 #ifdef ENABLE_BOOST_ATOMIC_COUNT
40 
41 typedef boost::detail::atomic_count atomic_count;
42 
43 #elif _MSC_VER
44 
45  //atomic counter based on interlocked functions for Win32
46  class atomic_count
47  {
48  public:
49  explicit atomic_count( long v ): m_counter( v )
50  {
51  }
52 
53  long operator++()
54  {
55  return ::InterlockedIncrement( &m_counter );
56  }
57 
58  long operator--()
59  {
60  return ::InterlockedDecrement( &m_counter );
61  }
62 
63  operator long() const
64  {
65  return ::InterlockedExchangeAdd(const_cast<long volatile *>( &m_counter ), 0 );
66  }
67 
68  private:
69 
70  atomic_count( atomic_count const & );
71  atomic_count & operator=( atomic_count const & );
72 
73  long volatile m_counter;
74  };
75 
76 #elif defined(__SUNPRO_CC) || defined(__TOS_AIX__)
77 
78 // general purpose atomic counter using mutexes
79 class atomic_count
80 {
81 public:
82  explicit atomic_count( long v ): m_counter( v )
83  {
84  }
85 
86  long operator++()
87  {
88  Locker _lock(m_mutex);
89  return ++m_counter;
90  }
91 
92  long operator--()
93  {
94  Locker _lock(m_mutex);
95  return --m_counter;
96  }
97 
98  operator long() const
99  {
100  return static_cast<long const volatile &>( m_counter );
101  }
102 
103 private:
104 
105  atomic_count( atomic_count const & );
106  atomic_count & operator=( atomic_count const & );
107 
108  Mutex m_mutex;
109  long m_counter;
110 };
111 
112 #else
113 
114  //
115  // boost/detail/atomic_count_gcc_x86.hpp
116  //
117  // atomic_count for g++ on 486+/AMD64
118  //
119  // Copyright 2007 Peter Dimov
120  //
121  // Distributed under the Boost Software License, Version 1.0. (See
122  // accompanying file LICENSE_1_0.txt or copy at
123  // http://www.boost.org/LICENSE_1_0.txt)
124  //
125 
126  class atomic_count
127  {
128  public:
129 
130  explicit atomic_count( long v ) : value_(static_cast<int>(v)) {}
131 
132  long operator++()
133  {
134  return atomic_exchange_and_add( &value_, 1 ) + 1;
135  }
136 
137  long operator--()
138  {
139  return atomic_exchange_and_add( &value_, -1 ) - 1;
140  }
141 
142  operator long() const
143  {
144  return atomic_exchange_and_add( &value_, 0 );
145  }
146 
147  private:
148 
149  atomic_count( atomic_count const & );
150  atomic_count & operator=( atomic_count const & );
151 
152  mutable int value_;
153 
154  private:
155 
156  static int atomic_exchange_and_add(int * pw, int dv)
157  {
158  // int r = *pw;
159  // *pw += dv;
160  // return r;
161 
162  int r;
163 
164  __asm__ __volatile__
165  (
166  "lock\n\t"
167  "xadd %1, %0":
168  "+m"(*pw), "=r"(r) : // outputs (%0, %1)
169  "1"(dv) : // inputs (%2 == %1)
170  "memory", "cc" // clobbers
171  );
172 
173  return r;
174  }
175  };
176 
177 #endif
178 
179 }
180 
181 #endif
182 
FIX::atomic_count::value_
int value_
Definition: AtomicCount.h:186
FIX::atomic_count
Atomic count class - consider using interlocked functions.
Definition: AtomicCount.h:143
FIX::atomic_count::atomic_exchange_and_add
static int atomic_exchange_and_add(int *pw, int dv)
Definition: AtomicCount.h:190
FIX::atomic_count::atomic_count
atomic_count(long v)
Definition: AtomicCount.h:164
Mutex.h
FIX::atomic_count::operator++
long operator++()
Definition: AtomicCount.h:166
FIX::atomic_count::operator=
atomic_count & operator=(atomic_count const &)
FIX
Definition: Acceptor.cpp:34
FIX::atomic_count::operator--
long operator--()
Definition: AtomicCount.h:171
Utility.h

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001