atomicity in 32/64 bit

独自空忆成欢 提交于 2019-12-01 04:44:12

问题


the question is about when does a 64bit load/store operations are considered to be atomic.

  • if i have a 64bit processor, but i'm using 32bit OS. Will i have 64bit atomicity?
  • if i'm using 64bit OS but running an 32bit application (using WoW64), will i have 64bit atomicity?

回答1:


The application must be running on a 64bit OS and in native 64bit mode to gain the advantages of x64, unsurprisingly. If you're running in 32bit mode, either on a 32bit OS (with a 32bit app), you will get 32bit atomicity. If you're running 64bit mode on a 64bit OS on a 64bit CPU, you will get 64bit atomicity. All of the components in the chain (app, OS, CPU) must be running 64bit to get 64bit.




回答2:


Not by default! But some SSE instructions under x86 support 64bit and 128bit atomic load/store, of course you must first ensure memory aligment. Look examples:

procedure Move64(var Source, Destination);
//Move 8 bytes atomicly from Source 8-byte aligned to Destination!
asm
  movq  xmm0, qword [Source]
  movq  qword [Destination], xmm0
end;

procedure Move64(newData: pointer; newReference: cardinal; var Destination); overload; 
//Move 8 bytes atomically into 8-byte Destination!
asm
  movd  xmm0, eax
  movd  xmm1, edx
  punpckldq xmm0, xmm1
  movq  qword [Destination], xmm0
end; 

procedure Move128(var Source, Destination);
//Move 16 bytes atomicly from Source to 16-byte aligned to Destination!
asm
  movdqa  xmm0, dqword [Source]
  movdqa  dqword [Destination], xmm0
end;



回答3:


It is only the 32/64 "bitness" of the application that matters - i.e. that your 64 bit loads/stores are atomic at the assembly level.
You need a 64 bit application to get that for "free".*
For the 64 bit application you need a 64 bit CPU and an OS that can execute it.
The OS can be anything, as long as it can start a 64 bit process on a 64 bit CPU.

if i have a 64bit processor, but i'm using 32bit OS. Will i have 64bit atomicity?

32 bit Windows, Linux: You simply cannot run 64 bit applications on 32 bit Windows or Linux, even on a 64 bit CPU.
32 bit Mac OS X: If your application is a 64 bit app, then yes.

if i'm using 64bit OS but running an 32bit application (using WoW64), will i have 64bit atomicity?

No. Your loads and stores at the machine code level will still be 32 bit loads and stores if you are running a 32 bit application.

*You can get 64 bit atomic reads/writes on a 32 bit CPU with compiler intrinsics where available and/or direct assembly.



来源:https://stackoverflow.com/questions/3123326/atomicity-in-32-64-bit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!