00001 /* atomicCounter.h 00002 */ 00003 #ifndef OSL_ATOMICCOUNTER_H 00004 #define OSL_ATOMICCOUNTER_H 00005 00006 #include "osl/config.h" 00007 #ifdef USE_TBB_ATOMIC 00008 # include <tbb/atomic.h> 00009 #else 00010 # include "osl/misc/lightMutex.h" 00011 #endif 00012 #include <algorithm> 00013 00014 namespace osl 00015 { 00016 namespace misc 00017 { 00018 template <class Counter> 00019 struct IncrementLock 00020 { 00021 Counter& counter; 00022 explicit IncrementLock(Counter& c) : counter(c) 00023 { 00024 counter.inc(); 00025 } 00026 ~IncrementLock() 00027 { 00028 counter.dec(); 00029 } 00030 }; 00031 #ifdef USE_TBB_ATOMIC 00032 class AtomicCounter 00033 { 00034 tbb::atomic<int> count; 00035 public: 00036 explicit AtomicCounter(int count_=0) { 00037 this->count=count_; 00038 } 00039 void inc(){ 00040 count.fetch_and_increment(); 00041 } 00042 void inc(int value){ 00043 count.fetch_and_add(value); 00044 } 00045 int valueAndinc(){ 00046 return count.fetch_and_increment(); 00047 } 00048 void dec(){ 00049 count.fetch_and_decrement(); 00050 } 00051 void max(int val){ 00052 int x=count; 00053 if(x<val){ 00054 int oldx; 00055 while((oldx=count.compare_and_swap(val,x))!=x){ 00056 x=oldx; 00057 if(x>=val) break; 00058 } 00059 } 00060 } 00061 int value() const{ 00062 return count; 00063 } 00064 void setValue(int value) { 00065 count = value; 00066 } 00067 typedef IncrementLock<AtomicCounter> IncLock; 00068 }; 00069 #else 00070 class AtomicCounter 00071 { 00072 typedef LightMutex Mutex; 00073 mutable Mutex m; 00074 int count; 00075 public: 00076 explicit AtomicCounter(int count=0) :count(count){} 00077 void inc(){ 00078 SCOPED_LOCK(lk,m); 00079 count++; 00080 } 00081 int valueAndinc(){ 00082 SCOPED_LOCK(lk,m); 00083 return count++; 00084 } 00085 void dec(){ 00086 SCOPED_LOCK(lk,m); 00087 count--; 00088 } 00089 void max(int val){ 00090 SCOPED_LOCK(lk,m); 00091 count=std::max(count,val); 00092 } 00093 int value() const{ 00094 SCOPED_LOCK(lk,m); 00095 return count; 00096 } 00097 void setValue(int value) { 00098 SCOPED_LOCK(lk,m); 00099 count = value; 00100 } 00101 typedef IncrementLock<AtomicCounter> IncLock; 00102 }; 00103 #endif 00104 } 00105 using misc::AtomicCounter; 00106 } 00107 00108 #endif /* OSL_ATOMICCOUNTER_H */ 00109 // ;;; Local Variables: 00110 // ;;; mode:c++ 00111 // ;;; c-basic-offset:2 00112 // ;;; End: 00113