TAtomicCountGcc.h

Go to the documentation of this file.
00001 // @(#)root/thread:$Id: TAtomicCountGcc.h 34271 2010-07-01 10:10:52Z rdm $
00002 // Author: Fons Rademakers   14/11/06
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TAtomicCountGcc
00013 #define ROOT_TAtomicCountGcc
00014 
00015 
00016 //////////////////////////////////////////////////////////////////////////
00017 //                                                                      //
00018 // TAtomicCountGcc                                                      //
00019 //                                                                      //
00020 // Class providing atomic operations on a long. Setting, getting,       //
00021 // incrementing and decrementing are atomic, thread safe, operations.   //
00022 //                                                                      //
00023 // This implementation uses GNU libstdc++ v3 atomic primitives, see     //
00024 // http://gcc.gnu.org/onlinedocs/porting/Thread-safety.html.            //
00025 //                                                                      //
00026 // ATTENTION: Don't use this file directly, it is included by           //
00027 //            TAtomicCount.h.                                           //
00028 //                                                                      //
00029 //////////////////////////////////////////////////////////////////////////
00030 
00031 #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 2) || \
00032     (defined(__APPLE_CC__) && __APPLE_CC__ > 5000  && !defined(MAC_OS_X_VERSION_10_6))
00033 #include <bits/atomicity.h>
00034 #else
00035 #include <ext/atomicity.h>
00036 #endif
00037 
00038 #if defined(__GLIBCXX__) // g++ 3.4+
00039 
00040 using __gnu_cxx::__atomic_add;
00041 using __gnu_cxx::__exchange_and_add;
00042 
00043 #endif
00044 
00045 
00046 class TAtomicCount {
00047 private:
00048    mutable _Atomic_word fCnt;   // counter
00049 
00050    TAtomicCount(const TAtomicCount &);             // not implemented
00051    TAtomicCount &operator=(const TAtomicCount &);  // not implemented
00052 
00053 public:
00054    explicit TAtomicCount(Long_t v) : fCnt(v) { }
00055    void operator++() { __atomic_add(&fCnt, 1); }
00056    Long_t operator--() { return __exchange_and_add(&fCnt, -1) - 1; }
00057    operator long() const { return __exchange_and_add(&fCnt, 0); }
00058    void Set(Long_t v) {
00059       fCnt = v;
00060 #ifdef _GLIBCXX_WRITE_MEM_BARRIER
00061 #if !(defined(__INTEL_COMPILER) && defined(__ia64__)) //ICC doesn't support inline asm on IA-64
00062       _GLIBCXX_WRITE_MEM_BARRIER;
00063 #endif
00064 #endif
00065    }
00066    Long_t Get() const { return __exchange_and_add(&fCnt, 0); }
00067 };
00068 
00069 #endif

Generated on Tue Jul 5 14:11:59 2011 for ROOT_528-00b_version by  doxygen 1.5.1