00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : AtomicCounter 00006 * Package : Zeus.ZeusBase.System 00007 * Author : Benjamin Hadorn 00008 * Date : 27.12.2011 00009 * System : Zeus-Framework 00010 ***************************************************************************** 00011 * Licence: * 00012 * This library is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU Lesser General Public License as * 00014 * published by the Free Software Foundation; either version * 00015 * 2.1 of the License, or (at your option) any later version. * 00016 * * 00017 * This library is distributed in the hope that it will be useful, * 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00020 * GNU Lesser General Public License for more details. * 00021 * * 00022 * You should have received a copy of the GNU Lesser General Public * 00023 * License along with this library; if not, write to the Free Software * 00024 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00025 *****************************************************************************/ 00026 00027 /***************************************************************************** 00028 * Changes: 00029 * 27.12.2011 bha: created zeus 2.0 00030 *****************************************************************************/ 00031 00032 #ifndef AtomicCounterHPP 00033 #define AtomicCounterHPP 00034 00035 #include <zeusbase/System/CriticalScopeLock.hpp> 00036 #include <zeusbase/System/CriticalSection.h> 00037 00038 #if defined(ENABLE_WIN32_BINDING) 00039 #if defined(USE_WINDOWS_H) 00040 #include <windows.h> 00041 #endif 00042 #endif 00043 00044 BEGIN_NAMESPACE_Zeus 00045 00046 /***************************************************************************/ 00051 /***************************************************************************/ 00052 class TAtomicCounter 00053 { 00054 public: 00055 /***********************************************************************/ 00058 /***********************************************************************/ 00059 inline TAtomicCounter() 00060 : m_rLock(*new TCriticalSection(TCriticalSection::etRecursive)), 00061 m_lValue(0) 00062 {} 00063 00064 /***********************************************************************/ 00068 /***********************************************************************/ 00069 inline TAtomicCounter(Int32 lValue) 00070 : m_rLock(*new TCriticalSection(TCriticalSection::etRecursive)), 00071 m_lValue(lValue) 00072 {} 00073 00074 /***********************************************************************/ 00077 /***********************************************************************/ 00078 inline TAtomicCounter(const TAtomicCounter& rValue) 00079 : m_rLock(*new TCriticalSection(TCriticalSection::etRecursive)), 00080 m_lValue(0) 00081 { 00082 *this = rValue; 00083 } 00084 00085 /***********************************************************************/ 00088 /***********************************************************************/ 00089 inline virtual ~TAtomicCounter() 00090 { 00091 m_rLock.release(); 00092 } 00093 00094 /**************************************************************************/ 00097 /**************************************************************************/ 00098 inline Int32 getAndDecrement() 00099 { 00101 // LOCK 00102 TCriticalScopeLock Lock(m_rLock); 00103 Int32 lRetval = m_lValue; 00104 decrementAndGet(); 00105 return lRetval; 00107 } 00108 00109 /**************************************************************************/ 00112 /**************************************************************************/ 00113 inline Int32 getAndIncrement() 00114 { 00116 // LOCK 00117 TCriticalScopeLock Lock(m_rLock); 00118 Int32 lRetval = m_lValue; 00119 incrementAndGet(); 00120 return lRetval; 00122 } 00123 /***********************************************************************/ 00126 /***********************************************************************/ 00127 inline Int32 incrementAndGet() 00128 { 00129 #if defined(ENABLE_WIN32_BINDING) 00130 return InterlockedIncrement(const_cast<Int32*>(&m_lValue)); 00131 #else 00132 TCriticalScopeLock Lock(m_rLock); 00133 ++m_lValue; 00134 return m_lValue; 00135 #endif 00136 } 00137 00138 /***********************************************************************/ 00141 /***********************************************************************/ 00142 inline Int32 decrementAndGet() 00143 { 00144 #if defined(ENABLE_WIN32_BINDING) 00145 return InterlockedDecrement(const_cast<Int32*>(&m_lValue)); 00146 #else 00147 TCriticalScopeLock Lock(m_rLock); 00148 --m_lValue; 00149 return m_lValue; 00150 #endif 00151 } 00152 00153 /***********************************************************************/ 00156 /***********************************************************************/ 00157 inline operator Int32() const 00158 { 00159 TCriticalScopeLock Lock(m_rLock); 00160 return m_lValue; 00161 } 00162 00163 /***********************************************************************/ 00166 /***********************************************************************/ 00167 inline Int32 operator++() { return incrementAndGet(); } 00168 00169 /***********************************************************************/ 00172 /***********************************************************************/ 00173 inline Int32 operator--() { return decrementAndGet(); } 00174 00175 /***********************************************************************/ 00178 /***********************************************************************/ 00179 inline Int32 operator++(int) { return getAndIncrement(); } 00180 00181 /***********************************************************************/ 00184 /***********************************************************************/ 00185 inline Int32 operator--(int) { return getAndDecrement(); } 00186 00187 /***********************************************************************/ 00190 /***********************************************************************/ 00191 inline TAtomicCounter& operator =(Int32 lInVal) 00192 { 00193 #if defined(ENABLE_WIN32_BINDING) 00194 LPLONG pDestination = const_cast<LPLONG>(&m_lValue); 00195 InterlockedExchange(pDestination, lInVal); 00196 #else 00197 00198 // LOCK 00199 TCriticalScopeLock Lock(m_rLock); 00200 m_lValue = lInVal; 00202 #endif 00203 return *this; 00204 } 00205 00206 /***********************************************************************/ 00209 /***********************************************************************/ 00210 inline TAtomicCounter& operator =(const TAtomicCounter& rInVal) 00211 { 00213 // LOCK 00214 m_rLock.enter(); 00215 rInVal.m_rLock.enter(); 00216 00217 m_lValue = rInVal.m_lValue; 00218 00219 rInVal.m_rLock.leave(); 00220 m_rLock.leave(); 00222 return *this; 00223 } 00224 00225 protected: 00227 Int32 m_lValue; 00229 TCriticalSection& m_rLock; 00230 00231 private: 00232 00233 }; 00234 00235 END_NAMESPACE_Zeus 00236 00237 #endif