AtomicCount.h
Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 
00003 /****************************************************************************
00004 ** Copyright (c) 2001-2014
00005 **
00006 ** This file is part of the QuickFIX FIX Engine
00007 **
00008 ** This file may be distributed under the terms of the quickfixengine.org
00009 ** license as defined by quickfixengine.org and appearing in the file
00010 ** LICENSE included in the packaging of this file.
00011 **
00012 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014 **
00015 ** See http://www.quickfixengine.org/LICENSE for licensing information.
00016 **
00017 ** Contact ask@quickfixengine.org if any conditions of this licensing are
00018 ** not clear to you.
00019 **
00020 ****************************************************************************/
00021 
00022 #ifndef ATOMIC_COUNT
00023 #define ATOMIC_COUNT
00024 
00025 #include "Mutex.h"
00026 
00027 namespace FIX
00028 {
00030 
00031 #ifdef ENABLE_BOOST_ATOMIC_COUNT
00032 
00033 #include <boost/smart_ptr/detail/atomic_count.hpp>
00034 typedef boost::detail::atomic_count atomic_count;
00035 
00036 #elif _MSC_VER 
00037 
00038   //atomic counter based on interlocked functions for Win32
00039   class atomic_count
00040   {
00041   public:
00042     explicit atomic_count( long v ): m_counter( v )
00043     {
00044     }
00045 
00046     long operator++()
00047     {
00048       return ::InterlockedIncrement( &m_counter );
00049     }
00050 
00051     long operator--()
00052     {
00053       return ::InterlockedDecrement( &m_counter );
00054     }
00055 
00056     operator long() const
00057     {
00058       return static_cast<long const volatile &>( m_counter );
00059     }
00060 
00061   private:
00062 
00063     atomic_count( atomic_count const & );
00064     atomic_count & operator=( atomic_count const & );
00065 
00066     long volatile m_counter;
00067   };
00068 
00069 #else
00070   // general purpose atomic counter using mutexes
00071   class atomic_count
00072   {
00073   public:
00074     explicit atomic_count( long v ): m_counter( v )
00075     {
00076     }
00077 
00078     long operator++()
00079     {
00080       Locker _lock(m_mutex);
00081       return ++m_counter;
00082     }
00083 
00084     long operator--()
00085     {
00086       Locker _lock(m_mutex);
00087       return --m_counter;
00088     }
00089 
00090     operator long() const
00091     {
00092       return static_cast<long const volatile &>( m_counter );
00093     }
00094 
00095   private:
00096 
00097     atomic_count( atomic_count const & );
00098     atomic_count & operator=( atomic_count const & );
00099 
00100     Mutex m_mutex;
00101     long m_counter;
00102   };
00103 
00104 #endif
00105 
00106 }
00107 
00108 #endif
00109 

Generated on Mon Sep 15 2014 01:23:54 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001