TAtomicCountPthread.h

Go to the documentation of this file.
00001 // @(#)root/thread:$Id: TAtomicCountPthread.h 20882 2007-11-19 11:31:26Z 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_TAtomicCountPthread
00013 #define ROOT_TAtomicCountPthread
00014 
00015 //////////////////////////////////////////////////////////////////////////
00016 //                                                                      //
00017 // TAtomicCountPthread                                                  //
00018 //                                                                      //
00019 // Class providing atomic operations on a long. Setting, getting,       //
00020 // incrementing and decrementing are atomic, thread safe, operations.   //
00021 //                                                                      //
00022 // This implementation uses pthread mutexes for locking. This clearly   //
00023 // is less efficient than the version using asm locking instructions    //
00024 // as in TAtomicCountGcc.h, but better than nothing.                    //
00025 //                                                                      //
00026 // ATTENTION: Don't use this file directly, it is included by           //
00027 //            TAtomicCount.h.                                           //
00028 //                                                                      //
00029 //////////////////////////////////////////////////////////////////////////
00030 
00031 #include <pthread.h>
00032 
00033 class TAtomicCount {
00034 private:
00035    Long_t                  fCnt;     // counter
00036    mutable pthread_mutex_t fMutex;   // mutex used to lock counter
00037 
00038    TAtomicCount(const TAtomicCount &);             // not implemented
00039    TAtomicCount &operator=(const TAtomicCount &);  // not implemented
00040 
00041    class LockGuard {
00042    private:
00043       pthread_mutex_t &fM;  // mutex to be guarded
00044    public:
00045       LockGuard(pthread_mutex_t &m): fM(m) { pthread_mutex_lock(&fM); }
00046       ~LockGuard() { pthread_mutex_unlock(&fM); }
00047    };
00048 
00049 public:
00050    explicit TAtomicCount(Long_t v): fCnt(v) {
00051       pthread_mutex_init(&fMutex, 0);
00052    }
00053 
00054    ~TAtomicCount() { pthread_mutex_destroy(&fMutex); }
00055 
00056    void operator++() {
00057       LockGuard lock(fMutex);
00058       ++fCnt;
00059    }
00060 
00061    Long_t operator--() {
00062       LockGuard lock(fMutex);
00063       return --fCnt;
00064    }
00065 
00066    operator long() const {
00067       LockGuard lock(fMutex);
00068       return fCnt;
00069    }
00070 
00071    void Set(Long_t v) {
00072       LockGuard lock(fMutex);
00073       fCnt = v;
00074    }
00075 
00076    Long_t Get() const {
00077       LockGuard lock(fMutex);
00078       return fCnt;
00079    }
00080  };
00081 
00082 #endif

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