在任何处理器平台下,都会有一些原子性操作,供操作系统使用,我们这里只讲x86下面的。在单处理器情况下,每条指令的执行都是原子性的,但在多处理器情况下,只有那些单独的读操作或写操作才是原子性的。为了弥补这一缺点,x86提供了附加的lock前缀,使带lock前缀的读修改写指令也能原子性执行。带lock前缀的指令在操作时会锁住总线,使自身的执行即使在多处理器间也是原子性执行的。xchg指令不带lock前缀也是原子性执行,也就是说xchg执行时默认会锁内存总线。原子性操作是线程间同步的基础,linux专门定义了一种只进行原子操作的类型atomic_t,并提供相关的原子读写调用API。本节就来分析这些原子操作在x86下的实现。

  1. typedef struct {
  2. volatile int counter;
  3. } atomic_t;

原子类型其实是int类型,只是禁止寄存器对其暂存。

  1. #define ATOMIC_INIT(i)  { (i) }

原子类型的初始化。32位x86平台下atomic API在arch/x86/include/asm/atomic_32.h中实现。

  1. static inline int atomic_read(const atomic_t *v)
  2. {
  3. return v->counter;
  4. }
  5. static inline void atomic_set(atomic_t *v, int i)
  6. {
  7. v->counter = i;
  8. }

单独的读操作或者写操作,在x86下都是原子性的。

  1. static inline void atomic_add(int i, atomic_t *v)
  2. {
  3. asm volatile(LOCK_PREFIX "addl %1,%0"
  4. : "+m" (v->counter)
  5. : "ir" (i));
  6. }
  7. static inline void atomic_sub(int i, atomic_t *v)
  8. {
  9. asm volatile(LOCK_PREFIX "subl %1,%0"
  10. : "+m" (v->counter)
  11. : "ir" (i));
  12. }

atomic_add和atomic_sub属于读修改写操作,实现时需要加lock前缀。

  1. static inline int atomic_sub_and_test(int i, atomic_t *v)
  2. {
  3. unsigned char c;
  4. asm volatile(LOCK_PREFIX "subl %2,%0; sete %1"
  5. : "+m" (v->counter), "=qm" (c)
  6. : "ir" (i) : "memory");
  7. return c;
  8. }

atomic_sub_and_test执行完减操作后检查结果是否为0。

  1. static inline void atomic_inc(atomic_t *v)
  2. {
  3. asm volatile(LOCK_PREFIX "incl %0"
  4. : "+m" (v->counter));
  5. }
  6. static inline void atomic_dec(atomic_t *v)
  7. {
  8. asm volatile(LOCK_PREFIX "decl %0"
  9. : "+m" (v->counter));
  10. }

atomic_inc和atomic_dec是递增递减操作。

  1. static inline int atomic_dec_and_test(atomic_t *v)
  2. {
  3. unsigned char c;
  4. asm volatile(LOCK_PREFIX "decl %0; sete %1"
  5. : "+m" (v->counter), "=qm" (c)
  6. : : "memory");
  7. return c != 0;
  8. }

atomic_dec_and_test在递减后检查结果是否为0。

  1. static inline int atomic_inc_and_test(atomic_t *v)
  2. {
  3. unsigned char c;
  4. asm volatile(LOCK_PREFIX "incl %0; sete %1"
  5. : "+m" (v->counter), "=qm" (c)
  6. : : "memory");
  7. return c != 0;
  8. }

atomic_inc_and_test在递增后检查结果是否为0。

  1. static inline int atomic_add_negative(int i, atomic_t *v)
  2. {
  3. unsigned char c;
  4. asm volatile(LOCK_PREFIX "addl %2,%0; sets %1"
  5. : "+m" (v->counter), "=qm" (c)
  6. : "ir" (i) : "memory");
  7. return c;
  8. }

atomic_add_negative在加操作后检查结果是否为负数。

  1. static inline int atomic_add_return(int i, atomic_t *v)
  2. {
  3. int __i;
  4. #ifdef CONFIG_M386
  5. unsigned long flags;
  6. if (unlikely(boot_cpu_data.x86 <= 3))
  7. goto no_xadd;
  8. #endif
  9. /* Modern 486+ processor */
  10. __i = i;
  11. asm volatile(LOCK_PREFIX "xaddl %0, %1"
  12. : "+r" (i), "+m" (v->counter)
  13. : : "memory");
  14. return i + __i;
  15. #ifdef CONFIG_M386
  16. no_xadd: /* Legacy 386 processor */
  17. local_irq_save(flags);
  18. __i = atomic_read(v);
  19. atomic_set(v, i + __i);
  20. local_irq_restore(flags);
  21. return i + __i;
  22. #endif
  23. }

atomic_add_return 不仅执行加操作,而且把相加的结果返回。它是通过xadd这一指令实现的。

  1. static inline int atomic_sub_return(int i, atomic_t *v)
  2. {
  3. return atomic_add_return(-i, v);
  4. }

atomic_sub_return 不仅执行减操作,而且把相减的结果返回。它是通过atomic_add_return实现的。

  1. static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
  2. {
  3. return cmpxchg(&v->counter, old, new);
  4. }
  5. #define cmpxchg(ptr, o, n)                      \
  6. ((__typeof__(*(ptr)))__cmpxchg((ptr), (unsigned long)(o),   \
  7. (unsigned long)(n),      \
  8. sizeof(*(ptr))))
  9. static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
  10. unsigned long newint size)
  11. {
  12. unsigned long prev;
  13. switch (size) {
  14. case 1:
  15. asm volatile(LOCK_PREFIX "cmpxchgb %b1,%2"
  16. : "=a"(prev)
  17. : "q"(new), "m"(*__xg(ptr)), "0"(old)
  18. : "memory");
  19. return prev;
  20. case 2:
  21. asm volatile(LOCK_PREFIX "cmpxchgw %w1,%2"
  22. : "=a"(prev)
  23. : "r"(new), "m"(*__xg(ptr)), "0"(old)
  24. : "memory");
  25. return prev;
  26. case 4:
  27. asm volatile(LOCK_PREFIX "cmpxchgl %k1,%2"
  28. : "=a"(prev)
  29. : "r"(new), "m"(*__xg(ptr)), "0"(old)
  30. : "memory");
  31. return prev;
  32. case 8:
  33. asm volatile(LOCK_PREFIX "cmpxchgq %1,%2"
  34. : "=a"(prev)
  35. : "r"(new), "m"(*__xg(ptr)), "0"(old)
  36. : "memory");
  37. return prev;
  38. }
  39. return old;
  40. }

atomic_cmpxchg是由cmpxchg指令完成的。它把旧值同atomic_t类型的值相比较,如果相同,就把新值存入atomic_t类型的值中,返回atomic_t类型变量中原有的值。

  1. static inline int atomic_xchg(atomic_t *v, int new)
  2. {
  3. return xchg(&v->counter, new);
  4. }
  5. #define xchg(ptr, v)                            \
  6. ((__typeof__(*(ptr)))__xchg((unsigned long)(v), (ptr), sizeof(*(ptr))))
  7. static inline unsigned long __xchg(unsigned long x, volatile void *ptr,
  8. int size)
  9. {
  10. switch (size) {
  11. case 1:
  12. asm volatile("xchgb %b0,%1"
  13. : "=q" (x)
  14. : "m" (*__xg(ptr)), "0" (x)
  15. : "memory");
  16. break;
  17. case 2:
  18. asm volatile("xchgw %w0,%1"
  19. : "=r" (x)
  20. : "m" (*__xg(ptr)), "0" (x)
  21. : "memory");
  22. break;
  23. case 4:
  24. asm volatile("xchgl %k0,%1"
  25. : "=r" (x)
  26. : "m" (*__xg(ptr)), "0" (x)
  27. : "memory");
  28. break;
  29. case 8:
  30. asm volatile("xchgq %0,%1"
  31. : "=r" (x)
  32. : "m" (*__xg(ptr)), "0" (x)
  33. : "memory");
  34. break;
  35. }
  36. return x;
  37. }

atomic_xchg则是将新值存入atomic_t类型的变量,并将变量的旧值返回。它使用xchg指令实现。

  1. /**
  2. * atomic_add_unless - add unless the number is already a given value
  3. * @v: pointer of type atomic_t
  4. * @a: the amount to add to v...
  5. * @u: ...unless v is equal to u.
  6. *
  7. * Atomically adds @a to @v, so long as @v was not already @u.
  8. * Returns non-zero if @v was not @u, and zero otherwise.
  9. */
  10. static inline int atomic_add_unless(atomic_t *v, int a, int u)
  11. {
  12. int c, old;
  13. c = atomic_read(v);
  14. for (;;) {
  15. if (unlikely(c == (u)))
  16. break;
  17. old = atomic_cmpxchg((v), c, c + (a));
  18. if (likely(old == c))
  19. break;
  20. c = old;
  21. }
  22. return c != (u);
  23. }

atomic_add_unless的功能比较特殊。它检查v是否等于u,如果不是则把v的值加上a,返回值表示相加前v是否等于u。因为在atomic_read和atomic_cmpxchg中间可能有其它的写操作,所以要循环检查自己的值是否被写进去。

  1. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  2. #define atomic_inc_return(v)  (atomic_add_return(1, v))
  3. #define atomic_dec_return(v)  (atomic_sub_return(1, v))

atomic_inc_not_zero在v值不是0时加1。

atomic_inc_return对v值加1,并返回相加结果。

atomic_dec_return对v值减1,并返回相减结果。

  1. #define atomic_clear_mask(mask, addr)               \
  2. asm volatile(LOCK_PREFIX "andl %0,%1"           \
  3. : : "r" (~(mask)), "m" (*(addr)) : "memory")

atomic_clear_mask清除变量某些位。

  1. #define atomic_set_mask(mask, addr)             \
  2. asm volatile(LOCK_PREFIX "orl %0,%1"                \
  3. : : "r" (mask), "m" (*(addr)) : "memory")

atomic_set_mask将变量的某些位置位。

  1. /* Atomic operations are already serializing on x86 */
  2. #define smp_mb__before_atomic_dec() barrier()
  3. #define smp_mb__after_atomic_dec()  barrier()
  4. #define smp_mb__before_atomic_inc() barrier()
  5. #define smp_mb__after_atomic_inc()  barrier()

因为x86的atomic操作大多使用原子指令或者带lock前缀的指令。带lock前缀的指令执行前会完成之前的读写操作,对于原子操作来说不会受之前对同一位置的读写操作,所以这里只是用空操作barrier()代替。barrier()的作用相当于告诉编译器这里有一个内存屏障,放弃在寄存器中的暂存值,重新从内存中读入。

本节的atomic_t类型操作是最基础的,为了介绍下面的内容,必须先介绍它。如果可以使用atomic_t类型代替临界区操作,也可以加快不少速度。

最新文章

  1. Python Day13
  2. 【原】移动web页面使用字体的思考
  3. json改造优化无刷新分页
  4. DropDownList的使用,RadioButtonList的使用
  5. 【原】在windows下使用VirtualEnv
  6. Atitit selenium3 新特性
  7. Android使用的设计模式2——策略模式
  8. RedirectResult,RedirectToRoute
  9. VC++ CopyFile函数使用方法
  10. uva 620 Cellular Structure
  11. CUDA学习ing..
  12. Python 读取某个目录下的文件
  13. easyui 在编辑状态下,动态修改其他列值。
  14. 从EventLoop到宏任务与微任务
  15. day 10函数二
  16. 关于JS 的cookie 操作 与 json 的数据结构 问题
  17. hiredis 使用 linux c++
  18. DevExpress v18.1新版亮点——Reporting篇(四)
  19. python+selenium的环境配置
  20. String、Date和Timestamp的互转

热门文章

  1. Flask实战第56天:板块管理
  2. AspNet5 Changes to [Activate] in beta-5
  3. noip 1999 回文数
  4. [P4063][JXOI2017]数列(DP)
  5. [xsy2309]数字表格
  6. 【dijkstra】【次短路】【fread】hdu6181 Two Paths
  7. 【贪心】【multiset】Tinkoff Challenge - Final Round (Codeforces Round #414, rated, Div. 1 + Div. 2) C. Naming Company
  8. linux centos下安装docker
  9. Problem D: 判断上否上三角矩阵
  10. Problem C: 矩阵对角线求和