OgreAtomicWrappers.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2012 Torus Knot Software Ltd
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 -----------------------------------------------------------------------------
00027 */
00028 #ifndef _AtomicWrapper_H__
00029 #define _AtomicWrapper_H__
00030 
00031 #include <signal.h>
00032 #include "OgrePrerequisites.h"
00033 #include "OgreException.h"
00034 
00035 namespace Ogre {
00036 
00043     template <class T> class AtomicObject {
00044 
00045         public:
00046 
00047         AtomicObject (const T &initial)
00048             : mField(initial)
00049         {   }
00050 
00051         AtomicObject (const AtomicObject<T> &cousin)
00052             : mField(cousin.get())
00053         {   }
00054 
00055         AtomicObject ()
00056         {   }
00057 
00058         void operator= (const AtomicObject<T> &cousin)
00059         {
00060             set(cousin.get());
00061         }
00062 
00063         T get (void) const
00064         {
00065             OGRE_LOCK_AUTO_MUTEX
00066             return mField;
00067         }
00068 
00069         void set (const T &v)
00070         {
00071             OGRE_LOCK_AUTO_MUTEX
00072             mField = v;
00073         }
00074 
00075         bool cas (const T &old, const T &nu)
00076         {
00077             OGRE_LOCK_AUTO_MUTEX
00078             if (mField != old) return false;
00079             mField = nu;
00080             return true;
00081         }
00082 
00083         T operator++ (void)
00084         {
00085             OGRE_LOCK_AUTO_MUTEX
00086             return ++mField;
00087         }
00088 
00089         T operator++ (int)
00090         {
00091             OGRE_LOCK_AUTO_MUTEX
00092             return mField++;
00093         }
00094 
00095         T operator-- (int)
00096         {
00097             OGRE_LOCK_AUTO_MUTEX
00098             return mField--;
00099         }
00100 
00101         T operator+=(const T &add)
00102         {
00103             OGRE_LOCK_AUTO_MUTEX
00104             mField += add;
00105             return mField;
00106         }
00107 
00108         protected:
00109 
00110         OGRE_AUTO_MUTEX
00111 
00112         volatile T mField;
00113 
00114     };
00118 }
00119 
00120 #if (((OGRE_COMPILER == OGRE_COMPILER_GNUC) && (OGRE_COMP_VER >= 412)) || (OGRE_COMPILER == OGRE_COMPILER_CLANG)) && OGRE_THREAD_SUPPORT
00121 
00122 namespace Ogre {
00123 
00130     template<class T> class AtomicScalar
00131     {
00132 
00133         public:
00134 
00135         AtomicScalar (const T &initial)
00136             : mField(initial)
00137         {   }
00138 
00139         AtomicScalar (const AtomicScalar<T> &cousin)
00140             : mField(cousin.mField)
00141         {   }
00142 
00143         AtomicScalar () 
00144         {   }
00145 
00146         void operator= (const AtomicScalar<T> &cousin)
00147         {
00148             mField = cousin.mField;
00149         }
00150 
00151         T get (void) const
00152         {
00153             return mField;
00154         }
00155 
00156         void set (const T &v)
00157         {
00158             mField = v; 
00159         }   
00160 
00161         bool cas (const T &old, const T &nu)
00162         {
00163             return __sync_bool_compare_and_swap (&mField, old, nu);
00164         }
00165             
00166         T operator++ (void)
00167         {
00168             __sync_add_and_fetch (&mField, 1);
00169         }
00170             
00171         T operator-- (void)
00172         {
00173             __sync_add_and_fetch (&mField, -1);
00174         }
00175 
00176         T operator++ (int)
00177         {
00178             __sync_fetch_and_add (&mField, 1);
00179         }
00180             
00181         T operator-- (int)
00182         {
00183             __sync_fetch_and_add (&mField, -1);
00184         }
00185 
00186         T operator+=(const T &add)
00187         {
00188             return __sync_add_and_fetch (&mField, add);
00189         }
00190 
00191         volatile T mField;
00192 
00193     };
00197 }
00198 
00199  #elif OGRE_COMPILER == OGRE_COMPILER_MSVC && OGRE_COMP_VER >= 1400 && OGRE_THREAD_SUPPORT
00200 
00201 #ifndef WIN32_LEAN_AND_MEAN
00202 #  define WIN32_LEAN_AND_MEAN
00203 #endif
00204 #if !defined(NOMINMAX) && defined(_MSC_VER)
00205 #   define NOMINMAX // required to stop windows.h messing up std::min
00206 #endif
00207 #include <windows.h>
00208 #include <intrin.h>
00209 
00210 namespace Ogre {
00211 
00218     template<class T> class AtomicScalar
00219     {
00220 
00221         public:
00222 
00223         AtomicScalar (const T &initial)
00224             : mField(initial)
00225         {   }
00226 
00227         AtomicScalar (const AtomicScalar<T> &cousin)
00228             : mField(cousin.mField)
00229         {   }
00230 
00231         AtomicScalar () 
00232         {   }
00233 
00234         void operator= (const AtomicScalar<T> &cousin)
00235         {
00236             mField = cousin.mField;
00237         }
00238 
00239         T get (void) const
00240         {
00241             return mField;
00242         }
00243 
00244         void set (const T &v)
00245         {
00246             mField = v;
00247         }   
00248 
00249         bool cas (const T &old, const T &nu)
00250         {
00251             if (sizeof(T)==2) {
00252                 return _InterlockedCompareExchange16((SHORT*)&mField, static_cast<SHORT>(nu), static_cast<SHORT>(old)) == static_cast<SHORT>(old);
00253             } 
00254             else if (sizeof(T)==4) 
00255             {
00256                 return _InterlockedCompareExchange((LONG*)&mField, static_cast<LONG>(nu), static_cast<LONG>(old)) == static_cast<LONG>(old);
00257             } 
00258             else if (sizeof(T)==8) {
00259                 return _InterlockedCompareExchange64((LONGLONG*)&mField, static_cast<LONGLONG>(nu), static_cast<LONGLONG>(old)) == static_cast<LONGLONG>(old);
00260             } 
00261             else {
00262                 OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,"Only 16, 32, and 64 bit scalars supported in win32.","AtomicScalar::cas");
00263             }
00264         }
00265             
00266         T operator++ (void)
00267         {
00268             if (sizeof(T)==2) {
00269                 return InterlockedIncrement16((SHORT*)&mField);
00270             } else if (sizeof(T)==4) {
00271                 return InterlockedIncrement((LONG*)&mField);
00272             } else if (sizeof(T)==8) {
00273                 return InterlockedIncrement64((LONGLONG*)&mField);
00274             } else {
00275                 OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,"Only 16, 32, and 64 bit scalars supported in win32.","AtomicScalar::operator++(prefix)");
00276             }
00277         }
00278             
00279         T operator-- (void)
00280         {
00281             if (sizeof(T)==2) {
00282                 return InterlockedDecrement16((SHORT*)&mField);
00283             } else if (sizeof(T)==4) {
00284                 return InterlockedDecrement((LONG*)&mField);
00285             } else if (sizeof(T)==8) {
00286                 return InterlockedDecrement64((LONGLONG*)&mField);
00287             } else {
00288                 OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,"Only 16, 32, and 64 bit scalars supported in win32.","AtomicScalar::operator--(prefix)");
00289             }
00290         }
00291 
00292         T operator++ (int)
00293         {
00294             if (sizeof(T)==2) {
00295                 return InterlockedIncrement16((SHORT*)&mField)-1;
00296             } else if (sizeof(T)==4) {
00297                 return InterlockedIncrement((LONG*)&mField)-1;
00298             } else if (sizeof(T)==8) {
00299                 return InterlockedIncrement64((LONGLONG*)&mField)-1;
00300             } else {
00301                 OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,"Only 16, 32, and 64 bit scalars supported in win32.","AtomicScalar::operator++(postfix)");
00302             }
00303         }
00304             
00305         T operator-- (int)
00306         {
00307             if (sizeof(T)==2) {
00308                 return InterlockedDecrement16((SHORT*)&mField)+1;
00309             } else if (sizeof(T)==4) {
00310                 return InterlockedDecrement((LONG*)&mField)+1;
00311             } else if (sizeof(T)==8) {
00312                 return InterlockedDecrement64((LONGLONG*)&mField)+1;
00313             } else {
00314                 OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,"Only 16, 32, and 64 bit scalars supported in win32.","AtomicScalar::operator--(postfix)");
00315             }
00316         }
00317 
00318         T operator+=(const T &add)
00319         {
00320             //The function InterlockedExchangeAdd is not available for 64 and 16 bit version
00321             //We will use the cas operation instead. 
00322             T newVal;
00323             do {
00324                 //Create a value of the current field plus the added value
00325                 newVal = mField + add;
00326                 //Replace the current field value with the new value. Ensure that the value 
00327                 //of the field hasn't changed in the mean time by comparing it to the new value
00328                 //minus the added value. 
00329             } while (!cas(newVal - add, newVal)); //repeat until successful
00330             return newVal;
00331         }
00332 
00333         volatile T mField;
00334 
00335     };
00339 }
00340 
00341 #else
00342 
00343 namespace Ogre {
00344 
00351     template <class T> class AtomicScalar {
00352 
00353         public:
00354 
00355         AtomicScalar (const T &initial)
00356             : mField(initial)
00357         {   }
00358 
00359         AtomicScalar (const AtomicScalar<T> &cousin)
00360             : mField(cousin.mField)
00361         {   }
00362 
00363         AtomicScalar ()
00364         {   }
00365 
00366         void operator= (const AtomicScalar<T> &cousin)
00367         {
00368             OGRE_LOCK_AUTO_MUTEX
00369             mField = cousin.mField;
00370         }
00371 
00372         T get (void) const
00373         {
00374             // no lock required here
00375             // since get will not interfere with set or cas
00376             // we may get a stale value, but this is ok
00377             return mField;
00378         }
00379 
00380         void set (const T &v)
00381         {
00382             OGRE_LOCK_AUTO_MUTEX
00383             mField = v;
00384         }
00385 
00386         bool cas (const T &old, const T &nu)
00387         {
00388             OGRE_LOCK_AUTO_MUTEX
00389             if (mField != old) return false;
00390             mField = nu;
00391             return true;
00392         }
00393 
00394         T operator++ (void)
00395         {
00396             OGRE_LOCK_AUTO_MUTEX
00397             return ++mField;
00398         }
00399 
00400         T operator-- (void)
00401         {
00402             OGRE_LOCK_AUTO_MUTEX
00403             return --mField;
00404         }
00405 
00406         T operator++ (int)
00407         {
00408             OGRE_LOCK_AUTO_MUTEX
00409             return mField++;
00410         }
00411 
00412         T operator-- (int)
00413         {
00414             OGRE_LOCK_AUTO_MUTEX
00415             return mField--;
00416         }
00417 
00418         T operator+=(const T &add)
00419         {
00420             OGRE_LOCK_AUTO_MUTEX
00421             mField += add;
00422             return mField;
00423         }
00424 
00425         protected:
00426 
00427         OGRE_AUTO_MUTEX
00428 
00429         volatile T mField;
00430 
00431     };
00435 }
00436 
00437 #endif
00438 
00439 
00440 #endif
00441 

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Fri May 25 23:36:23 2012