-
-
CAS (compare and set)
-
发表于: 2025-4-18 18:54 1716
-
每次时间一久,再次看到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()中的判断,为了直观,直接标注在图里了。
赞赏
他的文章
- KernelSnitch(侧信道泄漏内核地址部分) 1905
- CVE-2014-0038内核漏洞exp分析 3646
- 零碎记录 449
- how2heap动态演示 3507
- CAS (compare and set) 1717
赞赏
雪币:
留言: