最新在讀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語義