From 5510375bdfdca7b73ac6c51920702d38b79ffcca Mon Sep 17 00:00:00 2001 From: methodus Date: Thu, 8 Nov 2012 19:29:02 +0100 Subject: Added atomic integer class. --- include/tools/atomic.h | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 include/tools/atomic.h (limited to 'include') diff --git a/include/tools/atomic.h b/include/tools/atomic.h new file mode 100644 index 0000000..4e619ee --- /dev/null +++ b/include/tools/atomic.h @@ -0,0 +1,78 @@ +/* + * atomic.h + * + * Created on: 06.11.2012 + * Author: savop + */ + +#ifndef ATOMIC_H_ +#define ATOMIC_H_ + +#include + +namespace upnp { + +namespace tools { + +namespace atomic { + +class AtomicInteger { +private: + uint32_t value; + cMutex mutex; + +public: + AtomicInteger() + : value(0) + {} + + inline void increment() { + mutex.Lock(); + ++value; + mutex.Unlock(); + } + + inline void decrement() { + mutex.Lock(); + --value; + mutex.Unlock(); + } + + operator unsigned int() const { + return Get(); + } + + unsigned int Get() const { + return value; + } + + AtomicInteger& operator++(){ + increment(); + return *this; + } + + AtomicInteger operator++(int){ + increment(); + return *this; + } + + AtomicInteger& operator--(){ + decrement(); + return *this; + } + + AtomicInteger operator--(int){ + decrement(); + return *this; + } + +}; + +} // namespace atomic + +} // namespace tools + +} // namespace upnp + + +#endif /* ATOMIC_H_ */ -- cgit v1.2.3