4

In managed languages how is atomicity of reference operations enforced?

On some platforms references have to be at least 64 bit, so it cannot rely on usually atomic 32 bit word operations...

Is it done simply through the use of locks? Wouldn't that be considered too costly in terms of performance?

To be more precise I didn't mean all reference operations, I mean mostly assignment which is atomic in for example .NET and Java. But even assignment is not obviously atomic since on most platforms you cannot reliably change value of 64 bit word in atomic way...

5
  • 1
    Which operations, and which languages? I can't think of a single language that guarantees atomicity for any operation on references (or other data) unless one explicitly opts in with an AtomicReference type or something like that.
    – user7043
    Feb 2, 2015 at 11:07
  • @delnan Assignment is in fact atomic. I would argue it has to be, otherwise it could break VM's memory model... AtomicReference is needed for atomic compare-and-swap and get-and-set.
    – zduny
    Feb 2, 2015 at 11:13
  • Oh, you're using "atomic" in the sense of "it can't ever happen that only half of the data was written". Usually atomicity refers to more than just that.
    – user7043
    Feb 2, 2015 at 11:20
  • I have doubts regarding the impossibility of "atomic" 64 bit writes. One doesn't need all the guarantees of an atomic operation (such as the memory fence or the visibility to other threads). You just need to make sure that, if the two 32 bit halves are spread over two cache lines, both are moved to modified state (assuming MESI) before one half is written and requested by another thread. I would not be surprised if ordinary non-atomic movq already does that. Remember, you can't have 64 bit references on IA-32.
    – user7043
    Feb 2, 2015 at 11:32
  • 5
    Platforms with 64 bit pointers/references generally provide efficient atomic 64 bit writes. Feb 2, 2015 at 13:04

1 Answer 1

2

To extend on comments, your assertion

On some platforms references have to be at least 64 bit, so it cannot rely on usually atomic 32 bit word operations

is not quite correct.

Yes, on some platforms your references will be 64 bit, but that's (universally?) because their word size is 64 bit. After all, if the word size didn't match your address size, the processor could suffer the same sort of atomicity issues internally. So essentially the VM implementations can rely on atomic word size writes. I would expect the addressable memory range to always line up with the processor word size, and if it didn't I would expect the VM implementation to force the addressable memory range to the processor word size, even if that means it cannot utilize all of the machine's resources.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.