00001 /* atomicCounter.h 00002 */ 00003 #ifndef OSL_ATOMICCOUNTER_H 00004 #define OSL_ATOMICCOUNTER_H 00005 00006 #include "osl/misc/lightMutex.h" 00007 #include <algorithm> 00008 00009 namespace osl 00010 { 00011 namespace misc 00012 { 00013 class AtomicCounter 00014 { 00015 typedef LightMutex Mutex; 00016 00017 mutable Mutex m; 00018 int count; 00019 public: 00020 explicit AtomicCounter(int count=0) :count(count){} 00021 void inc(){ 00022 Mutex::scoped_lock lk(m); 00023 count++; 00024 } 00025 int valueAndinc(){ 00026 Mutex::scoped_lock lk(m); 00027 return count++; 00028 } 00029 void dec(){ 00030 Mutex::scoped_lock lk(m); 00031 count--; 00032 } 00033 void max(int val){ 00034 Mutex::scoped_lock lk(m); 00035 count=std::max(count,val); 00036 } 00037 int value() const{ 00038 Mutex::scoped_lock lk(m); 00039 return count; 00040 } 00041 void setValue(int value) { 00042 Mutex::scoped_lock lk(m); 00043 count = value; 00044 } 00045 struct IncLock 00046 { 00047 AtomicCounter& counter; 00048 explicit IncLock(AtomicCounter& c) : counter(c) 00049 { 00050 counter.inc(); 00051 } 00052 ~IncLock() 00053 { 00054 counter.dec(); 00055 } 00056 }; 00057 }; 00058 } 00059 using misc::AtomicCounter; 00060 } 00061 00062 #endif /* OSL_ATOMICCOUNTER_H */ 00063 // ;;; Local Variables: 00064 // ;;; mode:c++ 00065 // ;;; c-basic-offset:2 00066 // ;;; End: 00067