atomic.3c (2010 09)

a
atomic(3C) atomic(3C)
pointed to by var is compared with cmpVal. If these values are equal, the value pointed to by var is
replaced with newVal. The old value pointed to by var is returned by the functions whether or not
replacement occurred.
The
atomic_swap()
and atomic_cas() functions have an additional byte_size argument whose
value can be 1, 2, 4 or 8 bytes (8, 16, 32 or 64 bits respectively). A value other than 1, 2, 4 or 8 will cause
the functions to return
0 and set errno to [EINVAL].
The
atomic_inc() and atomic_dec() functions have an additional byte_size argument whose value
can be 4 or 8 bytes (32 or 64 bits respectively). A value other than 4 or 8 will cause the functions to
return
0 and set errno to [EINVAL]. Note that the behavior of
atomic_inc() with byte_size equal to 4
is the same as the
atomic_inc_32()
function.
Notes
On Integrity systems:
All the functions except atomic_swap()
, atomic_inc(), atomic_dec() and atomic_cas()
are provided as inline functions in the
<atomic.h> header file. These are available by defining
_HP_ATOMIC_INLINE
macro. For example:
cc -D_HP_ATOMIC_INLINE test_atomic.c -latomic
It is always recommended that applications using inlined version of functions still link with liba-
tomic for compatibility reasons.
Inline functions are available only when applications are compiled using HP
cc or aCC compilers.
When other compilers like gcc are used, these functions will get resolved to the functions in
liba-
tomic by linking to that library.
On PA-RISC systems:
No inline functions are available. All functions get resolved to libatomic.
RETURN VALUE
Upon successful completion, all the functions return the old value which was pointed to by var. If the
value byte_size passed to
atomic_swap()
, atomic_inc(), atomic_dec(), and atomic_cas()
functions is not supported, errno is set to [EINVAL] and
0 is returned.
As
0 is a permissible return value in a successful situation for all of these functions, an application wish-
ing to check for error situations should set errno to 0, then call the appropriate function, and if it returns
0, check to see whether errno is non-zero.
ERRORS
[EINVAL] The byte_size passed to
atomic_swap()
, atomic_inc(), atomic_dec(), and
atomic_cas() functions is not supported.
EXAMPLES
1. Perform an atomic increment operation for a 32 bit memory location pointed to by var :
atomic_inc_32((uint32_t *)var);
2. Perform a compare and exchange on a 64 bit memory location pointed to by var:
atomic_cas_64((uint64_t *)var, (uint64_t)cmpVal, (uint64_t)newVal);
3. Alternatively, the atomic_cas() function with byte_size equal to 8 can be used to perform the
operation mentioned in example 2.
atomic_cas((uint64_t *)var, (uint64_t)cmpVal, (uint64_t)newVal, 8);
WARNINGS
The application program must make sure that the correct data type value is passed to these functions;
passing a value of the wrong data type could result in an undefined behavior.
For example, if a value of type
int is passed to the atomic_inc_64() function, which expects a
uint64_t, the result would be undefined.
For another example, if a value of type
int is passed to the atomic_inc() function and byte_size is set
to 1, the result would be undefined.
2 Hewlett-Packard Company 2 HP-UX 11i Version 3: September 2010