CppDB
|
00001 00002 // 00003 // Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com> 00004 // 00005 // Distributed under: 00006 // 00007 // the Boost Software License, Version 1.0. 00008 // (See accompanying file LICENSE_1_0.txt or copy at 00009 // http://www.boost.org/LICENSE_1_0.txt) 00010 // 00011 // or (at your opinion) under: 00012 // 00013 // The MIT License 00014 // (See accompanying file MIT.txt or a copy at 00015 // http://www.opensource.org/licenses/mit-license.php) 00016 // 00018 #ifndef CPPDB_ATOMIC_COUNT_H 00019 #define CPPDB_ATOMIC_COUNT_H 00020 00021 #include <cppdb/defs.h> 00022 00023 00024 namespace cppdb { 00025 00038 00039 class CPPDB_API atomic_counter { 00040 public: 00044 explicit atomic_counter( long v); 00045 ~atomic_counter(); 00046 00050 long operator++() 00051 { 00052 return inc(); 00053 } 00057 long operator--() 00058 { 00059 return dec(); 00060 } 00064 operator long() const 00065 { 00066 return get(); 00067 } 00068 private: 00069 long inc(); 00070 long dec(); 00071 long get() const; 00072 00073 atomic_counter(atomic_counter const &); 00074 atomic_counter & operator=(atomic_counter const &); 00075 00076 mutable union { 00077 int i; 00078 unsigned ui; 00079 long l; 00080 unsigned long ul; 00081 long long ll; 00082 unsigned long long ull; 00083 } value_; 00084 // Is actually used for platforms without lock 00085 // it would not be used when atomic operations 00086 // available 00087 void *mutex_; 00088 }; 00089 00090 } // cppdb 00091 00092 #endif 00093 00094