- 论坛徽章:
- 0
|
原帖由 albcamus 于 2006-3-9 15:37 发表
>对barrier() 我的理解是这只是一个compiler barrier, 这个barrier加入到代码中,会使cache invalidation
>而mb是hardware barrier,在代码运行中,CPU会prevent from reordering cache visit.
非常感 ...
碰巧这几天在看Solaris的锁机制,正好也涉及到类似的问题。对比x86和sparc的锁你就会发现,实际上,mutex_enter在x86就是只用lock,没有明着用barriers,但是sparc就不同了。再看看手册就知道,lock在这里起双重作用:
AMD64 Architecture Programmer's Manual, Volume 2, System Programming.
"Read/write barrier instructions
force all prior reads or writes to complete before
subsequent reads or writes are executed....
...
Serializing instructions, I/O instructions, and locked
instructions can also be used as read/write barriers." - Page 198, 199
"Locked Instructions - Before completing a locked instruction
(an instruction executed using the LOCK prefix), all
previous reads and writes must be written to memory, and
the locked instruction must complete before completing
subsequent writes." - Page 206
http://cvs.opensolaris.org/sourc ... ia32/ml/lock_prim.s
554 ENTRY_NP(mutex_enter)
555 movq %gs:CPU_THREAD, %rdx /* rdx = thread ptr */
556 xorl %eax, %eax /* rax = 0 (unheld adaptive) */
557 lock ----> lock在此处也起了barrier的作用
558 cmpxchgq %rdx, (%rdi) ----> 获得锁
559 jnz mutex_vector_enter
560 .mutex_enter_lockstat_patch_point:
561 ret
http://cvs.opensolaris.org/sourc ... c/v9/ml/lock_prim.s
382 ENTRY(mutex_enter)
383 mov THREAD_REG, %o1
384 casx [%o0], %g0, %o1 ! try to acquire as adaptive --> 获得锁
385 brnz,pn %o1, 1f ! locked or wrong type
386 membar #LoadLoad ---> mem barrier指令
387 .mutex_enter_lockstat_patch_point:
388 retl
[ 本帖最后由 Solaris12 于 2006-3-10 10:35 编辑 ] |
|