Jump to content
  • Advertisement
Sign in to follow this  
Catafriggm

Why Memory Barriers?

This topic is 4548 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

VC++ supports three memory barrier functions: WriteBarrier, ReadBarrier, and ReadWriteBarrier. As far as I can tell, unlike the DDK version of those functions, the VC++ versions do not execute instructions that force hardware access ordering, they only prevent the compiler from reordering instructions in the corresponding way. My question is... why? What use is instruction ordering if it doesn't affect what memory (and other CPUs) will actually see?

Share this post


Link to post
Share on other sites
Advertisement
Well, I don't even know what a memory barrier is, so maybe I'm talking nonsense. But the question seems a little similar to 'what use is a higher-order language, if it doesn't affect what the hardware sees?' It's all zeros and ones at some level; but the programmer doesn't want to interact with that.

Share this post


Link to post
Share on other sites
Modern CPU's cache things extremely aggressively. They can also run your program in a different order than how the instructions for the program are layed out. There is a bunch of complex logic inside the CPU that figures out if there are any dependencies between instructions and if not then it will feel free to do them in whatever order it wants. This works fine if you've only got a single processor or if your processors never touch the same memory. If they do try to use the same memory though you could be in trouble. Consider something really overly simplified like:

CPU 1
-----
y = 0
x = 0
x = 1
y = 1

CPU 2
-----
while (y != 1) wait
x = ?

What is the value for x for CPU 2? It looks like it has to be 1 but in fact it could be 0. Actually it's also possible that the wait loop will never see y turn to 1 and end up spinning forever. There are a variety of ways to solve this sort of problem but often the most performanant is to add memory barriers that force things to be more consistent with what you would expect in a naive reading of the code.

Now, while the CPU can do all this wierd stuff the compiler can *also* do it. This is basically the heart of Catafriggm's question - do the functions that VC++ provide effect the compiler, the CPU, or both. I don't know the answer myself and the docs are a bit ambiguous.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anon Mike
...interesting explanation...


Wow, really quite interesting. Never heard of the three(3) functions mentioned. Are they part of the Windows API or compiler dependant? I think that would surely answer the question of whether they are compiler scheduled or scheduled via the hardware.

GCS584

EDIT: AnonMike, do you mind putting up a link to the documentation you found (if available online), for I couldn't find it myself.

Share this post


Link to post
Share on other sites
In this case, these functions affect the behavior of the compiler. In other cases (such as the trigonometric functions), the compiler will generate certain op codes depending on the platform.

As far as Read/Write/ReadWrite|Barrier go: These functions are interpreted by the compiler, they do not actually affect the ouput code by emitting instructions, but mearly by ensuring that certain optimizations do not take place that can cause reads/writes to be eliminated or reordered. To quote MSDN: "Some intrinsics, such as __assume and __ReadWriteBarrier, provide information to the compiler, which affects the behavior of the optimizer."

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!