最新在读muduo源码,想写一个源码阅读的系列,就以这个为开篇吧
源码如下:
template<typename T>
class AtomicIntegerT : noncopyable
{
public:
AtomicIntegerT()
: value_(0)
{
}
// uncomment if you need copying and assignment
//
// AtomicIntegerT(const AtomicIntegerT& that)
// : value_(that.get())
// {}
//
// AtomicIntegerT& operator=(const AtomicIntegerT& that)
// {
// getAndSet(that.get());
// return *this;
// }
T get()
{
// in gcc >= 4.7: __atomic_load_n(&value_, __ATOMIC_SEQ_CST)
return __sync_val_compare_and_swap(&value_, 0, 0);
}
T getAndAdd(T x)
{
// in gcc >= 4.7: __atomic_fetch_add(&value_, x, __ATOMIC_SEQ_CST)
return __sync_fetch_and_add(&value_, x);
}
T addAndGet(T x)
{
return getAndAdd(x) + x;
}
T incrementAndGet()
{
return addAndGet(1);
}
T decrementAndGet()
{
return addAndGet(-1);
}
void add(T x)
{
getAndAdd(x);
}
void increment()
{
incrementAndGet();
}
void decrement()
{
decrementAndGet();
}
T getAndSet(T newValue)
{
// in gcc >= 4.7: __atomic_exchange_n(&value_, newValue, __ATOMIC_SEQ_CST)
return __sync_lock_test_and_set(&value_, newValue);
}
private:
volatile T value_;
};
} // namespace detail
typedef detail::AtomicIntegerT<int32_t> AtomicInt32;
typedef detail::AtomicIntegerT<int64_t> AtomicInt64;
其中主要接口只有几个,第一个是get,是使用__sync_val_compare_and_swap实现的,该函数的功能如下:
//type __sync_val_compare_and_swap (type *ptr, type oldval type newval, ...)
// 比较*ptr与oldval的值,如果两者相等,则将newval更新到*ptr并返回操作之前*ptr的值
所以 源码中__sync_val_compare_and_swap(&value_, 0, 0)的意思就是如果value_为0,则赋值为0,其实什么都没有改变,但是最后会返回value的值,所以实现了get功能。
第二个是getAndAdd,使用__sync_fetch_and_add实现,该函数的功能如下:
//type __sync_fetch_and_add (type *ptr, type value, ...)
// 将value加到*ptr上,结果更新到*ptr,并返回操作之前*ptr的值
所以getAndAdd实现的功能就是先将value_加x,然后返回加x之前的value_。
第三个就是getAndSet,使用__sync_lock_test_and_set实现,该函数的功能如下:
//type __sync_lock_test_and_set (type *ptr, type value, ...)
// 将value写入*ptr,对*ptr加锁,并返回操作之前*ptr的值。
getAndSet实现的功能就是使用newValue修改value_的值,并返回修改之前的value_。
其他的所有接口都是使用上面的几个接口间接实现的,都很好理解。
gcc内置的原子操作还有很多,从某个地方抄了一些,不记得在哪抄的了,如果有问题之后再修改吧,现在先放在这里。
//type __sync_fetch_and_add (type *ptr, type value, ...)
// 将value加到*ptr上,结果更新到*ptr,并返回操作之前*ptr的值
//type __sync_fetch_and_sub (type *ptr, type value, ...)
// 从*ptr减去value,结果更新到*ptr,并返回操作之前*ptr的值
//type __sync_fetch_and_or (type *ptr, type value, ...)
// 将*ptr与value相或,结果更新到*ptr, 并返回操作之前*ptr的值
//type __sync_fetch_and_and (type *ptr, type value, ...)
// 将*ptr与value相与,结果更新到*ptr,并返回操作之前*ptr的值
//type __sync_fetch_and_xor (type *ptr, type value, ...)
// 将*ptr与value异或,结果更新到*ptr,并返回操作之前*ptr的值
//type __sync_fetch_and_nand (type *ptr, type value, ...)
// 将*ptr取反后,与value相与,结果更新到*ptr,并返回操作之前*ptr的值
//type __sync_add_and_fetch (type *ptr, type value, ...)
// 将value加到*ptr上,结果更新到*ptr,并返回操作之后新*ptr的值
//type __sync_sub_and_fetch (type *ptr, type value, ...)
// 从*ptr减去value,结果更新到*ptr,并返回操作之后新*ptr的值
//type __sync_or_and_fetch (type *ptr, type value, ...)
// 将*ptr与value相或, 结果更新到*ptr,并返回操作之后新*ptr的值
//type __sync_and_and_fetch (type *ptr, type value, ...)
// 将*ptr与value相与,结果更新到*ptr,并返回操作之后新*ptr的值
//type __sync_xor_and_fetch (type *ptr, type value, ...)
// 将*ptr与value异或,结果更新到*ptr,并返回操作之后新*ptr的值
//type __sync_nand_and_fetch (type *ptr, type value, ...)
// 将*ptr取反后,与value相与,结果更新到*ptr,并返回操作之后新*ptr的值
//bool __sync_bool_compare_and_swap (type *ptr, type oldval type newval, ...)
// 比较*ptr与oldval的值,如果两者相等,则将newval更新到*ptr并返回true
//type __sync_val_compare_and_swap (type *ptr, type oldval type newval, ...)
// 比较*ptr与oldval的值,如果两者相等,则将newval更新到*ptr并返回操作之前*ptr的值
///__sync_synchronize (...)
// 发出完整内存栅栏
//type __sync_lock_test_and_set (type *ptr, type value, ...)
// 将value写入*ptr,对*ptr加锁,并返回操作之前*ptr的值。即,try spinlock语义
//void __sync_lock_release (type *ptr, ...)
// 将0写入到*ptr,并对*ptr解锁。即,unlock spinlock语义