Note also the suggested usage of the PAUSE instruction in a spin-lock wait.
joe
I was referring to the LOCK mechanism as the basis for multiprocessor
synchronization.
One uses a LOCKED instruction to form a lock to protect the sequence of
instructions (a simple spinlock).
To elaborate on the simple spinlock we could create a lock variable.
Initialize it to 0. To acquire the lock we do an XCHG with an argument
of
- If the previous value returned is 0 we have the lock, if it is 1 we
do
not and should delay and try again until we get the lock. To release the
lock we do the XCHG with an argument of 0. The longer sequence of code is
between the acquire and release and is protected by the (implicit) LOCK on
the XCHG instruction.
It is of course better to use the Windows SpinLock.
Ed
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@flounder.com
Sent: Friday, October 11, 2013 2:08 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] How does InterlockedExchange work
The LOCK prefix (or an implicitly-locked instruction) is a single
instruction cycle. So I’m not sure what you meant by “groups of
operations”
in the explanation below; if you execute three LOCKED instructions in a
row,
the combination of accidental sequencing and the built-in lock logic means
they may be interleaved in every possible order between two or more cores,
even if the first instruction is granted to CPU0, the lock does not
persist
across the next instruction, even if it is LOCKed as well. So the only
“groups” of operations that can be done are those that involve a single
instruction (E.g., InterlockedIncrement, which reads, increments, and
stores in one operation, or InterlockedCompareAndExchange, which does a
lot
more, but again, only within one instruction.
joe
> The InterlockedExchange uses the Intel XCHG instruction. XCHG returns
> the previous value of the location so it is a read/write sequence
> which is locked to be atomic.
>
>
>
>
>
> The XCHG instruction will guarantee that one of the
> InterlockedExchange operations in your example will complete before
> the other executes. We do not know if CPU1 or CPU2 will be the first
> to complete but we do know that each exchange will be atomic, the
> second can not start until the first is complete. That is the second
> to execute will return the value set by the first to execute (5 or 3) .
>
>
>
>
>
>
>
> The CPUs have a hardware line for interprocessor synchronization, the
> LOCK signal. The LOCK signal is asserted during the execution of a
> few instructions by default and can be selected by the LOCK
> instruction Prefix on a few more instructions. This allows the
> synchronization.
>
>
>
> Some instructions are atomic by default:
>
> The Intel486 processor (and newer processors since) guarantees that
> the following basic memory operations will always be carried out
atomically:
>
> . Reading or writing a byte
>
> . Reading or writing a word aligned on a 16-bit boundary
>
> . Reading or writing a doubleword aligned on a 32-bit boundary
>
> The Pentium processor (and newer processors since) guarantees that the
> following additional memory operations will always be carried out
> atomically:
>
> . Reading or writing a quadword aligned on a 64-bit boundary
>
> . 16-bit accesses to uncached memory locations that fit within a
> 32-bit data bus
>
> The P6 family processors (and newer processors since) guarantee that
> the following additional memory operation will always be carried out
> atomically:
>
> . Unaligned 16-, 32-, and 64-bit accesses to cached memory that fit
> within a cache line
>
>
>
> Other operations and collections of operations are not atomic by
> default.
> The LOCK prefix is allowed for some instructions to form LOCKED
> instructions.
>
>
>
> We can use LOCKED instructions to synchronize the other operations and
> groups of operations. We also use locked instructions to implement
> locking for much longer sequences of instructions. (For example the
> simplest form of spinlock can be constructed with the XCHG
> instruction.)
>
>
>
>
>
>
>
> The reference in the stackoverflow reply is the definitive reference
> for multiprocessor operations:
>
> "See “Bus Locking” in Chapter 8, "Multiple-Processor Management,"of
> the IntelR 64 and IA-32 Architectures Software Developer’s Manual,
> Volume 3A, for more information on bus locking."
>
> http:
> http://download.intel.com/design/processor/manuals/253668.pdf
>
>>
>
>>
>
>>
>
>>
>
>>
>
>>
>
>>
>
>>
>
>>
>
>>
>
>>
>
>> -----Original Message-----
>
>> From: mailto:xxxxx
> xxxxx@lists.osr.com
>
>> [mailto:xxxxx
> mailto:xxxxx@lists.osr.com] On Behalf Of
>
>> mailto:xxxxx xxxxx@gmx.de
>
>> Sent: Friday, October 11, 2013 11:54 AM
>
>> To: Windows System Software Devs Interest List
>
>> Subject: [ntdev] How does InterlockedExchange work
>
>>
>
>>
>
>>
>
>> Hi,
>
>>
>
>>
>
>>
>
>> I have a Windows 7 x86/amd64 driver where I want to synchronize access
>
>> to a variable. Can I use InterlockedExchange for it?
>
>>
>
>>
>
>>
>
>> My current understanding of InterlockedExchange is, that
>
>> InterlockedExchange is done via compiler intrinsics. That means, the
>
>> read (InterlockedExchange returns the old value) and the write is done
>
>> in one clock cycle. The interlocked functions are atomic only when the
>
>> variable is always accessed via an interlocked function.
>
>>
>
>>
>
>>
>
>> But what happens in this case:
>
>>
>
>>
>
>>
>
>> CPU1: InterlockedExchange(&Adapter->StatusVariable, 5);
>
>>
>
>> CPU2: InterlockedExchange(&Adapter->StatusVariable, 3);
>
>>
>
>>
>
>>
>
>> StatusVariable is written in the same clock cycle on two CPU cores.
>
>> Does the function notice that the variable is accessed and defer the
>
>> write to a different clock cycle? Or is it undefined which value the
>
>> variable has after the write? Is it also possible that the variable
>
>> contains garbage?
>
>>
>
>>
>
>>
>
>> This is a cross-post from stackoverflow, because I didn’t get
>
>> satisfying answers there.
>
>>
>
>>
>
>> http:>
>> o-cpu-
>
>> cores>
>
>> http:
> http://stackoverflow.com/questions/18076416/interlockedexchange-on-two
>
>> -cpu-c
>
>> ores
>
>>
>
>>
>
>>
>
>> —
>
>>
>
>> NTDEV is sponsored by OSR
>
>>
>
>>
>
>>
>
>> Visit the list at:
>
>> < http:
> http://www.osronline.com/showlists.cfm?list=ntdev>
>
>> http:
> http://www.osronline.com/showlists.cfm?list=ntdev
>
>>
>
>>
>
>>
>
>> OSR is HIRING!! See < http:
> http://www.osr.com/careers>
>
>> http: http://www.osr.com/careers
>
>>
>
>>
>
>>
>
>> For our schedule of WDF, WDM, debugging and other seminars visit:
>
>>
>
>> < http: http://www.osr.com/seminars>
> http: http://www.osr.com/seminars
>
>>
>
>>
>
>>
>
>> To unsubscribe, visit the List Server section of OSR Online at
>
>> < http:
> http://www.osronline.com/page.cfm?name=ListServer>
>
>> http:
> http://www.osronline.com/page.cfm?name=ListServer
>
>>
>
>>
>
>> —
>
>> NTDEV is sponsored by OSR
>
>>
>
>> Visit the list at: http:
> http://www.osronline.com/showlists.cfm?list=ntdev
>
>>
>
>> OSR is HIRING!! See http:
> http://www.osr.com/careers
>
>>
>
>> For our schedule of WDF, WDM, debugging and other seminars visit:
>
>> http: http://www.osr.com/seminars
>
>>
>
>> To unsubscribe, visit the List Server section of OSR Online at
>
>> http:
> http://www.osronline.com/page.cfm?name=ListServer
>
>
>
>
>
>
>
> —
>
> NTDEV is sponsored by OSR
>
>
>
> Visit the list at: http:
> http://www.osronline.com/showlists.cfm?list=ntdev
>
>
>
> OSR is HIRING!! See http:
> http://www.osr.com/careers
>
>
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
>
> http: http://www.osr.com/seminars
>
>
>
> To unsubscribe, visit the List Server section of OSR Online at
> http:
> http://www.osronline.com/page.cfm?name=ListServer
>
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer</http:></http:></http:></http:></http:></http:></http:></http:></http:></http:></http:></http:></http:></http:></http:></http:></http:></http:></mailto:xxxxx></mailto:xxxxx></mailto:xxxxx></http:>