共用方式為


Annotations for Interlocked Operands

A large family of functions takes as one of their parameters the address of a variable that should be accessed by using an interlocked processor instruction. These are cache read-through atomic instructions, and if the operands are used incorrectly, very subtle bugs result.

Use the following annotation for function parameters to identify it as an interlocked operand:

__drv_interlocked

System-supplied functions are already annotated for interlocked operands.

PREfast for Drivers (PFD) assumes that, if a variable is accessed by any interlocked function, the developer intends to share the variable between threads that could be running on different processors. Thus, any attempt to access or modify that variable without an interlocked operation is at risk of being done only in the local processor's cache, which would be potentially incorrect code. Variables in the local stack frame that are used as the interlocked operand are both very unusual and often dangerous, and usually indicate a misuse of the function.

The following example shows the annotation for the InterlockedExchange function. This annotation specifies that the Target parameter must always be accessed by using an interlocked operation.

LONG 
  InterlockedExchange(
    __inout __drv_in(__drv_interlocked) PLONG  Target,
    __in LONG  Value
    );

 

 

Send comments about this topic to Microsoft

Build date: 5/3/2011