atomiccounter.h
Go to the documentation of this file.00001 #ifndef CPPTL_ATOMICCOUNTER_H_INCLUDED
00002 # define CPPTL_ATOMICCOUNTER_H_INCLUDED
00003
00004 # include <cpptl/config.h>
00005 # if CPPTL_USE_PTHREAD_ATOMIC
00006 # include <pthread.h>
00007 # endif
00008
00009 # if CPPTL_USE_WIN32_ATOMIC // Forwards declaration for WIN32 (avoid including windows.h)
00010
00011 # if _MSC_VER <= 1200 // VC++ 6 platform SDK as not volatile
00012 extern "C" __declspec(dllimport) long __stdcall InterlockedIncrement(long *);
00013 extern "C" __declspec(dllimport) long __stdcall InterlockedDecrement(long *);
00014 # else
00015 extern "C" __declspec(dllimport) long __stdcall InterlockedIncrement(long volatile *);
00016 extern "C" __declspec(dllimport) long __stdcall InterlockedDecrement(long volatile *);
00017 # endif
00018
00019 # endif // ifdef CPPTL_USE_WIN32_ATOMIC
00020
00021
00022
00023 namespace CppTL {
00024
00025
00026
00027
00028
00029
00030 # if CPPTL_USE_WIN32_ATOMIC
00031
00032 class CPPTL_API AtomicCounter
00033 {
00034 public:
00035 AtomicCounter( long count = 0 )
00036 : count_( count )
00037 {
00038 }
00039
00040 void increment()
00041 {
00042 InterlockedIncrement( const_cast< long * >( &count_ ) );
00043 }
00044
00047 bool decrement()
00048 {
00049 return InterlockedDecrement( const_cast< long * >( &count_ ) ) != 0;
00050 }
00051
00055 long count() const
00056 {
00057 return count_;
00058 }
00059
00060 private:
00061 volatile long count_;
00062 };
00063
00064
00065
00066
00067
00068
00069
00070 #elif CPPTL_USE_PTHREAD_ATOMIC
00071
00072 class CPPTL_API AtomicCounter
00073 {
00074 public:
00075 AtomicCounter( long count = 0 )
00076 : count_( count )
00077 {
00078 pthread_mutex_init( &lock_, 0 );
00079 }
00080
00081 AtomicCounter( const AtomicCounter &other )
00082 {
00083 pthread_mutex_init( &lock_, 0 );
00084 }
00085
00086 ~AtomicCounter()
00087 {
00088 pthread_mutex_destroy( &lock_ );
00089 }
00090
00091 AtomicCounter &operator =( const AtomicCounter &other )
00092 {
00093 return *this;
00094 }
00095
00096 void increment()
00097 {
00098 pthread_mutex_lock( &lock_ );
00099 ++count_;
00100 pthread_mutex_unlock( &lock_ );
00101 }
00102
00105 bool decrement()
00106 {
00107 pthread_mutex_lock( &lock_ );
00108 bool isNotNull = --count_ != 0;
00109 pthread_mutex_unlock( &lock_ );
00110 return isNotNull;
00111 }
00112
00116 long count() const
00117 {
00118 pthread_mutex_lock( &lock_ );
00119 long value = count_;
00120 pthread_mutex_unlock( &lock_ );
00121 return value;
00122 }
00123
00124 private:
00125 mutable pthread_mutex_t lock_;
00126 volatile long count_;
00127 };
00128
00129
00130
00131
00132
00133
00134
00135
00136 #else
00137
00138 class CPPTL_API AtomicCounter
00139 {
00140 public:
00141 AtomicCounter( long count = 0 );
00142 : count_( count )
00143 {
00144 }
00145
00146 void increment()
00147 {
00148 return ++count_;
00149 }
00150
00151 bool decrement()
00152 {
00153 return --count_ != 0;
00154 }
00155
00156 long count() const
00157 {
00158 return count_;
00159 }
00160
00161 private:
00162 long count_;
00163 };
00164
00165 # endif // ifdef CPPTL_USE_WIN32_ATOMIC
00166
00167
00168 }
00169
00170
00171 #endif // CPPTL_ATOMICCOUNTER_H_INCLUDED
00172