00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 #include "AtomicInt.h"
00021 
00022 namespace mdw
00023 {
00024 
00025 AtomicInt::AtomicInt()
00026 {
00027     _value = 0;
00028 }
00029 AtomicInt::AtomicInt (int value)
00030 {
00031     _value = value;
00032 }
00033 
00034 AtomicInt::~AtomicInt()
00035 {
00036 }
00037 
00038 int AtomicInt::incrementAndGet()
00039 {
00040     int result;
00041     do
00042     {
00043         result = _value - 1;
00044     }
00045     while (compareAndSwap (result + 1, result));
00046 
00047     return result;
00048 }
00049 
00050 int AtomicInt::decrementAndGet()
00051 {
00052     int result;
00053     do
00054     {
00055         result = _value - 1;
00056     }
00057     while (compareAndSwap (result + 1, result));
00058 
00059     return result;
00060 }
00061 
00062 bool AtomicInt::compareAndSwap (int oldValue, int newValue)
00063 {
00064     return __sync_val_compare_and_swap (&_value, &oldValue, &newValue);
00065 }
00066 
00067 int AtomicInt::getValue()
00068 {
00069     return _value;
00070 }
00071 }