Chapter 12
Atomic Operations
This chapter describes the global functions you use to perform atomic operations.
The functions define a portable API that may be reliably used in any environment.
Since not all operating environments provide access to such functions, their
performance may vary considerably.
Atomic Operations Functions
The API defined for the atomic functions is consistent across all supported
platforms. However, the implementation may vary greatly, and hence the
performance. On systems that do not provide direct access to atomic operators,
NSPR emulates the capabilities by using its own locking mechanisms. For such
systems, NSPR performs atomic operations just as efficiently as the client could.
Therefore, to preserve portability, it is recommended that clients use the NSPR API
for atomic operations.
PR_AtomicIncrement
PR_AtomicDecrement
PR_AtomicSet
PR_AtomicIncrement
Atomically increments a 32-bit value.
Syntax
#include <pratom.h>
PRInt32 PR_AtomicIncrement(PRInt32 *val);
Parameters
The function has the following parameter:
val
|
A pointer to the value to increment.
|
Returns
The function returns the incremented value (i.e., the result).
Description
The referenced variable is incremented by one. The result of the function is the
value of the memory after the operation. The writing of the memory is
unconditional.
PR_AtomicDecrement
Atomically decrements a 32-bit value.
Syntax
#include <pratom.h>
PRInt32 PR_AtomicDecrement(PRInt32 *val);
Parameters
The function has the following parameter:
val
|
A pointer to the value to decrement.
|
Returns
The function returns the decremented value (i.e., the result).
Description
PR_AtomicDecrement
first decrements the referenced variable by one. The value
returned is the referenced variable's final value. The modification to memory is
unconditional.
PR_AtomicSet
Atomically sets a 32-bit value and return its previous contents.
Syntax
#include <pratom.h>
PRInt32 PR_AtomicSet(
PRInt32 *val,
PRInt32 newval);
Parameters
The function has the following parameters:
val
|
A pointer to the value to be set.
|
newval
|
The new value to assign to the val parameter.
|
Returns
The function returns the prior value of the referenced variable.
Description
PR_AtomicSet
first reads the value of var, then updates it with the supplied value.
The returned value is the value that was read before memory was updated. The
memory modification is unconditional--that is, it isn't a test and set operation.