IMHO this documentation is misleading. The reason why InterlockedRead functions do not exist is that an assumption of aligned reads being atomic is implicit for all platforms Windows runs on. Unaligned variables can’t be used for Interlocked operations, so regardless of the actual implementation of the interlocked function, a read may be performed atomically
Sent from Surface Pro
From: xxxxx@qualcomm.com
Sent: Tuesday, April 01, 2014 2:36 PM
To: Windows System Software Devs Interest List
Below is the MSDN documentation for InterlockedExchange():
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683590(v=vs.85).aspx
It says “This function is atomic with respect to calls to other interlocked functions.” That seems reasonable because the caller should not make any assumptions about the implementation. On most architectures there is HW support for the interlocked functions and they are also atomic with respect to simple reads and writes. However, if HW support is unavailable, the interlocked functions would need to use spinlocks. Why, then, is there no InterlockedRead() or InterlockedWrite()? Consider the following example:
NTSTATUS try_lock(ULONG *x)
{
return(InterlockedExchange(x, 1) == 0 ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL);
}
void release_lock(ULONG *x)
{
*x = 0; // InterlockedWrite() is not available
}
Now suppose the follow sequence of events from two different processes (P2 already owns the lock):
P1: calls try_lock()
P1: acquires spinlock
P1: reads *x == 1
P2: calls release_lock()
P2: writes *x = 0
P1: writes *x = 1
P1: releases spinlock
P1: try_lock() returns STATUS_UNSUCCESSFUL
Now the lock is left in a bad state and all future calls to try_lock() will fail.
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