开发者

Are CIL Instructions atomic?

Does the .Net runtime make any guarantees about the atomicity of CIL instructions? If so, what are they? Are all CIL instructions atomic? Or does it depend on the atomicity of the machine code generated by the JIT compiler and ther开发者_如何学运维efore too platform specific to make any guarantees?


The C# spec talks about this, and the same truths essentially apply to CIL too; basically, all the integer types up to 4 bytes are guaranteed atomic, and any reference reads/writes are guaranteed atomic.

However, outside that no guarantees are made; double is not guaranteed atomic, for example - here Interlocked may be helpful.

To quote from ECMA 335 (Common Language Infrastructure (CLI) Partitions I to VI)

12.6.6 Atomic reads and writes

A conforming CLI shall guarantee that read and write access to properly aligned memory locations no larger than the native word size (the size of type native int) is atomic (see §12.6.2) when all the write accesses to a location are the same size. Atomic writes shall alter no bits other than those written. Unless explicit layout control (see Partition II (Controlling Instance Layout)) is used to alter the default behavior, data elements no larger than the natural word size (the size of a native int) shall be properly aligned. Object references shall be treated as though they are stored in the native word size.

[Note: There is no guarantee about atomic update (read-modify-write) of memory, except for methods provided for that purpose as part of the class library (see Partition IV). An atomic write of a ―small data item‖ (an item no larger than the native word size) is required to do an atomic read/modify/write on hardware that does not support direct writes to small data items. end note]

[Note: There is no guaranteed atomic access to 8-byte data when the size of a native int is 32 bits even though some implementations might perform atomic operations when the data is aligned on an 8-byte boundary. end note]

Since native int must be at least 32 bits, that is the most guarantee it can offer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜