-
-
CAS (compare and set)
-
发表于: 2025-4-18 18:54 1592
-
每次时间一久,再次看到CAS相关的代码,还是会晕,所以今天记录一下。
调用REMOVE_FB(fb, pp, victim)的目的,是让fd指向victim的下一个节点,将victim从链表中删除:
为了防止多线程之间的干扰,REMOVE_FB()宏内部,使用了catomic_compare_and_exchange_val_acq()这种CAS操作接口:
1 2 3 4 5 6 7 8 9 10 11 | #define REMOVE_FB(fb, victim, pp) \ do \ { \ victim = pp; \ if (victim = = NULL) \ break ; \ pp = REVEAL_PTR (victim - >fd); \ if (__glibc_unlikely (pp ! = NULL && misaligned_chunk (pp))) \ malloc_printerr ( "malloc(): unaligned fastbin chunk detected" ); \ } \ while ((pp = catomic_compare_and_exchange_val_acq (fb, pp, victim)) ! = victim); |
关键就是要理解while()中的判断,为了直观,直接标注在图里了。
赞赏
他的文章
- how2heap动态演示 270
- CAS (compare and set) 1593
- shell+Makefile,坑我晚下班 2619
- 调试器:显示调用栈 3072
- helloworld减肥 + 单步执行原理 3273
赞赏
雪币:
留言: