Re: Memory ordering and flag/state variables
- From: soviet_bloke@xxxxxxxxxxx
- Date: 7 Sep 2006 03:54:44 -0700
Hi Tim
On Intel processors, a
write will always complete before a read from the same address.
In fact, the above is 100% true only for one- byte read/writes -
one-byte memory access always result in bus locking. However, when it
comes to words and dwords, your statement applies if and only if
memory access is properly aligned( i.e. respectively on word and dword
boundary), or LOCK prefix is explicitly used - non-aligned memory
access does not result in bus locking
Anton Bassov
Tim Roberts wrote:
BubbaGump <> wrote:
If a flag (in a device extension for instance) is set in some driver
routine and checked in another, shouldn't the first routine
technically perform some sort of memory barrier (KeMemoryBarrier() or
an InterlockedExchange()) to make sure the new value of the flag would
be seen by all CPUs before the function returns?
Usually, "before the function returns" is not particularly important, since
multiple threads could be inside the function at the same time.
Any time you have a piece of data that can be read and written in different
threads, you need to worry about interlocking. However, KeMemoryBarrier
really applies only in very special situations. On Intel processors, a
write will always complete before a read from the same address.
I found out about memory ordering a little while ago, and I've heard
the theories about how out-of-order loads and stores can cause
problems, but when I look at a lot of code I don't see as many
barriers as I'd expect and I wonder why it works. I often just see
plain old C assignment statements, and I wonder if it happens to work
either because current CPUs order their memory operations or because
there are already so many implicit barriers from other intervening
operations.
Right. Intel CPUs do it for you. There are certainly lots of chips that
don't; if you ever do embedded programming, you'll have to learn the proper
habits...
--
- Tim Roberts, timr@xxxxxxxxx
Providenza & Boekelheide, Inc.
.
- References:
- Re: Memory ordering and flag/state variables
- From: Tim Roberts
- Re: Memory ordering and flag/state variables
- Prev by Date: Re: File System - large file random access
- Next by Date: Re: WinXPSP2, Intel Core2Duo, Kernel drivers
- Previous by thread: Re: Memory ordering and flag/state variables
- Next by thread: Re: Memory ordering and flag/state variables
- Index(es):
Relevant Pages
|