atomic_counter.h

00001 /*
00002  * atomic_counter.h
00003  * This file is part of dbPager Server
00004  *
00005  * Copyright (C) 2008 - Dennis Prochko <wolfsoft@mail.ru>
00006  *
00007  * dbPager Server is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation version 3.
00010  *
00011  * dbPager Server is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with dbPager Server; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, 
00019  * Boston, MA  02110-1301  USA
00020  */
00021 
00022 #ifndef _ATOMIC_COUNTER_H_
00023 #define _ATOMIC_COUNTER_H_
00024 
00025 namespace dbp {
00026 
00028 
00033 class atomic_counter {
00034 public:
00036 
00039         atomic_counter(): _counter(0) { }
00041 
00046         atomic_counter(int value): _counter(value) { }
00048 
00053         atomic_counter(const atomic_counter &src) {
00054                 _counter = src._counter;
00055         }
00057         const atomic_counter& operator=(const atomic_counter &src) {
00058                 _counter = src._counter;
00059                 return *this;
00060         }
00062         inline void operator++() {
00063 #ifdef __GNUC__
00064                 __sync_add_and_fetch(&_counter, 1);
00065 #endif
00066         }
00068         inline void operator--() {
00069 #ifdef __GNUC__
00070                 __sync_sub_and_fetch(&_counter, 1);
00071 #endif
00072         }
00073 private:
00074         int _counter;
00075 };
00076 
00077 } // namespace
00078 
00079 #endif /*_ATOMIC_COUNTER_H_*/
00080 

 
Support This Project
SourceForge.net Logo