CppCMS
booster/atomic_counter.h
00001 //
00002 //  Copyright (C) 2009-2012 Artyom Beilis (Tonkikh)
00003 //
00004 //  Distributed under the Boost Software License, Version 1.0. (See
00005 //  accompanying file LICENSE_1_0.txt or copy at
00006 //  http://www.boost.org/LICENSE_1_0.txt)
00007 //
00008 #ifndef BOOSTER_ATOMIC_COUNT_H
00009 #define BOOSTER_ATOMIC_COUNT_H
00010 
00011 #include <booster/config.h>
00012 
00013 
00018 namespace booster {
00019 
00032 
00033         class BOOSTER_API atomic_counter {
00034         public:
00038                 explicit atomic_counter( long v );
00039                 ~atomic_counter();
00040 
00044                 long operator++()
00045                 {
00046                         return inc();
00047                 }
00051                 long operator--()
00052                 {
00053                         return dec();
00054                 }
00058                 operator long() const 
00059                 {
00060                         return get();
00061                 }
00062         private:
00063                 long inc();
00064                 long dec();
00065                 long get() const;
00066 
00067                 atomic_counter(atomic_counter const &);
00068                 atomic_counter & operator=(atomic_counter const &);
00069 
00070                 mutable union {
00071                         int i;
00072                         unsigned ui;
00073                         long l;
00074                         unsigned long ul;
00075                         long long ll;
00076                         unsigned long long ull;
00077                 } value_;
00078                 // Is actually used for platforms without lock
00079                 // it would not be used when atomic operations
00080                 // available
00081                 void *mutex_;
00082         };
00083 
00084 } // booster
00085 
00086 #endif
00087 
00088