免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 17021 | 回复: 33
打印 上一主题 下一主题

修改指针值的操作是一个原子操作? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-18 14:54 |只看该作者 |倒序浏览
ULK上在讲rcu的时候,提到写者会修改指针,然后说"修改指针值的操作是一个原子操作".
为什么是原子操作呢??怎么保证的?

论坛徽章:
0
2 [报告]
发表于 2008-05-18 15:22 |只看该作者
原帖由 iterator 于 2008-5-18 14:54 发表
ULK上在讲rcu的时候,提到写者会修改指针,然后说"修改指针值的操作是一个原子操作".
为什么是原子操作呢??怎么保证的?

在x86上单一的写是原子的。每个写操作之前会发起cache同步,所以一个CPU在写的时候,其它CPU无法发起cache同步,必须等该CPU写完(具体机制我也不清楚,原理应该是这样的)。
如果修改指针仅仅是给指针赋值,那就是一个单一的写操作,是原子的。

论坛徽章:
0
3 [报告]
发表于 2008-05-18 20:01 |只看该作者
原帖由 iterator 于 2008-5-18 14:54 发表
ULK上在讲rcu的时候,提到写者会修改指针,然后说"修改指针值的操作是一个原子操作".
为什么是原子操作呢??怎么保证的?

7.1.1       Guaranteed Atomic Operations
The Intel486 processor (and newer processors since) guarantees that the following
basic memory operations will always be carried out atomically:
•   Reading or writing a byte
•   Reading or writing a word aligned on a 16-bit boundary
•   Reading or writing a doubleword aligned on a 32-bit boundary
The Pentium processor (and newer processors since) guarantees that the following
additional memory operations will always be carried out atomically:
•   Reading or writing a quadword aligned on a 64-bit boundary
•   16-bit accesses to uncached memory locations that fit within a 32-bit data bus
The P6 family processors (and newer processors since) guarantee that the following
additional memory operation will always be carried out atomically:
•   Unaligned 16-, 32-, and 64-bit accesses to cached memory that fit within a cache
    line
Accesses to cacheable memory that are split across bus widths, cache lines, and
page boundaries are not guaranteed to be atomic by the Intel Core 2 Duo, Intel Core
Duo, Pentium M, Pentium 4, Intel Xeon, P6 family, Pentium, and Intel486 processors.
The Intel Core 2 Duo, Intel Core Duo, Pentium M, Pentium 4, Intel Xeon, and P6
family processors provide bus control signals that permit external memory
subsystems to make split accesses atomic; however, nonaligned data accesses will
seriously impact the performance of the processor and should be avoided.

论坛徽章:
0
4 [报告]
发表于 2008-05-19 10:48 |只看该作者
多谢.可3楼上怎么又说必须是32位对齐的呢?

另外,请教cacheable memory和externel memory subsystems都是什么意思?

论坛徽章:
0
5 [报告]
发表于 2008-05-19 11:40 |只看该作者
原帖由 iterator 于 2008-5-19 10:48 发表
多谢.可3楼上怎么又说必须是32位对齐的呢?

另外,请教cacheable memory和externel memory subsystems都是什么意思?

有不对齐的指针吗?
crpso贴的是IA32 spec关于原子操作的规定,里面内容有些是晦涩和不清楚的,例如“16-bit accesses to uncached memory locations that fit within a 32-bit data bus”
通常我们把它归纳成:对于单一的读写操作,对齐的访问是原子的,cacheline内的访问是原子的。
cacheable memory,大部分内存都是cacheable的,这里的cache指用CPU的cache。
externel memory subsystems,应该是指除了本地内存控制器直接访问的内存外,还有通过其它总线间接访问的内存。我觉得NUMA应该属于这种情况。

[ 本帖最后由 zx_wing 于 2008-5-19 11:42 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2008-05-19 12:31 |只看该作者
还是不太懂.要是一个结构体用attribute packed声明了,它内部的指针成员不就有可能不对齐了吗?

论坛徽章:
0
7 [报告]
发表于 2008-05-19 12:35 |只看该作者
原帖由 iterator 于 2008-5-19 12:31 发表
还是不太懂.要是一个结构体用attribute packed声明了,它内部的指针成员不就有可能不对齐了吗?


是有这个可能。

这样的32位写操作不见得是原子的。

但只要32位对齐(除非你告诉编译器不要这样做,否则都是对齐的),就是原子的。

论坛徽章:
0
8 [报告]
发表于 2008-05-19 12:41 |只看该作者
原帖由 iterator 于 2008-5-19 12:31 发表
还是不太懂.要是一个结构体用attribute packed声明了,它内部的指针成员不就有可能不对齐了吗?

不行的,__attribute__ ((packed))不是万能的,它无法构造一个不对齐的指针。
例如
struct
{
   char a;
   char *str;
   char b;
} __attribute__ ((packed)) x;

仍然会对齐

论坛徽章:
0
9 [报告]
发表于 2008-05-19 12:46 |只看该作者
`packed'
     The `packed' attribute specifies that a variable or structure field
     should have the smallest possible alignment--one byte for a
     variable,
and one bit for a field, unless you specify a larger
     value with the `aligned' attribute.

packed带来的紧凑模式是以最小单位对齐,并不是不对齐。

论坛徽章:
0
10 [报告]
发表于 2008-05-19 12:48 |只看该作者
原帖由 zx_wing 于 2008-5-19 12:41 发表

不行的,__attribute__ ((packed))不是万能的,它无法构造一个不对齐的指针。
例如

仍然会对齐


那个str应该不是32位对齐的呀
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP