FRR - 基础数据结构篇 - 数据同步atomlist.c

atomlist.c和atomlist.h文件中数据的解读离不开frratomic.h文件。

数据

/* atomic_atomptr_t may look a bit odd, it's for the sake of C++ compat */
typedef uintptr_t		atomptr_t;
typedef atomic_uintptr_t	atomic_atomptr_t;   //可能有些冗余,为了C++兼容

      看到这里可能有些迷糊,uintptr_t 和 atomic_uintptr_t 又是什么类型呢?在linux平台的/usr/include/stdint.h头文件中有如下定义:

/* Types for `void *' pointers.  */
#if __WORDSIZE == 64                  //64位机器
# ifndef __intptr_t_defined
typedef long int		intptr_t;
#  define __intptr_t_defined
# endif
typedef unsigned long int	uintptr_t;
#else                                 //32位机器
# ifndef __intptr_t_defined
typedef int			intptr_t;
#  define __intptr_t_defined
# endif
typedef unsigned int		uintptr_t;
#endif

      在64位的机器上,intptr_t和uintptr_t分别是long int、unsigned long int的别名;在32位的机器上,intptr_t和uintptr_t分别是int、unsigned int的别名。这两个数据类型别名也是为了“void *”指针。在C语言中,任何类型的指针都可以转换为void *类型,并且在将它转换回原来的类型时不会丢失信息。

    此外,在frratomic.h中有以下定义

/* ISO C11 */
#ifdef __cplusplus
#include <stdint.h>
#include <atomic>
using std::atomic_int;
using std::memory_order;

using std::memory_order_relaxed;                          //并行编程:设定6中指令执行顺序
using std::memory_order_acquire;
using std::memory_order_release;
using std::memory_order_acq_rel;
using std::memory_order_consume;
using std::memory_order_seq_cst;
 
typedef std::atomic<bool>		atomic_bool;              //多线程,原子类型
typedef std::atomic<size_t>		atomic_size_t;
typedef std::atomic<uint_fast32_t>	atomic_uint_fast32_t;
typedef std::atomic<uintptr_t>		atomic_uintptr_t;

#elif defined(HAVE_STDATOMIC_H)
#include <stdatomic.h>

/* These are available in gcc, but not in stdatomic */
#define atomic_add_fetch_explicit __atomic_add_fetch      
#define atomic_sub_fetch_explicit __atomic_sub_fetch
#define atomic_and_fetch_explicit __atomic_and_fetch
#define atomic_or_fetch_explicit __atomic_or_fetch

/* gcc 4.7 and newer */
#elif defined(HAVE___ATOMIC)

#define _Atomic volatile
#define _ATOMIC_WANT_TYPEDEFS

#define memory_order_relaxed __ATOMIC_RELAXED
#define memory_order_consume __ATOMIC_CONSUME
#define memory_order_acquire __ATOMIC_ACQUIRE
#define memory_order_release __ATOMIC_RELEASE
#define memory_order_acq_rel __ATOMIC_ACQ_REL
#define memory_order_seq_cst __ATOMIC_SEQ_CST

#define atomic_load_explicit __atomic_load_n
#define atomic_store_explicit __atomic_store_n
#define atomic_exchange_explicit __atomic_exchange_n
#define atomic_fetch_add_explicit __atomic_fetch_add
#define atomic_fetch_sub_explicit __atomic_fetch_sub
#define atomic_fetch_and_explicit __atomic_fetch_and
#define atomic_fetch_or_explicit __atomic_fetch_or

#define atomic_add_fetch_explicit __atomic_add_fetch
#define atomic_sub_fetch_explicit __atomic_sub_fetch
#define atomic_and_fetch_explicit __atomic_and_fetch
#define atomic_or_fetch_explicit __atomic_or_fetch

#define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1,      \
					      mem2)                            \
	__atomic_compare_exchange_n(atom, expect, desire, 1, mem1, mem2)
#define atomic_compare_exchange_strong_explicit(atom, expect, desire, mem1,    \
					      mem2)                            \
	__atomic_compare_exchange_n(atom, expect, desire, 0, mem1, mem2)

/* gcc 4.1 and newer,
 * clang 3.3 (possibly older)
 *
 * __sync_swap isn't in gcc's documentation, but clang has it
 *
 * note __sync_synchronize()
 */
#elif defined(HAVE___SYNC)

#define _Atomic volatile
#define _ATOMIC_WANT_TYPEDEFS

#define memory_order_relaxed 0
#define memory_order_consume 0
#define memory_order_acquire 0
#define memory_order_release 0
#define memory_order_acq_rel 0
#define memory_order_seq_cst 0

#define atomic_load_explicit(ptr, mem)                                         \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_fetch_and_add((ptr), 0);            \
		__sync_synchronize();                                          \
		rval;                                                          \
	})
#define atomic_store_explicit(ptr, val, mem)                                   \
	({                                                                     \
		__sync_synchronize();                                          \
		*(ptr) = (val);                                                \
		__sync_synchronize();                                          \
		(void)0;                                                       \
	})
#ifdef HAVE___SYNC_SWAP
#define atomic_exchange_explicit(ptr, val, mem)                                \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_swap((ptr, val), 0);                \
		__sync_synchronize();                                          \
		rval;                                                          \
	})
#else /* !HAVE___SYNC_SWAP */
#define atomic_exchange_explicit(ptr, val, mem)                                \
	({                                                                     \
		typeof(ptr) _ptr = (ptr);                                      \
		typeof(val) _val = (val);                                      \
		__sync_synchronize();                                          \
		typeof(*ptr) old1, old2 = __sync_fetch_and_add(_ptr, 0);       \
		do {                                                           \
			old1 = old2;                                           \
			old2 = __sync_val_compare_and_swap(_ptr, old1, _val);  \
		} while (old1 != old2);                                        \
		__sync_synchronize();                                          \
		old2;                                                          \
	})
#endif /* !HAVE___SYNC_SWAP */
#define atomic_fetch_add_explicit(ptr, val, mem)                               \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_fetch_and_add((ptr), (val));        \
		__sync_synchronize();                                          \
		rval;                                                          \
	})
#define atomic_fetch_sub_explicit(ptr, val, mem)                               \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_fetch_and_sub((ptr), (val));        \
		__sync_synchronize();                                          \
		rval;                                                          \
	})

#define atomic_compare_exchange_strong_explicit(atom, expect, desire, mem1,    \
					      mem2)                            \
	({                                                                     \
		typeof(atom) _atom = (atom);                                   \
		typeof(expect) _expect = (expect);                             \
		typeof(desire) _desire = (desire);                             \
		__sync_synchronize();                                          \
		typeof(*atom) rval =                                           \
			__sync_val_compare_and_swap(_atom, *_expect, _desire); \
		__sync_synchronize();                                          \
		bool ret = (rval == *_expect);                                 \
		*_expect = rval;                                               \
		ret;                                                           \
	})
#define atomic_compare_exchange_weak_explicit \
	atomic_compare_exchange_strong_explicit

#define atomic_fetch_and_explicit(ptr, val, mem)                               \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_fetch_and_and(ptr, val);            \
		__sync_synchronize();                                          \
		rval;                                                          \
	})
#define atomic_fetch_or_explicit(ptr, val, mem)                                \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_fetch_and_or(ptr, val);             \
		__sync_synchronize();                                          \
		rval;                                                          \
	})

#define atomic_add_fetch_explicit(ptr, val, mem)                               \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_add_and_fetch((ptr), (val));        \
		__sync_synchronize();                                          \
		rval;                                                          \
	})
#define atomic_sub_fetch_explicit(ptr, val, mem)                               \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_sub_and_fetch((ptr), (val));        \
		__sync_synchronize();                                          \
		rval;                                                          \
	})

#define atomic_and_fetch_explicit(ptr, val, mem)                               \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_and_and_fetch(ptr, val);            \
		__sync_synchronize();                                          \
		rval;                                                          \
	})
#define atomic_or_fetch_explicit(ptr, val, mem)                                \
	({                                                                     \
		__sync_synchronize();                                          \
		typeof(*ptr) rval = __sync_or_and_fetch(ptr, val);             \
		__sync_synchronize();                                          \
		rval;                                                          \
	})

#else /* !HAVE___ATOMIC && !HAVE_STDATOMIC_H */
#error no atomic functions...
#endif


#include <stdint.h>
#include <stdbool.h>

typedef _Atomic bool		atomic_bool;
typedef _Atomic size_t		atomic_size_t;
typedef _Atomic uint_fast32_t	atomic_uint_fast32_t;
typedef _Atomic uintptr_t	atomic_uintptr_t;
#endif

        其中memory_order等6种解释过并行编程。对于atomic_TYPE来说,如果定义C++,则用的是原子类型,否则就是volatile类型的别名。对于C++中的原子类型,具体可参考c++11 多线程(3)atomic 总结

       可以知道对于变量,用是原子变量或者volatile,对于变量的获取和修改会用__sync_synchronize()防止指令优化扰乱原本的顺序,即内存屏障。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值